@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,42 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ import { Hasher } from "../lib/hasher.class.js";
3
+ import { AbstractKDF } from "./kdf.model.js";
4
+ export interface OptionalEVPKDFConfig {
5
+ keySize?: number;
6
+ hasher?: typeof Hasher;
7
+ iterations?: number;
8
+ }
9
+ export interface EVPKDFConfig extends OptionalEVPKDFConfig {
10
+ keySize: number;
11
+ hasher: typeof Hasher;
12
+ iterations: number;
13
+ }
14
+ export declare class EVPKDF implements AbstractKDF {
15
+ cfg: EVPKDFConfig;
16
+ /**
17
+ * Initializes a newly created key derivation function.
18
+ *
19
+ * @param cfg (Optional) The configuration options to use for the derivation.
20
+ *
21
+ * @example
22
+ *
23
+ * let kdf = EvpKDF.create();
24
+ * let kdf = EvpKDF.create({ keySize: 8 });
25
+ * let kdf = EvpKDF.create({ keySize: 8, iterations: 1000 });
26
+ */
27
+ constructor(cfg?: OptionalEVPKDFConfig);
28
+ static execute(password: WordArray | string, salt: WordArray | string, cfg?: OptionalEVPKDFConfig): WordArray;
29
+ /**
30
+ * Derives a key from a password.
31
+ *
32
+ * @param password The password.
33
+ * @param salt A salt.
34
+ *
35
+ * @return The derived key.
36
+ *
37
+ * @example
38
+ *
39
+ * let key = kdf.compute(password, salt);
40
+ */
41
+ compute(password: WordArray | string, salt: WordArray | string): WordArray;
42
+ }
@@ -0,0 +1,59 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ import { MD5 } from "../algo/md5.class.js";
3
+ class EVPKDF {
4
+ /**
5
+ * Initializes a newly created key derivation function.
6
+ *
7
+ * @param cfg (Optional) The configuration options to use for the derivation.
8
+ *
9
+ * @example
10
+ *
11
+ * let kdf = EvpKDF.create();
12
+ * let kdf = EvpKDF.create({ keySize: 8 });
13
+ * let kdf = EvpKDF.create({ keySize: 8, iterations: 1000 });
14
+ */
15
+ constructor(cfg) {
16
+ this.cfg = Object.assign({
17
+ keySize: 128 / 32,
18
+ hasher: MD5,
19
+ iterations: 1
20
+ }, cfg);
21
+ }
22
+ static execute(password, salt, cfg) {
23
+ return new EVPKDF(cfg).compute(password, salt);
24
+ }
25
+ /**
26
+ * Derives a key from a password.
27
+ *
28
+ * @param password The password.
29
+ * @param salt A salt.
30
+ *
31
+ * @return The derived key.
32
+ *
33
+ * @example
34
+ *
35
+ * let key = kdf.compute(password, salt);
36
+ */
37
+ compute(password, salt) {
38
+ const hasher = new this.cfg.hasher();
39
+ const derivedKey = new WordArray();
40
+ let block;
41
+ while (derivedKey.words.length < this.cfg.keySize) {
42
+ if (block) {
43
+ hasher.update(block);
44
+ }
45
+ block = hasher.update(password).finalize(salt);
46
+ hasher.reset();
47
+ for (let i = 1; i < this.cfg.iterations; i++) {
48
+ block = hasher.finalize(block);
49
+ hasher.reset();
50
+ }
51
+ derivedKey.concat(block);
52
+ }
53
+ derivedKey.sigBytes = this.cfg.keySize * 4;
54
+ return derivedKey;
55
+ }
56
+ }
57
+ export {
58
+ EVPKDF
59
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./pbkdf2.class.js";
2
+ export * from "./evpkdf.class.js";
3
+ export * from "./kdf.model.js";
@@ -0,0 +1,3 @@
1
+ export * from "./pbkdf2.class.js";
2
+ export * from "./evpkdf.class.js";
3
+ export * from "./kdf.model.js";
@@ -0,0 +1,7 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ import { CipherParams } from "../lib/cipher-params.class.js";
3
+ export declare abstract class AbstractKDF {
4
+ static execute: (password: string, keySize: number, ivSize: number, salt?: WordArray | string) => CipherParams;
5
+ abstract compute?(password: WordArray | string, salt: WordArray | string): WordArray;
6
+ }
7
+ export type KDF = typeof AbstractKDF;
@@ -0,0 +1,5 @@
1
+ class AbstractKDF {
2
+ }
3
+ export {
4
+ AbstractKDF
5
+ };
@@ -0,0 +1,21 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ import { CipherParams } from "../lib/cipher-params.class.js";
3
+ import { AbstractKDF } from "./kdf.model.js";
4
+ export declare class OpenSSLKdf implements AbstractKDF {
5
+ /**
6
+ * Derives a key and IV from a password.
7
+ *
8
+ * @param password The password to derive from.
9
+ * @param keySize The size in words of the key to generate.
10
+ * @param ivSize The size in words of the IV to generate.
11
+ * @param salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly.
12
+ *
13
+ * @return A cipher params object with the key, IV, and salt.
14
+ *
15
+ * @example
16
+ *
17
+ * let derivedParams = OpenSSL.execute('Password', 256/32, 128/32);
18
+ * let derivedParams = OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
19
+ */
20
+ static execute(password: string, keySize: number, ivSize: number, salt?: WordArray | string): CipherParams;
21
+ }
@@ -0,0 +1,32 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ import { CipherParams } from "../lib/cipher-params.class.js";
3
+ import { EVPKDF } from "./evpkdf.class.js";
4
+ class OpenSSLKdf {
5
+ /**
6
+ * Derives a key and IV from a password.
7
+ *
8
+ * @param password The password to derive from.
9
+ * @param keySize The size in words of the key to generate.
10
+ * @param ivSize The size in words of the IV to generate.
11
+ * @param salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly.
12
+ *
13
+ * @return A cipher params object with the key, IV, and salt.
14
+ *
15
+ * @example
16
+ *
17
+ * let derivedParams = OpenSSL.execute('Password', 256/32, 128/32);
18
+ * let derivedParams = OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
19
+ */
20
+ static execute(password, keySize, ivSize, salt) {
21
+ if (!salt) {
22
+ salt = WordArray.random(64 / 8);
23
+ }
24
+ const key = new EVPKDF({ keySize: keySize + ivSize }).compute(password, salt);
25
+ const iv = new WordArray(key.words.slice(keySize), ivSize * 4);
26
+ key.sigBytes = keySize * 4;
27
+ return new CipherParams({ key, iv, salt });
28
+ }
29
+ }
30
+ export {
31
+ OpenSSLKdf
32
+ };
@@ -0,0 +1,18 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ import { HasherConstructor } from "../lib/hasher-constructor.type.js";
3
+ import { AbstractKDF } from "./kdf.model.js";
4
+ export interface PBKDF2Config {
5
+ keySize: number;
6
+ hasher: HasherConstructor;
7
+ iterations: number;
8
+ }
9
+ export declare class PBKDF2 implements AbstractKDF {
10
+ cfg: PBKDF2Config;
11
+ constructor(cfg: Partial<PBKDF2Config>);
12
+ static execute(password: WordArray | string, salt: WordArray | string, cfg?: {
13
+ keySize?: number;
14
+ hasher?: HasherConstructor;
15
+ iterations?: number;
16
+ }): WordArray;
17
+ compute(password: WordArray | string, salt: WordArray | string): WordArray;
18
+ }
@@ -0,0 +1,43 @@
1
+ import { SHA256 } from "../algo/sha256.class.js";
2
+ import { WordArray } from "../lib/word-array.class.js";
3
+ import { HMAC } from "../lib/hmac.class.js";
4
+ class PBKDF2 {
5
+ constructor(cfg) {
6
+ this.cfg = Object.assign({ keySize: 128 / 32, hasher: SHA256, iterations: 25e4 }, cfg);
7
+ }
8
+ static execute(password, salt, cfg) {
9
+ return new PBKDF2(cfg).compute(password, salt);
10
+ }
11
+ compute(password, salt) {
12
+ const cfg = this.cfg;
13
+ const hmac = new HMAC(cfg.hasher, password);
14
+ const derivedKey = new WordArray();
15
+ const blockIndex = new WordArray([1]);
16
+ const derivedKeyWords = derivedKey.words;
17
+ const blockIndexWords = blockIndex.words;
18
+ const keySize = cfg.keySize;
19
+ const iterations = cfg.iterations;
20
+ while (derivedKeyWords.length < keySize) {
21
+ const block = hmac.update(salt).finalize(blockIndex);
22
+ hmac.reset();
23
+ const blockWords = block.words;
24
+ const blockWordsLength = blockWords.length;
25
+ let intermediate = block;
26
+ for (let i = 1; i < iterations; i++) {
27
+ intermediate = hmac.finalize(intermediate);
28
+ hmac.reset();
29
+ const intermediateWords = intermediate.words;
30
+ for (let j = 0; j < blockWordsLength; j++) {
31
+ blockWords[j] ^= intermediateWords[j];
32
+ }
33
+ }
34
+ derivedKey.concat(block);
35
+ blockIndexWords[0]++;
36
+ }
37
+ derivedKey.sigBytes = keySize * 4;
38
+ return derivedKey;
39
+ }
40
+ }
41
+ export {
42
+ PBKDF2
43
+ };
@@ -0,0 +1,13 @@
1
+ import { Cipher } from "./cipher.class.js";
2
+ import { WordArray } from "./word-array.class.js";
3
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
4
+ import { BlockCipherModeAlgorithm } from "../mode/block-cipher-mode-algorithm.class.js";
5
+ export declare abstract class BlockCipher extends Cipher {
6
+ protected _mode: BlockCipherModeAlgorithm;
7
+ constructor(xformMode: number, key: WordArray, cfg?: BufferedBlockAlgorithmConfig);
8
+ abstract decryptBlock(M: number[], offset: number): void;
9
+ abstract encryptBlock(M: number[], offset: number): void;
10
+ reset(): void;
11
+ protected _doFinalize(): WordArray;
12
+ protected _doProcessBlock(words: number[], offset: number): void;
13
+ }
@@ -0,0 +1,55 @@
1
+ import { Cipher } from "./cipher.class.js";
2
+ import { CBC } from "../mode/cbc/cbc.class.js";
3
+ import { PKCS7 } from "../pad/pkcs7.class.js";
4
+ class BlockCipher extends Cipher {
5
+ constructor(xformMode, key, cfg) {
6
+ super(xformMode, key, Object.assign({
7
+ // default: 128 / 32
8
+ blockSize: 4,
9
+ mode: CBC,
10
+ padding: PKCS7
11
+ }, cfg));
12
+ }
13
+ reset() {
14
+ super.reset();
15
+ if (this.cfg.mode === void 0) {
16
+ throw new Error("missing mode in config");
17
+ }
18
+ let modeCreator;
19
+ if (this._xformMode === this.constructor._ENC_XFORM_MODE) {
20
+ modeCreator = this.cfg.mode.createEncryptor;
21
+ } else {
22
+ modeCreator = this.cfg.mode.createDecryptor;
23
+ this._minBufferSize = 1;
24
+ }
25
+ if (this._mode && this._mode.__creator === modeCreator) {
26
+ this._mode.init(this, this.cfg.iv && this.cfg.iv.words);
27
+ } else {
28
+ this._mode = modeCreator.call(this.cfg.mode, this, this.cfg.iv && this.cfg.iv.words);
29
+ this._mode.__creator = modeCreator;
30
+ }
31
+ }
32
+ _doFinalize() {
33
+ if (this.cfg.padding === void 0) {
34
+ throw new Error("missing padding in config");
35
+ }
36
+ let finalProcessedBlocks;
37
+ if (this._xformMode === this.constructor._ENC_XFORM_MODE) {
38
+ if (this.cfg.blockSize === void 0) {
39
+ throw new Error("missing blockSize in config");
40
+ }
41
+ this.cfg.padding.pad(this._data, this.cfg.blockSize);
42
+ finalProcessedBlocks = this._process(true);
43
+ } else {
44
+ finalProcessedBlocks = this._process(true);
45
+ this.cfg.padding.unpad(finalProcessedBlocks);
46
+ }
47
+ return finalProcessedBlocks;
48
+ }
49
+ _doProcessBlock(words, offset) {
50
+ this._mode.processBlock(words, offset);
51
+ }
52
+ }
53
+ export {
54
+ BlockCipher
55
+ };
@@ -0,0 +1,13 @@
1
+ import { Formatter } from "../format/formatter.interface.js";
2
+ import { WordArray } from "./word-array.class.js";
3
+ import { KDF } from "../kdf/kdf.model.js";
4
+ import { BlockCipherMode } from "../mode/block-cipher-mode.class.js";
5
+ import { Padding } from "../pad/padding.model.js";
6
+ export interface BufferedBlockAlgorithmConfig {
7
+ blockSize?: number;
8
+ iv?: WordArray;
9
+ format?: Formatter;
10
+ kdf?: KDF;
11
+ mode?: typeof BlockCipherMode;
12
+ padding?: Padding;
13
+ }
@@ -0,0 +1,54 @@
1
+ import { WordArray } from "./word-array.class.js";
2
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
3
+ export declare abstract class BufferedBlockAlgorithm {
4
+ cfg: BufferedBlockAlgorithmConfig;
5
+ protected _data: WordArray;
6
+ protected _minBufferSize: number;
7
+ protected _nDataBytes: number;
8
+ protected abstract _doProcessBlock(wordArray: number[], offset: number): void;
9
+ constructor(cfg?: BufferedBlockAlgorithmConfig);
10
+ /**
11
+ * Creates a copy of this object.
12
+ *
13
+ * @return The clone.
14
+ *
15
+ * @example
16
+ *
17
+ * let clone = bufferedBlockAlgorithm.clone();
18
+ */
19
+ clone(): BufferedBlockAlgorithm;
20
+ /**
21
+ * Resets this block algorithm's data buffer to its initial state.
22
+ *
23
+ * @example
24
+ *
25
+ * bufferedBlockAlgorithm.reset();
26
+ */
27
+ reset(): void;
28
+ /**
29
+ * Adds new data to this block algorithm's buffer.
30
+ *
31
+ * @param data The data to append. Strings are converted to a WordArray using UTF-8.
32
+ *
33
+ * @example
34
+ *
35
+ * bufferedBlockAlgorithm._append('data');
36
+ * bufferedBlockAlgorithm._append(wordArray);
37
+ */
38
+ protected _append(data: string | WordArray): void;
39
+ /**
40
+ * Processes available data blocks.
41
+ *
42
+ * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
43
+ *
44
+ * @param doFlush Whether all blocks and partial blocks should be processed.
45
+ *
46
+ * @return The processed data.
47
+ *
48
+ * @example
49
+ *
50
+ * let processedData = bufferedBlockAlgorithm._process();
51
+ * let processedData = bufferedBlockAlgorithm._process(!!'flush');
52
+ */
53
+ protected _process(doFlush?: boolean): WordArray;
54
+ }
@@ -0,0 +1,99 @@
1
+ import { WordArray } from "./word-array.class.js";
2
+ import { Utf8 } from "../enc/utf8.class.js";
3
+ class BufferedBlockAlgorithm {
4
+ constructor(cfg) {
5
+ this._minBufferSize = 0;
6
+ this.cfg = Object.assign({
7
+ blockSize: 1
8
+ }, cfg);
9
+ this._data = new WordArray();
10
+ this._nDataBytes = 0;
11
+ }
12
+ /**
13
+ * Creates a copy of this object.
14
+ *
15
+ * @return The clone.
16
+ *
17
+ * @example
18
+ *
19
+ * let clone = bufferedBlockAlgorithm.clone();
20
+ */
21
+ clone() {
22
+ const clone = this.constructor();
23
+ for (const attr in this) {
24
+ if (Object.prototype.hasOwnProperty.call(this, attr)) {
25
+ clone[attr] = this[attr];
26
+ }
27
+ }
28
+ clone._data = this._data.clone();
29
+ return clone;
30
+ }
31
+ /**
32
+ * Resets this block algorithm's data buffer to its initial state.
33
+ *
34
+ * @example
35
+ *
36
+ * bufferedBlockAlgorithm.reset();
37
+ */
38
+ reset() {
39
+ this._data = new WordArray();
40
+ this._nDataBytes = 0;
41
+ }
42
+ /**
43
+ * Adds new data to this block algorithm's buffer.
44
+ *
45
+ * @param data The data to append. Strings are converted to a WordArray using UTF-8.
46
+ *
47
+ * @example
48
+ *
49
+ * bufferedBlockAlgorithm._append('data');
50
+ * bufferedBlockAlgorithm._append(wordArray);
51
+ */
52
+ _append(data) {
53
+ if (typeof data === "string") {
54
+ data = Utf8.parse(data);
55
+ }
56
+ this._data.concat(data);
57
+ this._nDataBytes += data.sigBytes;
58
+ }
59
+ /**
60
+ * Processes available data blocks.
61
+ *
62
+ * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
63
+ *
64
+ * @param doFlush Whether all blocks and partial blocks should be processed.
65
+ *
66
+ * @return The processed data.
67
+ *
68
+ * @example
69
+ *
70
+ * let processedData = bufferedBlockAlgorithm._process();
71
+ * let processedData = bufferedBlockAlgorithm._process(!!'flush');
72
+ */
73
+ _process(doFlush) {
74
+ if (!this.cfg.blockSize) {
75
+ throw new Error("missing blockSize in config");
76
+ }
77
+ const blockSizeBytes = this.cfg.blockSize * 4;
78
+ let nBlocksReady = this._data.sigBytes / blockSizeBytes;
79
+ if (doFlush) {
80
+ nBlocksReady = Math.ceil(nBlocksReady);
81
+ } else {
82
+ nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
83
+ }
84
+ const nWordsReady = nBlocksReady * this.cfg.blockSize;
85
+ const nBytesReady = Math.min(nWordsReady * 4, this._data.sigBytes);
86
+ let processedWords;
87
+ if (nWordsReady) {
88
+ for (let offset = 0; offset < nWordsReady; offset += this.cfg.blockSize) {
89
+ this._doProcessBlock(this._data.words, offset);
90
+ }
91
+ processedWords = this._data.words.splice(0, nWordsReady);
92
+ this._data.sigBytes -= nBytesReady;
93
+ }
94
+ return new WordArray(processedWords, nBytesReady);
95
+ }
96
+ }
97
+ export {
98
+ BufferedBlockAlgorithm
99
+ };
@@ -0,0 +1,10 @@
1
+ import { WordArray } from "./word-array.class.js";
2
+ import { BufferedBlockAlgorithmConfig } from "./buffered-block-algorithm-config.interface.js";
3
+ import { CipherParams } from "./cipher-params.class.js";
4
+ import { Cipher } from "./cipher.class.js";
5
+ export declare class CipherHelper {
6
+ private readonly _cipher;
7
+ constructor(_cipher: typeof Cipher);
8
+ decrypt(ciphertext: CipherParams | string, key: WordArray | string, cfg?: BufferedBlockAlgorithmConfig): WordArray;
9
+ encrypt(message: WordArray | string, key: WordArray | string, cfg?: BufferedBlockAlgorithmConfig): CipherParams;
10
+ }
@@ -0,0 +1,24 @@
1
+ import { PasswordBasedCipher } from "./password-based-cipher.class.js";
2
+ import { SerializableCipher } from "./serializable-cipher.class.js";
3
+ class CipherHelper {
4
+ constructor(_cipher) {
5
+ this._cipher = _cipher;
6
+ }
7
+ decrypt(ciphertext, key, cfg) {
8
+ if (typeof key === "string") {
9
+ return PasswordBasedCipher.decrypt(this._cipher, ciphertext, key, cfg);
10
+ } else {
11
+ return SerializableCipher.decrypt(this._cipher, ciphertext, key, cfg);
12
+ }
13
+ }
14
+ encrypt(message, key, cfg) {
15
+ if (typeof key === "string") {
16
+ return PasswordBasedCipher.encrypt(this._cipher, message, key, cfg);
17
+ } else {
18
+ return SerializableCipher.encrypt(this._cipher, message, key, cfg);
19
+ }
20
+ }
21
+ }
22
+ export {
23
+ CipherHelper
24
+ };
@@ -0,0 +1,53 @@
1
+ import { WordArray } from "./word-array.class.js";
2
+ import { Cipher } from "./cipher.class.js";
3
+ import { BlockCipherMode } from "../mode/block-cipher-mode.class.js";
4
+ import { Padding } from "../pad/padding.model.js";
5
+ import { Formatter } from "../format/formatter.interface.js";
6
+ export declare class CipherParams {
7
+ algorithm?: typeof Cipher;
8
+ blockSize?: number;
9
+ ciphertext?: WordArray;
10
+ formatter?: Formatter;
11
+ iv?: WordArray;
12
+ key?: WordArray | string;
13
+ mode?: typeof BlockCipherMode;
14
+ padding?: Padding;
15
+ salt?: WordArray | string;
16
+ /**
17
+ * Initializes a newly created cipher params object.
18
+ *
19
+ * @param cipherParams An object with any of the possible cipher parameters.
20
+ *
21
+ * @example
22
+ *
23
+ * let cipherParams = CipherParams.create({
24
+ * ciphertext: ciphertextWordArray,
25
+ * key: keyWordArray,
26
+ * iv: ivWordArray,
27
+ * salt: saltWordArray,
28
+ * algorithm: AES,
29
+ * mode: CBC,
30
+ * padding: PKCS7,
31
+ * blockSize: 4,
32
+ * formatter: OpenSSLFormatter
33
+ * });
34
+ */
35
+ constructor(cipherParams: Omit<CipherParams, "extend" | "toString">);
36
+ extend(additionalParams: CipherParams): CipherParams;
37
+ /**
38
+ * Converts this cipher params object to a string.
39
+ *
40
+ * @param formatter (Optional) The formatting strategy to use.
41
+ *
42
+ * @return The stringified cipher params.
43
+ *
44
+ * @throws Error If neither the formatter nor the default formatter is set.
45
+ *
46
+ * @example
47
+ *
48
+ * let string = cipherParams + '';
49
+ * let string = cipherParams.toString();
50
+ * let string = cipherParams.toString(CryptoJS.format.OpenSSL);
51
+ */
52
+ toString(formatter?: Formatter): string;
53
+ }
@@ -0,0 +1,89 @@
1
+ class CipherParams {
2
+ /**
3
+ * Initializes a newly created cipher params object.
4
+ *
5
+ * @param cipherParams An object with any of the possible cipher parameters.
6
+ *
7
+ * @example
8
+ *
9
+ * let cipherParams = CipherParams.create({
10
+ * ciphertext: ciphertextWordArray,
11
+ * key: keyWordArray,
12
+ * iv: ivWordArray,
13
+ * salt: saltWordArray,
14
+ * algorithm: AES,
15
+ * mode: CBC,
16
+ * padding: PKCS7,
17
+ * blockSize: 4,
18
+ * formatter: OpenSSLFormatter
19
+ * });
20
+ */
21
+ constructor(cipherParams) {
22
+ this.ciphertext = cipherParams.ciphertext;
23
+ this.key = cipherParams.key;
24
+ this.iv = cipherParams.iv;
25
+ this.salt = cipherParams.salt;
26
+ this.algorithm = cipherParams.algorithm;
27
+ this.mode = cipherParams.mode;
28
+ this.padding = cipherParams.padding;
29
+ this.blockSize = cipherParams.blockSize;
30
+ this.formatter = cipherParams.formatter;
31
+ }
32
+ extend(additionalParams) {
33
+ if (additionalParams.ciphertext !== void 0) {
34
+ this.ciphertext = additionalParams.ciphertext;
35
+ }
36
+ if (additionalParams.key !== void 0) {
37
+ this.key = additionalParams.key;
38
+ }
39
+ if (additionalParams.iv !== void 0) {
40
+ this.iv = additionalParams.iv;
41
+ }
42
+ if (additionalParams.salt !== void 0) {
43
+ this.salt = additionalParams.salt;
44
+ }
45
+ if (additionalParams.algorithm !== void 0) {
46
+ this.algorithm = additionalParams.algorithm;
47
+ }
48
+ if (additionalParams.mode !== void 0) {
49
+ this.mode = additionalParams.mode;
50
+ }
51
+ if (additionalParams.padding !== void 0) {
52
+ this.padding = additionalParams.padding;
53
+ }
54
+ if (additionalParams.blockSize !== void 0) {
55
+ this.blockSize = additionalParams.blockSize;
56
+ }
57
+ if (additionalParams.formatter !== void 0) {
58
+ this.formatter = additionalParams.formatter;
59
+ }
60
+ return this;
61
+ }
62
+ /**
63
+ * Converts this cipher params object to a string.
64
+ *
65
+ * @param formatter (Optional) The formatting strategy to use.
66
+ *
67
+ * @return The stringified cipher params.
68
+ *
69
+ * @throws Error If neither the formatter nor the default formatter is set.
70
+ *
71
+ * @example
72
+ *
73
+ * let string = cipherParams + '';
74
+ * let string = cipherParams.toString();
75
+ * let string = cipherParams.toString(CryptoJS.format.OpenSSL);
76
+ */
77
+ toString(formatter) {
78
+ if (formatter) {
79
+ return formatter.stringify(this);
80
+ } else if (this.formatter) {
81
+ return this.formatter.stringify(this);
82
+ } else {
83
+ throw new Error("cipher needs a formatter to be able to convert the result into a string");
84
+ }
85
+ }
86
+ }
87
+ export {
88
+ CipherParams
89
+ };