@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,112 @@
1
+ import { BufferedBlockAlgorithm } from "./buffered-block-algorithm.class.js";
2
+ import { WordArray } from "./word-array.class.js";
3
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
4
+ import { CipherHelper } from "./cipher-helper.class.js";
5
+ export declare abstract class Cipher extends BufferedBlockAlgorithm {
6
+ /**
7
+ * This cipher's IV size. Default: 4 (128 bits / 32 Bits)
8
+ */
9
+ static ivSize: number;
10
+ /**
11
+ * This cipher's key size. Default: 4 (128 bits / 32 Bits)
12
+ */
13
+ static keySize: number;
14
+ /**
15
+ * A constant representing decryption mode.
16
+ */
17
+ protected static readonly _DEC_XFORM_MODE = 2;
18
+ /**
19
+ * A constant representing encryption mode.
20
+ */
21
+ protected static readonly _ENC_XFORM_MODE = 1;
22
+ /**
23
+ * The key.
24
+ */
25
+ protected _key: WordArray;
26
+ /**
27
+ * Either the encryption or decryption transformation mode constant.
28
+ */
29
+ protected _xformMode: number;
30
+ /**
31
+ * Initializes a newly created cipher.
32
+ *
33
+ * @param xformMode Either the encryption or decryption transormation mode constant.
34
+ * @param key The key.
35
+ * @param cfg (Optional) The configuration options to use for this operation.
36
+ *
37
+ * @example
38
+ *
39
+ * let cipher = AES.create(AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray });
40
+ */
41
+ constructor(xformMode: number, key: WordArray, cfg?: BufferedBlockAlgorithmConfig);
42
+ /**
43
+ * Creates shortcut functions to a cipher's object interface.
44
+ *
45
+ * @param cipher The cipher to create a helper for.
46
+ *
47
+ * @return An object with encrypt and decrypt shortcut functions.
48
+ *
49
+ * @example
50
+ *
51
+ * const AES = Cipher._createHelper(AESAlgorithm);
52
+ */
53
+ static _createHelper(cipher: typeof Cipher): CipherHelper;
54
+ /**
55
+ * Creates this cipher in decryption mode.
56
+ *
57
+ * @param key The key.
58
+ * @param cfg (Optional) The configuration options to use for this operation.
59
+ *
60
+ * @return A cipher instance.
61
+ *
62
+ * @example
63
+ *
64
+ * let cipher = AES.createDecryptor(keyWordArray, { iv: ivWordArray });
65
+ */
66
+ static createDecryptor(key: WordArray | string, cfg?: BufferedBlockAlgorithmConfig): Cipher;
67
+ /**
68
+ * Creates this cipher in encryption mode.
69
+ *
70
+ * @param key The key.
71
+ * @param cfg (Optional) The configuration options to use for this operation.
72
+ *
73
+ * @return A cipher instance.
74
+ *
75
+ * @example
76
+ *
77
+ * let cipher = AES.createEncryptor(keyWordArray, { iv: ivWordArray });
78
+ */
79
+ static createEncryptor(key: WordArray | string, cfg?: BufferedBlockAlgorithmConfig): Cipher;
80
+ /**
81
+ * Finalizes the encryption or decryption process.
82
+ * Note that the finalize operation is effectively a destructive, read-once operation.
83
+ *
84
+ * @param dataUpdate The final data to encrypt or decrypt.
85
+ *
86
+ * @return The data after final processing.
87
+ *
88
+ * @example
89
+ *
90
+ * var encrypted = cipher.finalize();
91
+ * var encrypted = cipher.finalize('data');
92
+ * var encrypted = cipher.finalize(wordArray);
93
+ */
94
+ finalize(dataUpdate?: WordArray | string): WordArray;
95
+ /**
96
+ * Adds data to be encrypted or decrypted.
97
+ *
98
+ * @param dataUpdate The data to encrypt or decrypt.
99
+ *
100
+ * @return The data after processing.
101
+ *
102
+ * @example
103
+ *
104
+ * let encrypted = cipher.process('data');
105
+ * let encrypted = cipher.process(wordArray);
106
+ */
107
+ process(dataUpdate: WordArray | string): WordArray;
108
+ /**
109
+ * Cipher specific finalize function explicitly implemented in the derived class.
110
+ */
111
+ protected abstract _doFinalize(): WordArray;
112
+ }
@@ -0,0 +1,112 @@
1
+ import { BufferedBlockAlgorithm } from "./buffered-block-algorithm.class.js";
2
+ import { CipherHelper } from "./cipher-helper.class.js";
3
+ class Cipher extends BufferedBlockAlgorithm {
4
+ /**
5
+ * Initializes a newly created cipher.
6
+ *
7
+ * @param xformMode Either the encryption or decryption transormation mode constant.
8
+ * @param key The key.
9
+ * @param cfg (Optional) The configuration options to use for this operation.
10
+ *
11
+ * @example
12
+ *
13
+ * let cipher = AES.create(AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray });
14
+ */
15
+ constructor(xformMode, key, cfg) {
16
+ super(Object.assign({
17
+ blockSize: 1
18
+ }, cfg));
19
+ this._xformMode = xformMode;
20
+ this._key = key;
21
+ this.reset();
22
+ }
23
+ /**
24
+ * Creates shortcut functions to a cipher's object interface.
25
+ *
26
+ * @param cipher The cipher to create a helper for.
27
+ *
28
+ * @return An object with encrypt and decrypt shortcut functions.
29
+ *
30
+ * @example
31
+ *
32
+ * const AES = Cipher._createHelper(AESAlgorithm);
33
+ */
34
+ // eslint-disable-next-line @typescript-eslint/naming-convention
35
+ static _createHelper(cipher) {
36
+ return new CipherHelper(cipher);
37
+ }
38
+ /**
39
+ * Creates this cipher in decryption mode.
40
+ *
41
+ * @param key The key.
42
+ * @param cfg (Optional) The configuration options to use for this operation.
43
+ *
44
+ * @return A cipher instance.
45
+ *
46
+ * @example
47
+ *
48
+ * let cipher = AES.createDecryptor(keyWordArray, { iv: ivWordArray });
49
+ */
50
+ static createDecryptor(key, cfg) {
51
+ const thisClass = this;
52
+ return new thisClass(this._DEC_XFORM_MODE, key, cfg);
53
+ }
54
+ /**
55
+ * Creates this cipher in encryption mode.
56
+ *
57
+ * @param key The key.
58
+ * @param cfg (Optional) The configuration options to use for this operation.
59
+ *
60
+ * @return A cipher instance.
61
+ *
62
+ * @example
63
+ *
64
+ * let cipher = AES.createEncryptor(keyWordArray, { iv: ivWordArray });
65
+ */
66
+ static createEncryptor(key, cfg) {
67
+ return new this(this._ENC_XFORM_MODE, key, cfg);
68
+ }
69
+ /**
70
+ * Finalizes the encryption or decryption process.
71
+ * Note that the finalize operation is effectively a destructive, read-once operation.
72
+ *
73
+ * @param dataUpdate The final data to encrypt or decrypt.
74
+ *
75
+ * @return The data after final processing.
76
+ *
77
+ * @example
78
+ *
79
+ * var encrypted = cipher.finalize();
80
+ * var encrypted = cipher.finalize('data');
81
+ * var encrypted = cipher.finalize(wordArray);
82
+ */
83
+ finalize(dataUpdate) {
84
+ if (dataUpdate) {
85
+ this._append(dataUpdate);
86
+ }
87
+ return this._doFinalize();
88
+ }
89
+ /**
90
+ * Adds data to be encrypted or decrypted.
91
+ *
92
+ * @param dataUpdate The data to encrypt or decrypt.
93
+ *
94
+ * @return The data after processing.
95
+ *
96
+ * @example
97
+ *
98
+ * let encrypted = cipher.process('data');
99
+ * let encrypted = cipher.process(wordArray);
100
+ */
101
+ process(dataUpdate) {
102
+ this._append(dataUpdate);
103
+ return this._process();
104
+ }
105
+ }
106
+ Cipher.ivSize = 4;
107
+ Cipher.keySize = 4;
108
+ Cipher._DEC_XFORM_MODE = 2;
109
+ Cipher._ENC_XFORM_MODE = 1;
110
+ export {
111
+ Cipher
112
+ };
@@ -0,0 +1,3 @@
1
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
2
+ import { Hasher } from "./hasher.class.js";
3
+ export type HasherConstructor<T extends Hasher = Hasher> = new (cfg?: BufferedBlockAlgorithmConfig) => T;
File without changes
@@ -0,0 +1,3 @@
1
+ import { WordArray } from "./word-array.class.js";
2
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
3
+ export type HasherHelper = (message: WordArray | string, cfg?: BufferedBlockAlgorithmConfig) => WordArray;
File without changes
@@ -0,0 +1,58 @@
1
+ import { BufferedBlockAlgorithm } from "./buffered-block-algorithm.class.js";
2
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
3
+ import { WordArray } from "./word-array.class.js";
4
+ import { HasherConstructor } from "./hasher-constructor.type.js";
5
+ import { HasherHelper } from "./hasher-helper.type.js";
6
+ export declare abstract class Hasher extends BufferedBlockAlgorithm {
7
+ /**
8
+ * Initializes a newly created hasher.
9
+ *
10
+ * @param cfg (Optional) The configuration options to use for this hash computation.
11
+ *
12
+ * @example
13
+ *
14
+ * let hasher = CryptoJS.algo.SHA256.create();
15
+ */
16
+ constructor(cfg?: BufferedBlockAlgorithmConfig);
17
+ /**
18
+ * Creates a shortcut function to the object interface of a hasher.
19
+ *
20
+ * @param hasher The hasher to create a helper for.
21
+ *
22
+ * @return The shortcut function.
23
+ *
24
+ * @example
25
+ *
26
+ * const SHA256 = Hasher._createHelper(SHA256);
27
+ */
28
+ static _createHelper<T extends Hasher = Hasher>(hasher: HasherConstructor<T>): HasherHelper;
29
+ protected abstract _doFinalize(): WordArray;
30
+ /**
31
+ * Finalizes the hash computation.
32
+ * Note that the finalize operation is effectively a destructive, read-once operation.
33
+ *
34
+ * @param messageUpdate (Optional) A final message update.
35
+ *
36
+ * @return The hash.
37
+ *
38
+ * @example
39
+ *
40
+ * let hash = hasher.finalize();
41
+ * let hash = hasher.finalize('message');
42
+ * let hash = hasher.finalize(wordArray);
43
+ */
44
+ finalize(messageUpdate?: WordArray | string): WordArray;
45
+ /**
46
+ * Updates this hasher with a message.
47
+ *
48
+ * @param messageUpdate The message to append.
49
+ *
50
+ * @return This hasher.
51
+ *
52
+ * @example
53
+ *
54
+ * hasher.update('message');
55
+ * hasher.update(wordArray);
56
+ */
57
+ update(messageUpdate: WordArray | string): Hasher;
58
+ }
@@ -0,0 +1,77 @@
1
+ import { BufferedBlockAlgorithm } from "./buffered-block-algorithm.class.js";
2
+ class Hasher extends BufferedBlockAlgorithm {
3
+ /**
4
+ * Initializes a newly created hasher.
5
+ *
6
+ * @param cfg (Optional) The configuration options to use for this hash computation.
7
+ *
8
+ * @example
9
+ *
10
+ * let hasher = CryptoJS.algo.SHA256.create();
11
+ */
12
+ constructor(cfg) {
13
+ super(Object.assign({
14
+ blockSize: 512 / 32
15
+ }, cfg));
16
+ this.reset();
17
+ }
18
+ /**
19
+ * Creates a shortcut function to the object interface of a hasher.
20
+ *
21
+ * @param hasher The hasher to create a helper for.
22
+ *
23
+ * @return The shortcut function.
24
+ *
25
+ * @example
26
+ *
27
+ * const SHA256 = Hasher._createHelper(SHA256);
28
+ */
29
+ // eslint-disable-next-line @typescript-eslint/naming-convention
30
+ static _createHelper(hasher) {
31
+ function helper(message, cfg) {
32
+ const hasherInstance = new hasher(cfg);
33
+ return hasherInstance.finalize(message);
34
+ }
35
+ return helper;
36
+ }
37
+ /**
38
+ * Finalizes the hash computation.
39
+ * Note that the finalize operation is effectively a destructive, read-once operation.
40
+ *
41
+ * @param messageUpdate (Optional) A final message update.
42
+ *
43
+ * @return The hash.
44
+ *
45
+ * @example
46
+ *
47
+ * let hash = hasher.finalize();
48
+ * let hash = hasher.finalize('message');
49
+ * let hash = hasher.finalize(wordArray);
50
+ */
51
+ finalize(messageUpdate) {
52
+ if (messageUpdate) {
53
+ this._append(messageUpdate);
54
+ }
55
+ return this._doFinalize();
56
+ }
57
+ /**
58
+ * Updates this hasher with a message.
59
+ *
60
+ * @param messageUpdate The message to append.
61
+ *
62
+ * @return This hasher.
63
+ *
64
+ * @example
65
+ *
66
+ * hasher.update('message');
67
+ * hasher.update(wordArray);
68
+ */
69
+ update(messageUpdate) {
70
+ this._append(messageUpdate);
71
+ this._process();
72
+ return this;
73
+ }
74
+ }
75
+ export {
76
+ Hasher
77
+ };
@@ -0,0 +1,11 @@
1
+ import { HasherConstructor } from "./hasher-constructor.type.js";
2
+ import { WordArray } from "./word-array.class.js";
3
+ export declare class HMAC {
4
+ private _hasher;
5
+ private _iKey;
6
+ private _oKey;
7
+ constructor(hasher: HasherConstructor, rawKey: WordArray | string);
8
+ finalize(messageUpdate?: WordArray | string): WordArray;
9
+ reset(): void;
10
+ update(messageUpdate: WordArray | string): this;
11
+ }
@@ -0,0 +1,39 @@
1
+ import { Utf8 } from "../enc/utf8.class.js";
2
+ class HMAC {
3
+ constructor(hasher, rawKey) {
4
+ this._hasher = new hasher();
5
+ let key = typeof rawKey == "string" ? Utf8.parse(rawKey) : rawKey;
6
+ const hasherBlockSize = this._hasher.cfg.blockSize;
7
+ const hasherBlockSizeBytes = hasherBlockSize * 4;
8
+ if (key.sigBytes > hasherBlockSizeBytes) {
9
+ key = this._hasher.finalize(key);
10
+ }
11
+ key.clamp();
12
+ const oKey = this._oKey = key.clone();
13
+ const iKey = this._iKey = key.clone();
14
+ const oKeyWords = oKey.words;
15
+ const iKeyWords = iKey.words;
16
+ for (let i = 0; i < hasherBlockSize; i++) {
17
+ oKeyWords[i] ^= 1549556828;
18
+ iKeyWords[i] ^= 909522486;
19
+ }
20
+ oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
21
+ this.reset();
22
+ }
23
+ finalize(messageUpdate) {
24
+ const innerHash = this._hasher.finalize(messageUpdate);
25
+ this._hasher.reset();
26
+ return this._hasher.finalize(this._oKey.clone().concat(innerHash));
27
+ }
28
+ reset() {
29
+ this._hasher.reset();
30
+ this._hasher.update(this._iKey);
31
+ }
32
+ update(messageUpdate) {
33
+ this._hasher.update(messageUpdate);
34
+ return this;
35
+ }
36
+ }
37
+ export {
38
+ HMAC
39
+ };
@@ -0,0 +1,8 @@
1
+ export { BlockCipher } from "./block-cipher.class.js";
2
+ export { CipherParams } from "./cipher-params.class.js";
3
+ export { Hasher } from "./hasher.class.js";
4
+ export { PasswordBasedCipher } from "./password-based-cipher.class.js";
5
+ export { SerializableCipher } from "./serializable-cipher.class.js";
6
+ export { WordArray } from "./word-array.class.js";
7
+ export { X64Word } from "./x64-word.class.js";
8
+ export { X64WordArray } from "./x64-word-array.class.js";
@@ -0,0 +1,18 @@
1
+ import { BlockCipher } from "./block-cipher.class.js";
2
+ import { CipherParams } from "./cipher-params.class.js";
3
+ import { Hasher } from "./hasher.class.js";
4
+ import { PasswordBasedCipher } from "./password-based-cipher.class.js";
5
+ import { SerializableCipher } from "./serializable-cipher.class.js";
6
+ import { WordArray } from "./word-array.class.js";
7
+ import { X64Word } from "./x64-word.class.js";
8
+ import { X64WordArray } from "./x64-word-array.class.js";
9
+ export {
10
+ BlockCipher,
11
+ CipherParams,
12
+ Hasher,
13
+ PasswordBasedCipher,
14
+ SerializableCipher,
15
+ WordArray,
16
+ X64Word,
17
+ X64WordArray
18
+ };
@@ -0,0 +1,54 @@
1
+ import { WordArray } from "./word-array.class.js";
2
+ import { Cipher } from "./cipher.class.js";
3
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
4
+ import { CipherParams } from "./cipher-params.class.js";
5
+ import { Formatter } from "../format/formatter.interface.js";
6
+ export declare class PasswordBasedCipher {
7
+ static cfg: BufferedBlockAlgorithmConfig;
8
+ /**
9
+ * Decrypts serialized ciphertext using a password.
10
+ *
11
+ * @param cipher The cipher algorithm to use.
12
+ * @param ciphertext The ciphertext to decrypt.
13
+ * @param password The password.
14
+ * @param cfg (Optional) The configuration options to use for this operation.
15
+ *
16
+ * @return The plaintext.
17
+ *
18
+ * @example
19
+ *
20
+ * var plaintext = PasswordBasedCipher.decrypt(AES, formattedCiphertext, 'password', { format: OpenSSL });
21
+ * var plaintext = PasswordBasedCipher.decrypt(AES, ciphertextParams, 'password', { format: OpenSSL });
22
+ */
23
+ static decrypt(cipher: typeof Cipher, ciphertext: CipherParams | string, password: string, cfg?: BufferedBlockAlgorithmConfig): WordArray;
24
+ /**
25
+ * Encrypts a message using a password.
26
+ *
27
+ * @param cipher The cipher algorithm to use.
28
+ * @param message The message to encrypt.
29
+ * @param password The password.
30
+ * @param cfg (Optional) The configuration options to use for this operation.
31
+ *
32
+ * @return A cipher params object.
33
+ *
34
+ * @example
35
+ *
36
+ * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(AES, message, 'password');
37
+ * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(AES, message, 'password', { format: OpenSSL });
38
+ */
39
+ static encrypt(cipher: typeof Cipher, message: WordArray | string, password: string, cfg?: BufferedBlockAlgorithmConfig): CipherParams;
40
+ /**
41
+ * Converts serialized ciphertext to CipherParams,
42
+ * else assumed CipherParams already and returns ciphertext unchanged.
43
+ *
44
+ * @param ciphertext The ciphertext.
45
+ * @param format The formatting strategy to use to parse serialized ciphertext.
46
+ *
47
+ * @return The unserialized ciphertext.
48
+ *
49
+ * @example
50
+ *
51
+ * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
52
+ */
53
+ protected static _parse(ciphertext: CipherParams | string, format: Formatter): CipherParams;
54
+ }
@@ -0,0 +1,92 @@
1
+ import { SerializableCipher } from "./serializable-cipher.class.js";
2
+ import { WordArray } from "./word-array.class.js";
3
+ import { OpenSSL } from "../format/open-ssl.class.js";
4
+ import { OpenSSLKdf } from "../kdf/open-ssl-kdf.class.js";
5
+ class PasswordBasedCipher {
6
+ /**
7
+ * Decrypts serialized ciphertext using a password.
8
+ *
9
+ * @param cipher The cipher algorithm to use.
10
+ * @param ciphertext The ciphertext to decrypt.
11
+ * @param password The password.
12
+ * @param cfg (Optional) The configuration options to use for this operation.
13
+ *
14
+ * @return The plaintext.
15
+ *
16
+ * @example
17
+ *
18
+ * var plaintext = PasswordBasedCipher.decrypt(AES, formattedCiphertext, 'password', { format: OpenSSL });
19
+ * var plaintext = PasswordBasedCipher.decrypt(AES, ciphertextParams, 'password', { format: OpenSSL });
20
+ */
21
+ static decrypt(cipher, ciphertext, password, cfg) {
22
+ const config = Object.assign({}, this.cfg, cfg);
23
+ if (config.format === void 0) {
24
+ throw new Error("missing format in config");
25
+ }
26
+ ciphertext = this._parse(ciphertext, config.format);
27
+ if (config.kdf === void 0) {
28
+ throw new Error("the key derivation function must be set");
29
+ }
30
+ const derivedParams = config.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
31
+ if (derivedParams.iv !== void 0) {
32
+ config.iv = derivedParams.iv;
33
+ }
34
+ return SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, config);
35
+ }
36
+ /**
37
+ * Encrypts a message using a password.
38
+ *
39
+ * @param cipher The cipher algorithm to use.
40
+ * @param message The message to encrypt.
41
+ * @param password The password.
42
+ * @param cfg (Optional) The configuration options to use for this operation.
43
+ *
44
+ * @return A cipher params object.
45
+ *
46
+ * @example
47
+ *
48
+ * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(AES, message, 'password');
49
+ * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(AES, message, 'password', { format: OpenSSL });
50
+ */
51
+ static encrypt(cipher, message, password, cfg) {
52
+ const config = Object.assign({}, this.cfg, cfg);
53
+ if (config.kdf === void 0) {
54
+ throw new Error("missing kdf in config");
55
+ }
56
+ const derivedParams = config.kdf.execute(password, cipher.keySize, cipher.ivSize);
57
+ if (derivedParams.iv !== void 0) {
58
+ config.iv = derivedParams.iv;
59
+ }
60
+ const ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, config);
61
+ return ciphertext.extend(derivedParams);
62
+ }
63
+ /**
64
+ * Converts serialized ciphertext to CipherParams,
65
+ * else assumed CipherParams already and returns ciphertext unchanged.
66
+ *
67
+ * @param ciphertext The ciphertext.
68
+ * @param format The formatting strategy to use to parse serialized ciphertext.
69
+ *
70
+ * @return The unserialized ciphertext.
71
+ *
72
+ * @example
73
+ *
74
+ * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
75
+ */
76
+ static _parse(ciphertext, format) {
77
+ if (typeof ciphertext === "string") {
78
+ return format.parse(ciphertext);
79
+ } else {
80
+ return ciphertext;
81
+ }
82
+ }
83
+ }
84
+ PasswordBasedCipher.cfg = {
85
+ blockSize: 4,
86
+ iv: new WordArray([]),
87
+ format: OpenSSL,
88
+ kdf: OpenSSLKdf
89
+ };
90
+ export {
91
+ PasswordBasedCipher
92
+ };
@@ -0,0 +1,73 @@
1
+ import { WordArray } from "./word-array.class.js";
2
+ import { Cipher } from "./cipher.class.js";
3
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
4
+ import { CipherParams } from "./cipher-params.class.js";
5
+ import { Formatter } from "../format/formatter.interface.js";
6
+ export declare class SerializableCipher {
7
+ static cfg: BufferedBlockAlgorithmConfig;
8
+ /**
9
+ * Decrypts serialized ciphertext.
10
+ *
11
+ * @param cipher The cipher algorithm to use.
12
+ * @param ciphertext The ciphertext to decrypt.
13
+ * @param key The key.
14
+ * @param optionalCfg The configuration options to use for this operation.
15
+ *
16
+ * @return The plaintext.
17
+ *
18
+ * @example
19
+ *
20
+ * let plaintext = SerializableCipher.decrypt(
21
+ * AESAlgorithm,
22
+ * formattedCiphertext,
23
+ * key, {
24
+ * iv: iv,
25
+ * format: CryptoJS.format.OpenSSL
26
+ * }
27
+ * );
28
+ *
29
+ * let plaintext = SerializableCipher.decrypt(
30
+ * AESAlgorithm,
31
+ * ciphertextParams,
32
+ * key, {
33
+ * iv: iv,
34
+ * format: CryptoJS.format.OpenSSL
35
+ * }
36
+ * );
37
+ */
38
+ static decrypt(cipher: typeof Cipher, ciphertext: CipherParams | string, key: WordArray | string, optionalCfg?: BufferedBlockAlgorithmConfig): WordArray;
39
+ /**
40
+ * Encrypts a message.
41
+ *
42
+ * @param cipher The cipher algorithm to use.
43
+ * @param message The message to encrypt.
44
+ * @param key The key.
45
+ * @param cfg (Optional) The configuration options to use for this operation.
46
+ *
47
+ * @return A cipher params object.
48
+ *
49
+ * @example
50
+ *
51
+ * let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
52
+ * let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
53
+ * let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, {
54
+ * iv: iv,
55
+ * format: CryptoJS.format.OpenSSL
56
+ * });
57
+ */
58
+ static encrypt(cipher: typeof Cipher, message: WordArray | string, key: WordArray | string, cfg?: BufferedBlockAlgorithmConfig): CipherParams;
59
+ /**
60
+ * Converts serialized ciphertext to CipherParams,
61
+ * else assumed CipherParams already and returns ciphertext unchanged.
62
+ *
63
+ * @param ciphertext The ciphertext.
64
+ * @param format The formatting strategy to use to parse serialized ciphertext.
65
+ *
66
+ * @return The deserialized ciphertext.
67
+ *
68
+ * @example
69
+ *
70
+ * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
71
+ */
72
+ protected static _parse(ciphertext: CipherParams | string, format: Formatter): CipherParams;
73
+ }