@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,80 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ class Base64 {
3
+ /**
4
+ * Converts a Base64 string to a word array.
5
+ *
6
+ * @param base64Str The Base64 string.
7
+ *
8
+ * @return The word array.
9
+ *
10
+ * @example
11
+ *
12
+ * let wordArray = Base64.parse(base64String);
13
+ */
14
+ static parse(base64Str) {
15
+ let base64StrLength = base64Str.length;
16
+ if (this._reverseMap === void 0) {
17
+ this._reverseMap = [];
18
+ for (let j = 0; j < this._map.length; j++) {
19
+ this._reverseMap[this._map.charCodeAt(j)] = j;
20
+ }
21
+ }
22
+ const paddingChar = this._map.charAt(64);
23
+ if (paddingChar) {
24
+ const paddingIndex = base64Str.indexOf(paddingChar);
25
+ if (paddingIndex !== -1) {
26
+ base64StrLength = paddingIndex;
27
+ }
28
+ }
29
+ return this.parseLoop(base64Str, base64StrLength, this._reverseMap);
30
+ }
31
+ static parseLoop(base64Str, base64StrLength, reverseMap) {
32
+ const words = [];
33
+ let nBytes = 0;
34
+ for (let i = 0; i < base64StrLength; i++) {
35
+ if (i % 4) {
36
+ const bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << i % 4 * 2;
37
+ const bits2 = reverseMap[base64Str.charCodeAt(i)] >>> 6 - i % 4 * 2;
38
+ words[nBytes >>> 2] |= (bits1 | bits2) << 24 - nBytes % 4 * 8;
39
+ nBytes++;
40
+ }
41
+ }
42
+ return new WordArray(words, nBytes);
43
+ }
44
+ /**
45
+ * Converts a word array to a Base64 string.
46
+ *
47
+ * @param wordArray The word array.
48
+ *
49
+ * @return The Base64 string.
50
+ *
51
+ * @example
52
+ *
53
+ * let base64String = Base64.stringify(wordArray);
54
+ */
55
+ static stringify(wordArray) {
56
+ wordArray.clamp();
57
+ const base64Chars = [];
58
+ for (let i = 0; i < wordArray.sigBytes; i += 3) {
59
+ const byte1 = wordArray.words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
60
+ const byte2 = wordArray.words[i + 1 >>> 2] >>> 24 - (i + 1) % 4 * 8 & 255;
61
+ const byte3 = wordArray.words[i + 2 >>> 2] >>> 24 - (i + 2) % 4 * 8 & 255;
62
+ const triplet = byte1 << 16 | byte2 << 8 | byte3;
63
+ for (let j = 0; j < 4 && i + j * 0.75 < wordArray.sigBytes; j++) {
64
+ base64Chars.push(this._map.charAt(triplet >>> 6 * (3 - j) & 63));
65
+ }
66
+ }
67
+ const paddingChar = this._map.charAt(64);
68
+ if (paddingChar) {
69
+ while (base64Chars.length % 4) {
70
+ base64Chars.push(paddingChar);
71
+ }
72
+ }
73
+ return base64Chars.join("");
74
+ }
75
+ }
76
+ Base64._map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
77
+ Base64._reverseMap = void 0;
78
+ export {
79
+ Base64
80
+ };
@@ -0,0 +1,6 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ export declare abstract class AbstractEncoder {
3
+ static parse: (word: string) => WordArray;
4
+ static stringify: (word: WordArray) => string;
5
+ }
6
+ export type Encoder = typeof AbstractEncoder;
@@ -0,0 +1,5 @@
1
+ class AbstractEncoder {
2
+ }
3
+ export {
4
+ AbstractEncoder
5
+ };
@@ -0,0 +1,27 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ export declare class Hex {
3
+ /**
4
+ * Converts a hex string to a word array.
5
+ *
6
+ * @param hexStr The hex string.
7
+ *
8
+ * @return The word array.
9
+ *
10
+ * @example
11
+ *
12
+ * let wordArray = Hex.parse(hexString);
13
+ */
14
+ static parse(hexStr: string): WordArray;
15
+ /**
16
+ * Converts a word array to a hex string.
17
+ *
18
+ * @param wordArray The word array.
19
+ *
20
+ * @return The hex string.
21
+ *
22
+ * @example
23
+ *
24
+ * let hexString = Hex.stringify(wordArray);
25
+ */
26
+ static stringify(wordArray: WordArray): string;
27
+ }
@@ -0,0 +1,45 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ class Hex {
3
+ /**
4
+ * Converts a hex string to a word array.
5
+ *
6
+ * @param hexStr The hex string.
7
+ *
8
+ * @return The word array.
9
+ *
10
+ * @example
11
+ *
12
+ * let wordArray = Hex.parse(hexString);
13
+ */
14
+ static parse(hexStr) {
15
+ const hexStrLength = hexStr.length;
16
+ const words = [];
17
+ for (let i = 0; i < hexStrLength; i += 2) {
18
+ words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << 24 - i % 8 * 4;
19
+ }
20
+ return new WordArray(words, hexStrLength / 2);
21
+ }
22
+ /**
23
+ * Converts a word array to a hex string.
24
+ *
25
+ * @param wordArray The word array.
26
+ *
27
+ * @return The hex string.
28
+ *
29
+ * @example
30
+ *
31
+ * let hexString = Hex.stringify(wordArray);
32
+ */
33
+ static stringify(wordArray) {
34
+ const hexChars = [];
35
+ for (let i = 0; i < wordArray.sigBytes; i++) {
36
+ const bite = wordArray.words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
37
+ hexChars.push((bite >>> 4).toString(16));
38
+ hexChars.push((bite & 15).toString(16));
39
+ }
40
+ return hexChars.join("");
41
+ }
42
+ }
43
+ export {
44
+ Hex
45
+ };
@@ -0,0 +1,6 @@
1
+ export * from "./base64.class.js";
2
+ export * from "./encoder.model.js";
3
+ export * from "./hex.class.js";
4
+ export * from "./latin1.class.js";
5
+ export * from "./utf16.class.js";
6
+ export * from "./utf8.class.js";
@@ -0,0 +1,6 @@
1
+ export * from "./base64.class.js";
2
+ export * from "./encoder.model.js";
3
+ export * from "./hex.class.js";
4
+ export * from "./latin1.class.js";
5
+ export * from "./utf16.class.js";
6
+ export * from "./utf8.class.js";
@@ -0,0 +1,27 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ export declare class Latin1 {
3
+ /**
4
+ * Converts a Latin1 string to a word array.
5
+ *
6
+ * @param latin1Str The Latin1 string.
7
+ *
8
+ * @return The word array.
9
+ *
10
+ * @example
11
+ *
12
+ * let wordArray = Latin1.parse(latin1String);
13
+ */
14
+ static parse(latin1Str: string): WordArray;
15
+ /**
16
+ * Converts a word array to a Latin1 string.
17
+ *
18
+ * @param wordArray The word array.
19
+ *
20
+ * @return The Latin1 string.
21
+ *
22
+ * @example
23
+ *
24
+ * let latin1String = Latin1.stringify(wordArray);
25
+ */
26
+ static stringify(wordArray: WordArray): string;
27
+ }
@@ -0,0 +1,44 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ class Latin1 {
3
+ /**
4
+ * Converts a Latin1 string to a word array.
5
+ *
6
+ * @param latin1Str The Latin1 string.
7
+ *
8
+ * @return The word array.
9
+ *
10
+ * @example
11
+ *
12
+ * let wordArray = Latin1.parse(latin1String);
13
+ */
14
+ static parse(latin1Str) {
15
+ const latin1StrLength = latin1Str.length;
16
+ const words = [];
17
+ for (let i = 0; i < latin1StrLength; i++) {
18
+ words[i >>> 2] |= (latin1Str.charCodeAt(i) & 255) << 24 - i % 4 * 8;
19
+ }
20
+ return new WordArray(words, latin1StrLength);
21
+ }
22
+ /**
23
+ * Converts a word array to a Latin1 string.
24
+ *
25
+ * @param wordArray The word array.
26
+ *
27
+ * @return The Latin1 string.
28
+ *
29
+ * @example
30
+ *
31
+ * let latin1String = Latin1.stringify(wordArray);
32
+ */
33
+ static stringify(wordArray) {
34
+ const latin1Chars = [];
35
+ for (let i = 0; i < wordArray.sigBytes; i++) {
36
+ const bite = wordArray.words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
37
+ latin1Chars.push(String.fromCharCode(bite));
38
+ }
39
+ return latin1Chars.join("");
40
+ }
41
+ }
42
+ export {
43
+ Latin1
44
+ };
@@ -0,0 +1,28 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ import { AbstractEncoder } from "./encoder.model.js";
3
+ export declare class Utf16 implements AbstractEncoder {
4
+ /**
5
+ * Converts a UTF-16 BE string to a word array.
6
+ *
7
+ * @param utf16Str The UTF-16 BE string.
8
+ *
9
+ * @return The word array.
10
+ *
11
+ * @example
12
+ *
13
+ * const wordArray = Utf16.parse(utf8String);
14
+ */
15
+ static parse(utf16Str: string): WordArray;
16
+ /**
17
+ * Converts a word array to a UTF-16 BE string.
18
+ *
19
+ * @param wordArray The word array.
20
+ *
21
+ * @return The UTF-16 BE string.
22
+ *
23
+ * @example
24
+ *
25
+ * const utf16String = Utf16.stringify(wordArray);
26
+ */
27
+ static stringify(wordArray: WordArray): string;
28
+ }
@@ -0,0 +1,46 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ class Utf16 {
3
+ /**
4
+ * Converts a UTF-16 BE string to a word array.
5
+ *
6
+ * @param utf16Str The UTF-16 BE string.
7
+ *
8
+ * @return The word array.
9
+ *
10
+ * @example
11
+ *
12
+ * const wordArray = Utf16.parse(utf8String);
13
+ */
14
+ static parse(utf16Str) {
15
+ const utf16StrLength = utf16Str.length;
16
+ const words = [];
17
+ for (let i = 0; i < utf16StrLength; i++) {
18
+ words[i >>> 1] |= utf16Str.charCodeAt(i) << 16 - i % 2 * 16;
19
+ }
20
+ return new WordArray(words, utf16StrLength * 2);
21
+ }
22
+ /**
23
+ * Converts a word array to a UTF-16 BE string.
24
+ *
25
+ * @param wordArray The word array.
26
+ *
27
+ * @return The UTF-16 BE string.
28
+ *
29
+ * @example
30
+ *
31
+ * const utf16String = Utf16.stringify(wordArray);
32
+ */
33
+ static stringify(wordArray) {
34
+ const words = wordArray.words;
35
+ const sigBytes = wordArray.sigBytes;
36
+ const utf16Chars = [];
37
+ for (let i = 0; i < sigBytes; i += 2) {
38
+ const codePoint = words[i >>> 2] >>> 16 - i % 4 * 8 & 65535;
39
+ utf16Chars.push(String.fromCharCode(codePoint));
40
+ }
41
+ return utf16Chars.join("");
42
+ }
43
+ }
44
+ export {
45
+ Utf16
46
+ };
@@ -0,0 +1,28 @@
1
+ import { WordArray } from "../lib/word-array.class.js";
2
+ import { AbstractEncoder } from "./encoder.model.js";
3
+ export declare class Utf8 implements AbstractEncoder {
4
+ /**
5
+ * Converts a UTF-8 string to a word array.
6
+ *
7
+ * @param utf8Str The UTF-8 string.
8
+ *
9
+ * @return The word array.
10
+ *
11
+ * @example
12
+ *
13
+ * let wordArray = Utf8.parse(utf8String);
14
+ */
15
+ static parse(utf8Str: string): WordArray;
16
+ /**
17
+ * Converts a word array to a UTF-8 string.
18
+ *
19
+ * @param wordArray The word array.
20
+ *
21
+ * @return The UTF-8 string.
22
+ *
23
+ * @example
24
+ *
25
+ * const utf8String = Utf8.stringify(wordArray);
26
+ */
27
+ static stringify(wordArray: WordArray): string;
28
+ }
@@ -0,0 +1,39 @@
1
+ import { Latin1 } from "./latin1.class.js";
2
+ class Utf8 {
3
+ /**
4
+ * Converts a UTF-8 string to a word array.
5
+ *
6
+ * @param utf8Str The UTF-8 string.
7
+ *
8
+ * @return The word array.
9
+ *
10
+ * @example
11
+ *
12
+ * let wordArray = Utf8.parse(utf8String);
13
+ */
14
+ static parse(utf8Str) {
15
+ return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
16
+ }
17
+ /**
18
+ * Converts a word array to a UTF-8 string.
19
+ *
20
+ * @param wordArray The word array.
21
+ *
22
+ * @return The UTF-8 string.
23
+ *
24
+ * @example
25
+ *
26
+ * const utf8String = Utf8.stringify(wordArray);
27
+ */
28
+ static stringify(wordArray) {
29
+ try {
30
+ return decodeURIComponent(escape(Latin1.stringify(wordArray)));
31
+ } catch (e) {
32
+ console.error("Malformed UTF-8 data", e);
33
+ throw new Error("Malformed UTF-8 data");
34
+ }
35
+ }
36
+ }
37
+ export {
38
+ Utf8
39
+ };
@@ -0,0 +1,5 @@
1
+ import { CipherParams } from '../lib/cipher-params.class.js';
2
+ export interface Formatter {
3
+ stringify: (cipherParams: CipherParams) => string;
4
+ parse: (paramsStr: string) => CipherParams;
5
+ }
File without changes
@@ -0,0 +1,27 @@
1
+ import { CipherParams } from "../lib/cipher-params.class.js";
2
+ export declare class OpenSSL {
3
+ /**
4
+ * Converts an OpenSSL-compatible string to a cipher params object.
5
+ *
6
+ * @param openSSLStr The OpenSSL-compatible string.
7
+ *
8
+ * @return The cipher params object.
9
+ *
10
+ * @example
11
+ *
12
+ * let cipherParams = OpenSSLFormatter.parse(openSSLString);
13
+ */
14
+ static parse(openSSLStr: string): CipherParams;
15
+ /**
16
+ * Converts a cipher params object to an OpenSSL-compatible string.
17
+ *
18
+ * @param cipherParams The cipher params object.
19
+ *
20
+ * @return The OpenSSL-compatible string.
21
+ *
22
+ * @example
23
+ *
24
+ * let openSSLString = OpenSSLFormatter.stringify(cipherParams);
25
+ */
26
+ static stringify(cipherParams: CipherParams): string;
27
+ }
@@ -0,0 +1,57 @@
1
+ import { CipherParams } from "../lib/cipher-params.class.js";
2
+ import { WordArray } from "../lib/word-array.class.js";
3
+ import { Base64 } from "../enc/base64.class.js";
4
+ class OpenSSL {
5
+ /**
6
+ * Converts an OpenSSL-compatible string to a cipher params object.
7
+ *
8
+ * @param openSSLStr The OpenSSL-compatible string.
9
+ *
10
+ * @return The cipher params object.
11
+ *
12
+ * @example
13
+ *
14
+ * let cipherParams = OpenSSLFormatter.parse(openSSLString);
15
+ */
16
+ static parse(openSSLStr) {
17
+ const ciphertext = Base64.parse(openSSLStr);
18
+ let salt;
19
+ if (ciphertext.words[0] === 1398893684 && ciphertext.words[1] === 1701076831) {
20
+ salt = new WordArray(ciphertext.words.slice(2, 4));
21
+ ciphertext.words.splice(0, 4);
22
+ ciphertext.sigBytes -= 16;
23
+ }
24
+ return new CipherParams({ ciphertext, salt });
25
+ }
26
+ /**
27
+ * Converts a cipher params object to an OpenSSL-compatible string.
28
+ *
29
+ * @param cipherParams The cipher params object.
30
+ *
31
+ * @return The OpenSSL-compatible string.
32
+ *
33
+ * @example
34
+ *
35
+ * let openSSLString = OpenSSLFormatter.stringify(cipherParams);
36
+ */
37
+ static stringify(cipherParams) {
38
+ if (!cipherParams.ciphertext) {
39
+ throw new Error("missing ciphertext in params");
40
+ }
41
+ const ciphertext = cipherParams.ciphertext;
42
+ const salt = cipherParams.salt;
43
+ let wordArray;
44
+ if (salt) {
45
+ if (typeof salt === "string") {
46
+ throw new Error("salt is expected to be a WordArray");
47
+ }
48
+ wordArray = new WordArray([1398893684, 1701076831]).concat(salt).concat(ciphertext);
49
+ } else {
50
+ wordArray = ciphertext;
51
+ }
52
+ return wordArray.toString(Base64);
53
+ }
54
+ }
55
+ export {
56
+ OpenSSL
57
+ };
@@ -0,0 +1,2 @@
1
+ import { CipherHelper } from "../lib/cipher-helper.class.js";
2
+ export declare const aesHelper: CipherHelper;
@@ -0,0 +1,6 @@
1
+ import { BlockCipher } from "../lib/block-cipher.class.js";
2
+ import { AES } from "../algo/aes.class.js";
3
+ const aesHelper = BlockCipher._createHelper(AES);
4
+ export {
5
+ aesHelper
6
+ };
@@ -0,0 +1,2 @@
1
+ import { CipherHelper } from "../lib/cipher-helper.class.js";
2
+ export declare const desHelper: CipherHelper;
@@ -0,0 +1,6 @@
1
+ import { BlockCipher } from "../lib/block-cipher.class.js";
2
+ import { DES } from "../algo/des.class.js";
3
+ const desHelper = BlockCipher._createHelper(DES);
4
+ export {
5
+ desHelper
6
+ };
@@ -0,0 +1,7 @@
1
+ export { aesHelper } from "./aes.helper.js";
2
+ export { desHelper } from "./des.helper.js";
3
+ export { md5Helper } from "./md5.helper.js";
4
+ export { sha1Helper } from "./sha1.helper.js";
5
+ export { sha256Helper } from "./sha256.helper.js";
6
+ export { sha512Helper } from "./sha512.helper.js";
7
+ export { tripleDesHelper } from "./triple-des.helper.js";
@@ -0,0 +1,16 @@
1
+ import { aesHelper } from "./aes.helper.js";
2
+ import { desHelper } from "./des.helper.js";
3
+ import { md5Helper } from "./md5.helper.js";
4
+ import { sha1Helper } from "./sha1.helper.js";
5
+ import { sha256Helper } from "./sha256.helper.js";
6
+ import { sha512Helper } from "./sha512.helper.js";
7
+ import { tripleDesHelper } from "./triple-des.helper.js";
8
+ export {
9
+ aesHelper,
10
+ desHelper,
11
+ md5Helper,
12
+ sha1Helper,
13
+ sha256Helper,
14
+ sha512Helper,
15
+ tripleDesHelper
16
+ };
@@ -0,0 +1,2 @@
1
+ import { HasherHelper } from "../lib/hasher-helper.type.js";
2
+ export declare const md5Helper: HasherHelper;
@@ -0,0 +1,6 @@
1
+ import { Hasher } from "../lib/hasher.class.js";
2
+ import { MD5 } from "../algo/md5.class.js";
3
+ const md5Helper = Hasher._createHelper(MD5);
4
+ export {
5
+ md5Helper
6
+ };
@@ -0,0 +1,2 @@
1
+ import { HasherHelper } from "../lib/hasher-helper.type.js";
2
+ export declare const sha1Helper: HasherHelper;
@@ -0,0 +1,6 @@
1
+ import { Hasher } from "../lib/hasher.class.js";
2
+ import { SHA1 } from "../algo/sha1.class.js";
3
+ const sha1Helper = Hasher._createHelper(SHA1);
4
+ export {
5
+ sha1Helper
6
+ };
@@ -0,0 +1,2 @@
1
+ import { HasherHelper } from "../lib/hasher-helper.type.js";
2
+ export declare const sha256Helper: HasherHelper;
@@ -0,0 +1,6 @@
1
+ import { Hasher } from "../lib/hasher.class.js";
2
+ import { SHA256 } from "../algo/sha256.class.js";
3
+ const sha256Helper = Hasher._createHelper(SHA256);
4
+ export {
5
+ sha256Helper
6
+ };
@@ -0,0 +1,2 @@
1
+ import { HasherHelper } from "../lib/hasher-helper.type.js";
2
+ export declare const sha512Helper: HasherHelper;
@@ -0,0 +1,6 @@
1
+ import { Hasher } from "../lib/hasher.class.js";
2
+ import { SHA512 } from "../algo/sha512.class.js";
3
+ const sha512Helper = Hasher._createHelper(SHA512);
4
+ export {
5
+ sha512Helper
6
+ };
@@ -0,0 +1,2 @@
1
+ import { CipherHelper } from "../lib/cipher-helper.class.js";
2
+ export declare const tripleDesHelper: CipherHelper;
@@ -0,0 +1,6 @@
1
+ import { BlockCipher } from "../lib/block-cipher.class.js";
2
+ import { TripleDES } from "../algo/triple-des.class.js";
3
+ const tripleDesHelper = BlockCipher._createHelper(TripleDES);
4
+ export {
5
+ tripleDesHelper
6
+ };
@@ -0,0 +1,7 @@
1
+ export { BlockCipher, CipherParams, Hasher, PasswordBasedCipher, SerializableCipher, WordArray, X64Word, X64WordArray } from "./lib/index.js";
2
+ export { AES, DES, MD5, SHA1, SHA256, SHA512, TripleDES } from "./algo/index.js";
3
+ export { Base64, Encoder, Hex, Latin1, Utf16, Utf8 } from "./enc/index.js";
4
+ export { EVPKDF, PBKDF2 } from "./kdf/index.js";
5
+ export { aesHelper, desHelper, md5Helper, sha1Helper, sha256Helper, sha512Helper, tripleDesHelper } from "./helpers/index.js";
6
+ export { CBC, CTR, ECB } from "./mode/index.js";
7
+ export { NoPadding, PKCS7 } from "./pad/index.js";
@@ -0,0 +1,43 @@
1
+ import { BlockCipher, CipherParams, Hasher, PasswordBasedCipher, SerializableCipher, WordArray, X64Word, X64WordArray } from "./lib/index.js";
2
+ import { AES, DES, MD5, SHA1, SHA256, SHA512, TripleDES } from "./algo/index.js";
3
+ import { Base64, Hex, Latin1, Utf16, Utf8 } from "./enc/index.js";
4
+ import { EVPKDF, PBKDF2 } from "./kdf/index.js";
5
+ import { aesHelper, desHelper, md5Helper, sha1Helper, sha256Helper, sha512Helper, tripleDesHelper } from "./helpers/index.js";
6
+ import { CBC, CTR, ECB } from "./mode/index.js";
7
+ import { NoPadding, PKCS7 } from "./pad/index.js";
8
+ export {
9
+ AES,
10
+ Base64,
11
+ BlockCipher,
12
+ CBC,
13
+ CTR,
14
+ CipherParams,
15
+ DES,
16
+ ECB,
17
+ EVPKDF,
18
+ Hasher,
19
+ Hex,
20
+ Latin1,
21
+ MD5,
22
+ NoPadding,
23
+ PBKDF2,
24
+ PKCS7,
25
+ PasswordBasedCipher,
26
+ SHA1,
27
+ SHA256,
28
+ SHA512,
29
+ SerializableCipher,
30
+ TripleDES,
31
+ Utf16,
32
+ Utf8,
33
+ WordArray,
34
+ X64Word,
35
+ X64WordArray,
36
+ aesHelper,
37
+ desHelper,
38
+ md5Helper,
39
+ sha1Helper,
40
+ sha256Helper,
41
+ sha512Helper,
42
+ tripleDesHelper
43
+ };