@tonysamperi/krypto 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/LICENCE +21 -0
  2. package/README.md +119 -0
  3. package/dist/cjs/algo/aes.class.d.cts +16 -0
  4. package/dist/cjs/algo/des.class.d.cts +19 -0
  5. package/dist/cjs/algo/index.d.cts +7 -0
  6. package/dist/cjs/algo/md5.class.d.cts +13 -0
  7. package/dist/cjs/algo/sha1.class.d.cts +9 -0
  8. package/dist/cjs/algo/sha256.class.d.cts +9 -0
  9. package/dist/cjs/algo/sha512.class.d.cts +9 -0
  10. package/dist/cjs/algo/triple-des.class.d.cts +16 -0
  11. package/dist/cjs/enc/base64.class.d.cts +31 -0
  12. package/dist/cjs/enc/encoder.model.d.cts +6 -0
  13. package/dist/cjs/enc/hex.class.d.cts +27 -0
  14. package/dist/cjs/enc/index.d.cts +6 -0
  15. package/dist/cjs/enc/latin1.class.d.cts +27 -0
  16. package/dist/cjs/enc/utf16.class.d.cts +28 -0
  17. package/dist/cjs/enc/utf8.class.d.cts +28 -0
  18. package/dist/cjs/format/formatter.interface.d.cts +5 -0
  19. package/dist/cjs/format/open-ssl.class.d.cts +27 -0
  20. package/dist/cjs/helpers/aes.helper.d.cts +2 -0
  21. package/dist/cjs/helpers/des.helper.d.cts +2 -0
  22. package/dist/cjs/helpers/index.d.cts +7 -0
  23. package/dist/cjs/helpers/md5.helper.d.cts +2 -0
  24. package/dist/cjs/helpers/sha1.helper.d.cts +2 -0
  25. package/dist/cjs/helpers/sha256.helper.d.cts +2 -0
  26. package/dist/cjs/helpers/sha512.helper.d.cts +2 -0
  27. package/dist/cjs/helpers/triple-des.helper.d.cts +2 -0
  28. package/dist/cjs/index.cjs +1 -0
  29. package/dist/cjs/index.d.cts +7 -0
  30. package/dist/cjs/kdf/evpkdf.class.d.cts +42 -0
  31. package/dist/cjs/kdf/index.d.cts +3 -0
  32. package/dist/cjs/kdf/kdf.model.d.cts +7 -0
  33. package/dist/cjs/kdf/open-ssl-kdf.class.d.cts +21 -0
  34. package/dist/cjs/kdf/pbkdf2.class.d.cts +18 -0
  35. package/dist/cjs/lib/block-cipher.class.d.cts +13 -0
  36. package/dist/cjs/lib/buffered-block-algorithm-config.interface.d.cts +13 -0
  37. package/dist/cjs/lib/buffered-block-algorithm.class.d.cts +54 -0
  38. package/dist/cjs/lib/cipher-helper.class.d.cts +10 -0
  39. package/dist/cjs/lib/cipher-params.class.d.cts +53 -0
  40. package/dist/cjs/lib/cipher.class.d.cts +112 -0
  41. package/dist/cjs/lib/hasher-constructor.type.d.cts +3 -0
  42. package/dist/cjs/lib/hasher-helper.type.d.cts +3 -0
  43. package/dist/cjs/lib/hasher.class.d.cts +58 -0
  44. package/dist/cjs/lib/hmac.class.d.cts +11 -0
  45. package/dist/cjs/lib/index.d.cts +8 -0
  46. package/dist/cjs/lib/password-based-cipher.class.d.cts +54 -0
  47. package/dist/cjs/lib/serializable-cipher.class.d.cts +73 -0
  48. package/dist/cjs/lib/word-array.class.d.cts +75 -0
  49. package/dist/cjs/lib/x64-word-array.class.d.cts +10 -0
  50. package/dist/cjs/lib/x64-word.class.d.cts +14 -0
  51. package/dist/cjs/mode/block-cipher-mode-algorithm.class.d.cts +20 -0
  52. package/dist/cjs/mode/block-cipher-mode.class.d.cts +28 -0
  53. package/dist/cjs/mode/cbc/cbc-decryptor.class.d.cts +17 -0
  54. package/dist/cjs/mode/cbc/cbc-encryptor.class.d.cts +17 -0
  55. package/dist/cjs/mode/cbc/cbc.class.d.cts +10 -0
  56. package/dist/cjs/mode/ctr/ctr-decryptor.class.d.cts +3 -0
  57. package/dist/cjs/mode/ctr/ctr-encryptor.class.d.cts +16 -0
  58. package/dist/cjs/mode/ctr/ctr.class.d.cts +10 -0
  59. package/dist/cjs/mode/ecb/ecb-decryptor.class.d.cts +14 -0
  60. package/dist/cjs/mode/ecb/ecb-encryptor.class.d.cts +14 -0
  61. package/dist/cjs/mode/ecb/ecb.class.d.cts +10 -0
  62. package/dist/cjs/mode/index.d.cts +11 -0
  63. package/dist/cjs/pad/index.d.cts +4 -0
  64. package/dist/cjs/pad/no-padding.class.d.cts +25 -0
  65. package/dist/cjs/pad/padding.model.d.cts +6 -0
  66. package/dist/cjs/pad/pkcs7.class.d.cts +25 -0
  67. package/dist/cjs/pad/zero-padding.class.d.cts +25 -0
  68. package/dist/esm/algo/aes.class.d.ts +16 -0
  69. package/dist/esm/algo/aes.class.js +138 -0
  70. package/dist/esm/algo/des.class.d.ts +19 -0
  71. package/dist/esm/algo/des.class.js +733 -0
  72. package/dist/esm/algo/index.d.ts +7 -0
  73. package/dist/esm/algo/index.js +7 -0
  74. package/dist/esm/algo/md5.class.d.ts +13 -0
  75. package/dist/esm/algo/md5.class.js +137 -0
  76. package/dist/esm/algo/sha1.class.d.ts +9 -0
  77. package/dist/esm/algo/sha1.class.js +66 -0
  78. package/dist/esm/algo/sha256.class.d.ts +9 -0
  79. package/dist/esm/algo/sha256.class.js +96 -0
  80. package/dist/esm/algo/sha512.class.d.ts +9 -0
  81. package/dist/esm/algo/sha512.class.js +221 -0
  82. package/dist/esm/algo/triple-des.class.d.ts +16 -0
  83. package/dist/esm/algo/triple-des.class.js +37 -0
  84. package/dist/esm/enc/base64.class.d.ts +31 -0
  85. package/dist/esm/enc/base64.class.js +80 -0
  86. package/dist/esm/enc/encoder.model.d.ts +6 -0
  87. package/dist/esm/enc/encoder.model.js +5 -0
  88. package/dist/esm/enc/hex.class.d.ts +27 -0
  89. package/dist/esm/enc/hex.class.js +45 -0
  90. package/dist/esm/enc/index.d.ts +6 -0
  91. package/dist/esm/enc/index.js +6 -0
  92. package/dist/esm/enc/latin1.class.d.ts +27 -0
  93. package/dist/esm/enc/latin1.class.js +44 -0
  94. package/dist/esm/enc/utf16.class.d.ts +28 -0
  95. package/dist/esm/enc/utf16.class.js +46 -0
  96. package/dist/esm/enc/utf8.class.d.ts +28 -0
  97. package/dist/esm/enc/utf8.class.js +39 -0
  98. package/dist/esm/format/formatter.interface.d.ts +5 -0
  99. package/dist/esm/format/formatter.interface.js +0 -0
  100. package/dist/esm/format/open-ssl.class.d.ts +27 -0
  101. package/dist/esm/format/open-ssl.class.js +57 -0
  102. package/dist/esm/helpers/aes.helper.d.ts +2 -0
  103. package/dist/esm/helpers/aes.helper.js +6 -0
  104. package/dist/esm/helpers/des.helper.d.ts +2 -0
  105. package/dist/esm/helpers/des.helper.js +6 -0
  106. package/dist/esm/helpers/index.d.ts +7 -0
  107. package/dist/esm/helpers/index.js +16 -0
  108. package/dist/esm/helpers/md5.helper.d.ts +2 -0
  109. package/dist/esm/helpers/md5.helper.js +6 -0
  110. package/dist/esm/helpers/sha1.helper.d.ts +2 -0
  111. package/dist/esm/helpers/sha1.helper.js +6 -0
  112. package/dist/esm/helpers/sha256.helper.d.ts +2 -0
  113. package/dist/esm/helpers/sha256.helper.js +6 -0
  114. package/dist/esm/helpers/sha512.helper.d.ts +2 -0
  115. package/dist/esm/helpers/sha512.helper.js +6 -0
  116. package/dist/esm/helpers/triple-des.helper.d.ts +2 -0
  117. package/dist/esm/helpers/triple-des.helper.js +6 -0
  118. package/dist/esm/index.d.ts +7 -0
  119. package/dist/esm/index.js +43 -0
  120. package/dist/esm/kdf/evpkdf.class.d.ts +42 -0
  121. package/dist/esm/kdf/evpkdf.class.js +59 -0
  122. package/dist/esm/kdf/index.d.ts +3 -0
  123. package/dist/esm/kdf/index.js +3 -0
  124. package/dist/esm/kdf/kdf.model.d.ts +7 -0
  125. package/dist/esm/kdf/kdf.model.js +5 -0
  126. package/dist/esm/kdf/open-ssl-kdf.class.d.ts +21 -0
  127. package/dist/esm/kdf/open-ssl-kdf.class.js +32 -0
  128. package/dist/esm/kdf/pbkdf2.class.d.ts +18 -0
  129. package/dist/esm/kdf/pbkdf2.class.js +43 -0
  130. package/dist/esm/lib/block-cipher.class.d.ts +13 -0
  131. package/dist/esm/lib/block-cipher.class.js +55 -0
  132. package/dist/esm/lib/buffered-block-algorithm-config.interface.d.ts +13 -0
  133. package/dist/esm/lib/buffered-block-algorithm-config.interface.js +0 -0
  134. package/dist/esm/lib/buffered-block-algorithm.class.d.ts +54 -0
  135. package/dist/esm/lib/buffered-block-algorithm.class.js +99 -0
  136. package/dist/esm/lib/cipher-helper.class.d.ts +10 -0
  137. package/dist/esm/lib/cipher-helper.class.js +24 -0
  138. package/dist/esm/lib/cipher-params.class.d.ts +53 -0
  139. package/dist/esm/lib/cipher-params.class.js +89 -0
  140. package/dist/esm/lib/cipher.class.d.ts +112 -0
  141. package/dist/esm/lib/cipher.class.js +112 -0
  142. package/dist/esm/lib/hasher-constructor.type.d.ts +3 -0
  143. package/dist/esm/lib/hasher-constructor.type.js +0 -0
  144. package/dist/esm/lib/hasher-helper.type.d.ts +3 -0
  145. package/dist/esm/lib/hasher-helper.type.js +0 -0
  146. package/dist/esm/lib/hasher.class.d.ts +58 -0
  147. package/dist/esm/lib/hasher.class.js +77 -0
  148. package/dist/esm/lib/hmac.class.d.ts +11 -0
  149. package/dist/esm/lib/hmac.class.js +39 -0
  150. package/dist/esm/lib/index.d.ts +8 -0
  151. package/dist/esm/lib/index.js +18 -0
  152. package/dist/esm/lib/password-based-cipher.class.d.ts +54 -0
  153. package/dist/esm/lib/password-based-cipher.class.js +92 -0
  154. package/dist/esm/lib/serializable-cipher.class.d.ts +73 -0
  155. package/dist/esm/lib/serializable-cipher.class.js +108 -0
  156. package/dist/esm/lib/word-array.class.d.ts +75 -0
  157. package/dist/esm/lib/word-array.class.js +123 -0
  158. package/dist/esm/lib/x64-word-array.class.d.ts +10 -0
  159. package/dist/esm/lib/x64-word-array.class.js +25 -0
  160. package/dist/esm/lib/x64-word.class.d.ts +14 -0
  161. package/dist/esm/lib/x64-word.class.js +47 -0
  162. package/dist/esm/mode/block-cipher-mode-algorithm.class.d.ts +20 -0
  163. package/dist/esm/mode/block-cipher-mode-algorithm.class.js +22 -0
  164. package/dist/esm/mode/block-cipher-mode.class.d.ts +28 -0
  165. package/dist/esm/mode/block-cipher-mode.class.js +36 -0
  166. package/dist/esm/mode/cbc/cbc-decryptor.class.d.ts +17 -0
  167. package/dist/esm/mode/cbc/cbc-decryptor.class.js +40 -0
  168. package/dist/esm/mode/cbc/cbc-encryptor.class.d.ts +17 -0
  169. package/dist/esm/mode/cbc/cbc-encryptor.class.js +39 -0
  170. package/dist/esm/mode/cbc/cbc.class.d.ts +10 -0
  171. package/dist/esm/mode/cbc/cbc.class.js +10 -0
  172. package/dist/esm/mode/ctr/ctr-decryptor.class.d.ts +3 -0
  173. package/dist/esm/mode/ctr/ctr-decryptor.class.js +6 -0
  174. package/dist/esm/mode/ctr/ctr-encryptor.class.d.ts +16 -0
  175. package/dist/esm/mode/ctr/ctr-encryptor.class.js +33 -0
  176. package/dist/esm/mode/ctr/ctr.class.d.ts +10 -0
  177. package/dist/esm/mode/ctr/ctr.class.js +10 -0
  178. package/dist/esm/mode/ecb/ecb-decryptor.class.d.ts +14 -0
  179. package/dist/esm/mode/ecb/ecb-decryptor.class.js +19 -0
  180. package/dist/esm/mode/ecb/ecb-encryptor.class.d.ts +14 -0
  181. package/dist/esm/mode/ecb/ecb-encryptor.class.js +19 -0
  182. package/dist/esm/mode/ecb/ecb.class.d.ts +10 -0
  183. package/dist/esm/mode/ecb/ecb.class.js +10 -0
  184. package/dist/esm/mode/index.d.ts +11 -0
  185. package/dist/esm/mode/index.js +24 -0
  186. package/dist/esm/pad/index.d.ts +4 -0
  187. package/dist/esm/pad/index.js +8 -0
  188. package/dist/esm/pad/no-padding.class.d.ts +25 -0
  189. package/dist/esm/pad/no-padding.class.js +28 -0
  190. package/dist/esm/pad/padding.model.d.ts +6 -0
  191. package/dist/esm/pad/padding.model.js +5 -0
  192. package/dist/esm/pad/pkcs7.class.d.ts +25 -0
  193. package/dist/esm/pad/pkcs7.class.js +40 -0
  194. package/dist/esm/pad/zero-padding.class.d.ts +25 -0
  195. package/dist/esm/pad/zero-padding.class.js +38 -0
  196. package/package.json +82 -0
@@ -0,0 +1,108 @@
1
+ import { WordArray } from "./word-array.class.js";
2
+ import { OpenSSL } from "../format/open-ssl.class.js";
3
+ import { CipherParams } from "./cipher-params.class.js";
4
+ class SerializableCipher {
5
+ /**
6
+ * Decrypts serialized ciphertext.
7
+ *
8
+ * @param cipher The cipher algorithm to use.
9
+ * @param ciphertext The ciphertext to decrypt.
10
+ * @param key The key.
11
+ * @param optionalCfg The configuration options to use for this operation.
12
+ *
13
+ * @return The plaintext.
14
+ *
15
+ * @example
16
+ *
17
+ * let plaintext = SerializableCipher.decrypt(
18
+ * AESAlgorithm,
19
+ * formattedCiphertext,
20
+ * key, {
21
+ * iv: iv,
22
+ * format: CryptoJS.format.OpenSSL
23
+ * }
24
+ * );
25
+ *
26
+ * let plaintext = SerializableCipher.decrypt(
27
+ * AESAlgorithm,
28
+ * ciphertextParams,
29
+ * key, {
30
+ * iv: iv,
31
+ * format: CryptoJS.format.OpenSSL
32
+ * }
33
+ * );
34
+ */
35
+ static decrypt(cipher, ciphertext, key, optionalCfg) {
36
+ const cfg = Object.assign({}, this.cfg, optionalCfg);
37
+ if (!cfg.format) {
38
+ throw new Error("could not determine format");
39
+ }
40
+ ciphertext = this._parse(ciphertext, cfg.format);
41
+ if (!ciphertext.ciphertext) {
42
+ throw new Error("could not determine ciphertext");
43
+ }
44
+ return cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
45
+ }
46
+ /**
47
+ * Encrypts a message.
48
+ *
49
+ * @param cipher The cipher algorithm to use.
50
+ * @param message The message to encrypt.
51
+ * @param key The key.
52
+ * @param cfg (Optional) The configuration options to use for this operation.
53
+ *
54
+ * @return A cipher params object.
55
+ *
56
+ * @example
57
+ *
58
+ * let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
59
+ * let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
60
+ * let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, {
61
+ * iv: iv,
62
+ * format: CryptoJS.format.OpenSSL
63
+ * });
64
+ */
65
+ static encrypt(cipher, message, key, cfg) {
66
+ const config = Object.assign({}, this.cfg, cfg);
67
+ const encryptor = cipher.createEncryptor(key, config);
68
+ const ciphertext = encryptor.finalize(message);
69
+ return new CipherParams({
70
+ ciphertext,
71
+ key,
72
+ iv: encryptor.cfg.iv,
73
+ algorithm: cipher,
74
+ mode: encryptor.cfg.mode,
75
+ padding: encryptor.cfg.padding,
76
+ blockSize: encryptor.cfg.blockSize,
77
+ formatter: config.format
78
+ });
79
+ }
80
+ /**
81
+ * Converts serialized ciphertext to CipherParams,
82
+ * else assumed CipherParams already and returns ciphertext unchanged.
83
+ *
84
+ * @param ciphertext The ciphertext.
85
+ * @param format The formatting strategy to use to parse serialized ciphertext.
86
+ *
87
+ * @return The deserialized ciphertext.
88
+ *
89
+ * @example
90
+ *
91
+ * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
92
+ */
93
+ static _parse(ciphertext, format) {
94
+ if (typeof ciphertext === "string") {
95
+ return format.parse(ciphertext);
96
+ } else {
97
+ return ciphertext;
98
+ }
99
+ }
100
+ }
101
+ SerializableCipher.cfg = {
102
+ blockSize: 4,
103
+ iv: new WordArray([]),
104
+ format: OpenSSL
105
+ };
106
+ export {
107
+ SerializableCipher
108
+ };
@@ -0,0 +1,75 @@
1
+ export declare class WordArray {
2
+ sigBytes: number;
3
+ words: number[];
4
+ /**
5
+ * Initializes a newly created word array.
6
+ *
7
+ * @param words (Optional) An array of 32-bit words.
8
+ * @param sigBytes (Optional) The number of significant bytes in the words.
9
+ *
10
+ * @example
11
+ *
12
+ * let wordArray = new WordArray();
13
+ * let wordArray = new WordArray([0x00010203, 0x04050607]);
14
+ * let wordArray = new WordArray([0x00010203, 0x04050607], 6);
15
+ */
16
+ constructor(words?: number[], sigBytes?: number);
17
+ /**
18
+ * Creates a word array filled with random bytes.
19
+ *
20
+ * @param nBytes The number of random bytes to generate.
21
+ *
22
+ * @return The random word array.
23
+ *
24
+ * @example
25
+ *
26
+ * let wordArray = WordArray.random(16);
27
+ */
28
+ static random(nBytes: number): WordArray;
29
+ /**
30
+ * Removes insignificant bits.
31
+ *
32
+ * @example
33
+ *
34
+ * wordArray.clamp();
35
+ */
36
+ clamp(): void;
37
+ /**
38
+ * Creates a copy of this word array.
39
+ *
40
+ * @return The clone.
41
+ *
42
+ * @example
43
+ *
44
+ * let clone = wordArray.clone();
45
+ */
46
+ clone(): WordArray;
47
+ /**
48
+ * Concatenates a word array to this word array.
49
+ *
50
+ * @param wordArray The word array to append.
51
+ *
52
+ * @return This word array.
53
+ *
54
+ * @example
55
+ *
56
+ * wordArray1.concat(wordArray2);
57
+ */
58
+ concat(wordArray: WordArray): WordArray;
59
+ /**
60
+ * Converts this word array to a string.
61
+ *
62
+ * @param encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
63
+ *
64
+ * @return The stringified word array.
65
+ *
66
+ * @example
67
+ *
68
+ * let string = wordArray + '';
69
+ * let string = wordArray.toString();
70
+ * let string = wordArray.toString(CryptoJS.enc.Utf8);
71
+ */
72
+ toString<T extends {
73
+ stringify: (arg: WordArray) => string;
74
+ }>(encoder?: T): string;
75
+ }
@@ -0,0 +1,123 @@
1
+ import { Hex } from "../enc/hex.class.js";
2
+ class WordArray {
3
+ /**
4
+ * Initializes a newly created word array.
5
+ *
6
+ * @param words (Optional) An array of 32-bit words.
7
+ * @param sigBytes (Optional) The number of significant bytes in the words.
8
+ *
9
+ * @example
10
+ *
11
+ * let wordArray = new WordArray();
12
+ * let wordArray = new WordArray([0x00010203, 0x04050607]);
13
+ * let wordArray = new WordArray([0x00010203, 0x04050607], 6);
14
+ */
15
+ constructor(words, sigBytes) {
16
+ this.words = words || [];
17
+ if (sigBytes !== void 0) {
18
+ this.sigBytes = sigBytes;
19
+ } else {
20
+ this.sigBytes = this.words.length * 4;
21
+ }
22
+ }
23
+ /**
24
+ * Creates a word array filled with random bytes.
25
+ *
26
+ * @param nBytes The number of random bytes to generate.
27
+ *
28
+ * @return The random word array.
29
+ *
30
+ * @example
31
+ *
32
+ * let wordArray = WordArray.random(16);
33
+ */
34
+ static random(nBytes) {
35
+ const words = [];
36
+ const r = function(mw) {
37
+ let mz = 987654321;
38
+ const mask = 4294967295;
39
+ return function() {
40
+ mz = 36969 * (mz & 65535) + (mz >> 16) & mask;
41
+ mw = 18e3 * (mw & 65535) + (mw >> 16) & mask;
42
+ let result = (mz << 16) + mw & mask;
43
+ result /= 4294967296;
44
+ result += 0.5;
45
+ return result * (Math.random() > 0.5 ? 1 : -1);
46
+ };
47
+ };
48
+ for (let i = 0, rcache; i < nBytes; i += 4) {
49
+ const rnd1 = r((rcache || Math.random()) * 4294967296);
50
+ rcache = rnd1() * 987654071;
51
+ words.push(rnd1() * 4294967296 | 0);
52
+ }
53
+ return new WordArray(words, nBytes);
54
+ }
55
+ /**
56
+ * Removes insignificant bits.
57
+ *
58
+ * @example
59
+ *
60
+ * wordArray.clamp();
61
+ */
62
+ clamp() {
63
+ this.words[this.sigBytes >>> 2] &= 4294967295 << 32 - this.sigBytes % 4 * 8;
64
+ this.words.length = Math.ceil(this.sigBytes / 4);
65
+ }
66
+ /**
67
+ * Creates a copy of this word array.
68
+ *
69
+ * @return The clone.
70
+ *
71
+ * @example
72
+ *
73
+ * let clone = wordArray.clone();
74
+ */
75
+ clone() {
76
+ return new WordArray(this.words.slice(0), this.sigBytes);
77
+ }
78
+ /**
79
+ * Concatenates a word array to this word array.
80
+ *
81
+ * @param wordArray The word array to append.
82
+ *
83
+ * @return This word array.
84
+ *
85
+ * @example
86
+ *
87
+ * wordArray1.concat(wordArray2);
88
+ */
89
+ concat(wordArray) {
90
+ this.clamp();
91
+ if (this.sigBytes % 4) {
92
+ for (let i = 0; i < wordArray.sigBytes; i++) {
93
+ const thatByte = wordArray.words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
94
+ this.words[this.sigBytes + i >>> 2] |= thatByte << 24 - (this.sigBytes + i) % 4 * 8;
95
+ }
96
+ } else {
97
+ for (let i = 0; i < wordArray.sigBytes; i += 4) {
98
+ this.words[this.sigBytes + i >>> 2] = wordArray.words[i >>> 2];
99
+ }
100
+ }
101
+ this.sigBytes += wordArray.sigBytes;
102
+ return this;
103
+ }
104
+ /**
105
+ * Converts this word array to a string.
106
+ *
107
+ * @param encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
108
+ *
109
+ * @return The stringified word array.
110
+ *
111
+ * @example
112
+ *
113
+ * let string = wordArray + '';
114
+ * let string = wordArray.toString();
115
+ * let string = wordArray.toString(CryptoJS.enc.Utf8);
116
+ */
117
+ toString(encoder) {
118
+ return (encoder || Hex).stringify(this);
119
+ }
120
+ }
121
+ export {
122
+ WordArray
123
+ };
@@ -0,0 +1,10 @@
1
+ import { X64Word } from "./x64-word.class.js";
2
+ import { WordArray } from "./word-array.class.js";
3
+ export declare class X64WordArray {
4
+ sigBytes: number;
5
+ words: X64Word[];
6
+ constructor(words?: X64Word[], sigBytes?: number);
7
+ static create(words?: X64Word[], sigBytes?: number): X64WordArray;
8
+ clone(): X64WordArray;
9
+ toX32(): WordArray;
10
+ }
@@ -0,0 +1,25 @@
1
+ import { X64Word } from "./x64-word.class.js";
2
+ import { WordArray } from "./word-array.class.js";
3
+ class X64WordArray {
4
+ constructor(words = [], sigBytes) {
5
+ this.words = words;
6
+ this.sigBytes = sigBytes !== void 0 ? sigBytes : words.length * 8;
7
+ }
8
+ static create(words = [], sigBytes) {
9
+ return new X64WordArray(words, sigBytes);
10
+ }
11
+ clone() {
12
+ const clonedWords = this.words.map((word) => new X64Word(word.high, word.low));
13
+ return new X64WordArray(clonedWords, this.sigBytes);
14
+ }
15
+ toX32() {
16
+ const x32Words = [];
17
+ for (const word of this.words) {
18
+ x32Words.push(word.high, word.low);
19
+ }
20
+ return new WordArray(x32Words, this.sigBytes);
21
+ }
22
+ }
23
+ export {
24
+ X64WordArray
25
+ };
@@ -0,0 +1,14 @@
1
+ export declare class X64Word {
2
+ high: number;
3
+ low: number;
4
+ constructor(high: number, low: number);
5
+ add(word: X64Word): X64Word;
6
+ and(word: X64Word): X64Word;
7
+ not(): X64Word;
8
+ or(word: X64Word): X64Word;
9
+ rotL(n: number): X64Word;
10
+ rotR(n: number): X64Word;
11
+ shiftL(n: number): X64Word;
12
+ shiftR(n: number): X64Word;
13
+ xor(word: X64Word): X64Word;
14
+ }
@@ -0,0 +1,47 @@
1
+ class X64Word {
2
+ constructor(high, low) {
3
+ this.high = high;
4
+ this.low = low;
5
+ }
6
+ add(word) {
7
+ const low = this.low + word.low | 0;
8
+ const carry = low >>> 0 < this.low >>> 0 ? 1 : 0;
9
+ const high = this.high + word.high + carry | 0;
10
+ return new X64Word(high, low);
11
+ }
12
+ and(word) {
13
+ return new X64Word(this.high & word.high, this.low & word.low);
14
+ }
15
+ not() {
16
+ return new X64Word(~this.high, ~this.low);
17
+ }
18
+ or(word) {
19
+ return new X64Word(this.high | word.high, this.low | word.low);
20
+ }
21
+ rotL(n) {
22
+ return this.shiftL(n).or(this.shiftR(64 - n));
23
+ }
24
+ rotR(n) {
25
+ return this.shiftR(n).or(this.shiftL(64 - n));
26
+ }
27
+ shiftL(n) {
28
+ if (n < 32) {
29
+ return new X64Word(this.high << n | this.low >>> 32 - n, this.low << n);
30
+ } else {
31
+ return new X64Word(this.low << n - 32, 0);
32
+ }
33
+ }
34
+ shiftR(n) {
35
+ if (n < 32) {
36
+ return new X64Word(this.high >>> n, this.low >>> n | this.high << 32 - n);
37
+ } else {
38
+ return new X64Word(0, this.high >>> n - 32);
39
+ }
40
+ }
41
+ xor(word) {
42
+ return new X64Word(this.high ^ word.high, this.low ^ word.low);
43
+ }
44
+ }
45
+ export {
46
+ X64Word
47
+ };
@@ -0,0 +1,20 @@
1
+ import { BlockCipher } from "../lib/block-cipher.class.js";
2
+ import { BlockCipherMode } from "./block-cipher-mode.class.js";
3
+ export declare abstract class BlockCipherModeAlgorithm {
4
+ __creator: ((cipher: BlockCipher, iv: number[]) => BlockCipherMode) | undefined;
5
+ protected _cipher: BlockCipher;
6
+ protected _iv: number[] | undefined;
7
+ constructor(cipher: BlockCipher, iv: number[]);
8
+ abstract processBlock(words: number[], offset: number): void;
9
+ /**
10
+ * Initializes a newly created mode.
11
+ *
12
+ * @param cipher A block cipher instance.
13
+ * @param iv The IV words.
14
+ *
15
+ * @example
16
+ *
17
+ * var mode = CBC.Encryptor.create(cipher, iv.words);
18
+ */
19
+ init(cipher: BlockCipher, iv?: number[]): void;
20
+ }
@@ -0,0 +1,22 @@
1
+ class BlockCipherModeAlgorithm {
2
+ constructor(cipher, iv) {
3
+ this.init(cipher, iv);
4
+ }
5
+ /**
6
+ * Initializes a newly created mode.
7
+ *
8
+ * @param cipher A block cipher instance.
9
+ * @param iv The IV words.
10
+ *
11
+ * @example
12
+ *
13
+ * var mode = CBC.Encryptor.create(cipher, iv.words);
14
+ */
15
+ init(cipher, iv) {
16
+ this._cipher = cipher;
17
+ this._iv = iv;
18
+ }
19
+ }
20
+ export {
21
+ BlockCipherModeAlgorithm
22
+ };
@@ -0,0 +1,28 @@
1
+ import { BlockCipher } from "../lib/block-cipher.class.js";
2
+ import { BlockCipherModeAlgorithm } from "./block-cipher-mode-algorithm.class.js";
3
+ export declare abstract class BlockCipherMode {
4
+ static Decryptor: any;
5
+ static Encryptor: any;
6
+ /**
7
+ * Creates this mode for decryption.
8
+ *
9
+ * @param cipher A block cipher instance.
10
+ * @param iv The IV words.
11
+ *
12
+ * @example
13
+ *
14
+ * const mode = CBC.createDecryptor(cipher, iv.words);
15
+ */
16
+ static createDecryptor(cipher: BlockCipher, iv: number[]): BlockCipherModeAlgorithm;
17
+ /**
18
+ * Creates this mode for encryption.
19
+ *
20
+ * @param cipher A block cipher instance.
21
+ * @param iv The IV words.
22
+ *
23
+ * @example
24
+ *
25
+ * var mode = CBC.createEncryptor(cipher, iv.words);
26
+ */
27
+ static createEncryptor(cipher: BlockCipher, iv: number[]): BlockCipherModeAlgorithm;
28
+ }
@@ -0,0 +1,36 @@
1
+ import { BlockCipherModeAlgorithm } from "./block-cipher-mode-algorithm.class.js";
2
+ class BlockCipherMode {
3
+ /**
4
+ * Creates this mode for decryption.
5
+ *
6
+ * @param cipher A block cipher instance.
7
+ * @param iv The IV words.
8
+ *
9
+ * @example
10
+ *
11
+ * const mode = CBC.createDecryptor(cipher, iv.words);
12
+ */
13
+ static createDecryptor(cipher, iv) {
14
+ const decryptorClass = this.Decryptor;
15
+ return new decryptorClass(cipher, iv);
16
+ }
17
+ /**
18
+ * Creates this mode for encryption.
19
+ *
20
+ * @param cipher A block cipher instance.
21
+ * @param iv The IV words.
22
+ *
23
+ * @example
24
+ *
25
+ * var mode = CBC.createEncryptor(cipher, iv.words);
26
+ */
27
+ static createEncryptor(cipher, iv) {
28
+ const encryptorClass = this.Encryptor;
29
+ return new encryptorClass(cipher, iv);
30
+ }
31
+ }
32
+ BlockCipherMode.Decryptor = BlockCipherModeAlgorithm;
33
+ BlockCipherMode.Encryptor = BlockCipherModeAlgorithm;
34
+ export {
35
+ BlockCipherMode
36
+ };
@@ -0,0 +1,17 @@
1
+ import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
2
+ export declare class CBCDecryptor extends BlockCipherModeAlgorithm {
3
+ get prevBlock(): number[];
4
+ protected _prevBlock: number[] | undefined;
5
+ /**
6
+ * Processes the data block at offset.
7
+ *
8
+ * @param words The data words to operate on.
9
+ * @param offset The offset where the block starts.
10
+ *
11
+ * @example
12
+ *
13
+ * mode.processBlock(data.words, offset);
14
+ */
15
+ processBlock(words: number[], offset: number): void;
16
+ xorBlock(words: number[], offset: number, blockSize: number): void;
17
+ }
@@ -0,0 +1,40 @@
1
+ import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
2
+ class CBCDecryptor extends BlockCipherModeAlgorithm {
3
+ get prevBlock() {
4
+ return this._prevBlock;
5
+ }
6
+ /**
7
+ * Processes the data block at offset.
8
+ *
9
+ * @param words The data words to operate on.
10
+ * @param offset The offset where the block starts.
11
+ *
12
+ * @example
13
+ *
14
+ * mode.processBlock(data.words, offset);
15
+ */
16
+ processBlock(words, offset) {
17
+ if (this._cipher.cfg.blockSize === void 0) {
18
+ throw new Error("missing blockSize in cipher config");
19
+ }
20
+ const thisBlock = words.slice(offset, offset + this._cipher.cfg.blockSize);
21
+ this._cipher.decryptBlock(words, offset);
22
+ this.xorBlock(words, offset, this._cipher.cfg.blockSize);
23
+ this._prevBlock = thisBlock;
24
+ }
25
+ xorBlock(words, offset, blockSize) {
26
+ let block;
27
+ if (this._iv) {
28
+ block = this._iv;
29
+ this._iv = void 0;
30
+ } else {
31
+ block = this._prevBlock;
32
+ }
33
+ for (let i = 0; i < blockSize; i++) {
34
+ words[offset + i] ^= block[i];
35
+ }
36
+ }
37
+ }
38
+ export {
39
+ CBCDecryptor
40
+ };
@@ -0,0 +1,17 @@
1
+ import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
2
+ export declare class CBCEncryptor extends BlockCipherModeAlgorithm {
3
+ get prevBlock(): number[];
4
+ protected _prevBlock: number[] | undefined;
5
+ /**
6
+ * Processes the data block at offset.
7
+ *
8
+ * @param words The data words to operate on.
9
+ * @param offset The offset where the block starts.
10
+ *
11
+ * @example
12
+ *
13
+ * mode.processBlock(data.words, offset);
14
+ */
15
+ processBlock(words: number[], offset: number): void;
16
+ xorBlock(words: number[], offset: number, blockSize: number): void;
17
+ }
@@ -0,0 +1,39 @@
1
+ import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
2
+ class CBCEncryptor extends BlockCipherModeAlgorithm {
3
+ get prevBlock() {
4
+ return this._prevBlock;
5
+ }
6
+ /**
7
+ * Processes the data block at offset.
8
+ *
9
+ * @param words The data words to operate on.
10
+ * @param offset The offset where the block starts.
11
+ *
12
+ * @example
13
+ *
14
+ * mode.processBlock(data.words, offset);
15
+ */
16
+ processBlock(words, offset) {
17
+ if (this._cipher.cfg.blockSize === void 0) {
18
+ throw new Error("missing blockSize in cipher config");
19
+ }
20
+ this.xorBlock(words, offset, this._cipher.cfg.blockSize);
21
+ this._cipher.encryptBlock(words, offset);
22
+ this._prevBlock = words.slice(offset, offset + this._cipher.cfg.blockSize);
23
+ }
24
+ xorBlock(words, offset, blockSize) {
25
+ let block;
26
+ if (this._iv) {
27
+ block = this._iv;
28
+ this._iv = void 0;
29
+ } else {
30
+ block = this._prevBlock;
31
+ }
32
+ for (let i = 0; i < blockSize; i++) {
33
+ words[offset + i] ^= block[i];
34
+ }
35
+ }
36
+ }
37
+ export {
38
+ CBCEncryptor
39
+ };
@@ -0,0 +1,10 @@
1
+ import { BlockCipherMode } from "../block-cipher-mode.class.js";
2
+ import { CBCEncryptor } from "./cbc-encryptor.class.js";
3
+ import { CBCDecryptor } from "./cbc-decryptor.class.js";
4
+ /**
5
+ * Cipher Block Chaining mode.
6
+ */
7
+ export declare abstract class CBC extends BlockCipherMode {
8
+ static Decryptor: typeof CBCDecryptor;
9
+ static Encryptor: typeof CBCEncryptor;
10
+ }
@@ -0,0 +1,10 @@
1
+ import { BlockCipherMode } from "../block-cipher-mode.class.js";
2
+ import { CBCEncryptor } from "./cbc-encryptor.class.js";
3
+ import { CBCDecryptor } from "./cbc-decryptor.class.js";
4
+ class CBC extends BlockCipherMode {
5
+ }
6
+ CBC.Decryptor = CBCDecryptor;
7
+ CBC.Encryptor = CBCEncryptor;
8
+ export {
9
+ CBC
10
+ };
@@ -0,0 +1,3 @@
1
+ import { CTREncryptor } from "./ctr-encryptor.class.js";
2
+ export declare class CTRDecryptor extends CTREncryptor {
3
+ }
@@ -0,0 +1,6 @@
1
+ import { CTREncryptor } from "./ctr-encryptor.class.js";
2
+ class CTRDecryptor extends CTREncryptor {
3
+ }
4
+ export {
5
+ CTRDecryptor
6
+ };
@@ -0,0 +1,16 @@
1
+ import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
2
+ export declare class CTREncryptor extends BlockCipherModeAlgorithm {
3
+ protected _counter: number[];
4
+ protected _prevBlock: number[] | undefined;
5
+ /**
6
+ * Processes the data block at offset.
7
+ *
8
+ * @param words The data words to operate on.
9
+ * @param offset The offset where the block starts.
10
+ *
11
+ * @example
12
+ *
13
+ * mode.processBlock(data.words, offset);
14
+ */
15
+ processBlock(words: number[], offset: number): void;
16
+ }