@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.
- package/LICENCE +21 -0
- package/README.md +119 -0
- package/dist/cjs/algo/aes.class.d.cts +16 -0
- package/dist/cjs/algo/des.class.d.cts +19 -0
- package/dist/cjs/algo/index.d.cts +7 -0
- package/dist/cjs/algo/md5.class.d.cts +13 -0
- package/dist/cjs/algo/sha1.class.d.cts +9 -0
- package/dist/cjs/algo/sha256.class.d.cts +9 -0
- package/dist/cjs/algo/sha512.class.d.cts +9 -0
- package/dist/cjs/algo/triple-des.class.d.cts +16 -0
- package/dist/cjs/enc/base64.class.d.cts +31 -0
- package/dist/cjs/enc/encoder.model.d.cts +6 -0
- package/dist/cjs/enc/hex.class.d.cts +27 -0
- package/dist/cjs/enc/index.d.cts +6 -0
- package/dist/cjs/enc/latin1.class.d.cts +27 -0
- package/dist/cjs/enc/utf16.class.d.cts +28 -0
- package/dist/cjs/enc/utf8.class.d.cts +28 -0
- package/dist/cjs/format/formatter.interface.d.cts +5 -0
- package/dist/cjs/format/open-ssl.class.d.cts +27 -0
- package/dist/cjs/helpers/aes.helper.d.cts +2 -0
- package/dist/cjs/helpers/des.helper.d.cts +2 -0
- package/dist/cjs/helpers/index.d.cts +7 -0
- package/dist/cjs/helpers/md5.helper.d.cts +2 -0
- package/dist/cjs/helpers/sha1.helper.d.cts +2 -0
- package/dist/cjs/helpers/sha256.helper.d.cts +2 -0
- package/dist/cjs/helpers/sha512.helper.d.cts +2 -0
- package/dist/cjs/helpers/triple-des.helper.d.cts +2 -0
- package/dist/cjs/index.cjs +1 -0
- package/dist/cjs/index.d.cts +7 -0
- package/dist/cjs/kdf/evpkdf.class.d.cts +42 -0
- package/dist/cjs/kdf/index.d.cts +3 -0
- package/dist/cjs/kdf/kdf.model.d.cts +7 -0
- package/dist/cjs/kdf/open-ssl-kdf.class.d.cts +21 -0
- package/dist/cjs/kdf/pbkdf2.class.d.cts +18 -0
- package/dist/cjs/lib/block-cipher.class.d.cts +13 -0
- package/dist/cjs/lib/buffered-block-algorithm-config.interface.d.cts +13 -0
- package/dist/cjs/lib/buffered-block-algorithm.class.d.cts +54 -0
- package/dist/cjs/lib/cipher-helper.class.d.cts +10 -0
- package/dist/cjs/lib/cipher-params.class.d.cts +53 -0
- package/dist/cjs/lib/cipher.class.d.cts +112 -0
- package/dist/cjs/lib/hasher-constructor.type.d.cts +3 -0
- package/dist/cjs/lib/hasher-helper.type.d.cts +3 -0
- package/dist/cjs/lib/hasher.class.d.cts +58 -0
- package/dist/cjs/lib/hmac.class.d.cts +11 -0
- package/dist/cjs/lib/index.d.cts +8 -0
- package/dist/cjs/lib/password-based-cipher.class.d.cts +54 -0
- package/dist/cjs/lib/serializable-cipher.class.d.cts +73 -0
- package/dist/cjs/lib/word-array.class.d.cts +75 -0
- package/dist/cjs/lib/x64-word-array.class.d.cts +10 -0
- package/dist/cjs/lib/x64-word.class.d.cts +14 -0
- package/dist/cjs/mode/block-cipher-mode-algorithm.class.d.cts +20 -0
- package/dist/cjs/mode/block-cipher-mode.class.d.cts +28 -0
- package/dist/cjs/mode/cbc/cbc-decryptor.class.d.cts +17 -0
- package/dist/cjs/mode/cbc/cbc-encryptor.class.d.cts +17 -0
- package/dist/cjs/mode/cbc/cbc.class.d.cts +10 -0
- package/dist/cjs/mode/ctr/ctr-decryptor.class.d.cts +3 -0
- package/dist/cjs/mode/ctr/ctr-encryptor.class.d.cts +16 -0
- package/dist/cjs/mode/ctr/ctr.class.d.cts +10 -0
- package/dist/cjs/mode/ecb/ecb-decryptor.class.d.cts +14 -0
- package/dist/cjs/mode/ecb/ecb-encryptor.class.d.cts +14 -0
- package/dist/cjs/mode/ecb/ecb.class.d.cts +10 -0
- package/dist/cjs/mode/index.d.cts +11 -0
- package/dist/cjs/pad/index.d.cts +4 -0
- package/dist/cjs/pad/no-padding.class.d.cts +25 -0
- package/dist/cjs/pad/padding.model.d.cts +6 -0
- package/dist/cjs/pad/pkcs7.class.d.cts +25 -0
- package/dist/cjs/pad/zero-padding.class.d.cts +25 -0
- package/dist/esm/algo/aes.class.d.ts +16 -0
- package/dist/esm/algo/aes.class.js +138 -0
- package/dist/esm/algo/des.class.d.ts +19 -0
- package/dist/esm/algo/des.class.js +733 -0
- package/dist/esm/algo/index.d.ts +7 -0
- package/dist/esm/algo/index.js +7 -0
- package/dist/esm/algo/md5.class.d.ts +13 -0
- package/dist/esm/algo/md5.class.js +137 -0
- package/dist/esm/algo/sha1.class.d.ts +9 -0
- package/dist/esm/algo/sha1.class.js +66 -0
- package/dist/esm/algo/sha256.class.d.ts +9 -0
- package/dist/esm/algo/sha256.class.js +96 -0
- package/dist/esm/algo/sha512.class.d.ts +9 -0
- package/dist/esm/algo/sha512.class.js +221 -0
- package/dist/esm/algo/triple-des.class.d.ts +16 -0
- package/dist/esm/algo/triple-des.class.js +37 -0
- package/dist/esm/enc/base64.class.d.ts +31 -0
- package/dist/esm/enc/base64.class.js +80 -0
- package/dist/esm/enc/encoder.model.d.ts +6 -0
- package/dist/esm/enc/encoder.model.js +5 -0
- package/dist/esm/enc/hex.class.d.ts +27 -0
- package/dist/esm/enc/hex.class.js +45 -0
- package/dist/esm/enc/index.d.ts +6 -0
- package/dist/esm/enc/index.js +6 -0
- package/dist/esm/enc/latin1.class.d.ts +27 -0
- package/dist/esm/enc/latin1.class.js +44 -0
- package/dist/esm/enc/utf16.class.d.ts +28 -0
- package/dist/esm/enc/utf16.class.js +46 -0
- package/dist/esm/enc/utf8.class.d.ts +28 -0
- package/dist/esm/enc/utf8.class.js +39 -0
- package/dist/esm/format/formatter.interface.d.ts +5 -0
- package/dist/esm/format/formatter.interface.js +0 -0
- package/dist/esm/format/open-ssl.class.d.ts +27 -0
- package/dist/esm/format/open-ssl.class.js +57 -0
- package/dist/esm/helpers/aes.helper.d.ts +2 -0
- package/dist/esm/helpers/aes.helper.js +6 -0
- package/dist/esm/helpers/des.helper.d.ts +2 -0
- package/dist/esm/helpers/des.helper.js +6 -0
- package/dist/esm/helpers/index.d.ts +7 -0
- package/dist/esm/helpers/index.js +16 -0
- package/dist/esm/helpers/md5.helper.d.ts +2 -0
- package/dist/esm/helpers/md5.helper.js +6 -0
- package/dist/esm/helpers/sha1.helper.d.ts +2 -0
- package/dist/esm/helpers/sha1.helper.js +6 -0
- package/dist/esm/helpers/sha256.helper.d.ts +2 -0
- package/dist/esm/helpers/sha256.helper.js +6 -0
- package/dist/esm/helpers/sha512.helper.d.ts +2 -0
- package/dist/esm/helpers/sha512.helper.js +6 -0
- package/dist/esm/helpers/triple-des.helper.d.ts +2 -0
- package/dist/esm/helpers/triple-des.helper.js +6 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +43 -0
- package/dist/esm/kdf/evpkdf.class.d.ts +42 -0
- package/dist/esm/kdf/evpkdf.class.js +59 -0
- package/dist/esm/kdf/index.d.ts +3 -0
- package/dist/esm/kdf/index.js +3 -0
- package/dist/esm/kdf/kdf.model.d.ts +7 -0
- package/dist/esm/kdf/kdf.model.js +5 -0
- package/dist/esm/kdf/open-ssl-kdf.class.d.ts +21 -0
- package/dist/esm/kdf/open-ssl-kdf.class.js +32 -0
- package/dist/esm/kdf/pbkdf2.class.d.ts +18 -0
- package/dist/esm/kdf/pbkdf2.class.js +43 -0
- package/dist/esm/lib/block-cipher.class.d.ts +13 -0
- package/dist/esm/lib/block-cipher.class.js +55 -0
- package/dist/esm/lib/buffered-block-algorithm-config.interface.d.ts +13 -0
- package/dist/esm/lib/buffered-block-algorithm-config.interface.js +0 -0
- package/dist/esm/lib/buffered-block-algorithm.class.d.ts +54 -0
- package/dist/esm/lib/buffered-block-algorithm.class.js +99 -0
- package/dist/esm/lib/cipher-helper.class.d.ts +10 -0
- package/dist/esm/lib/cipher-helper.class.js +24 -0
- package/dist/esm/lib/cipher-params.class.d.ts +53 -0
- package/dist/esm/lib/cipher-params.class.js +89 -0
- package/dist/esm/lib/cipher.class.d.ts +112 -0
- package/dist/esm/lib/cipher.class.js +112 -0
- package/dist/esm/lib/hasher-constructor.type.d.ts +3 -0
- package/dist/esm/lib/hasher-constructor.type.js +0 -0
- package/dist/esm/lib/hasher-helper.type.d.ts +3 -0
- package/dist/esm/lib/hasher-helper.type.js +0 -0
- package/dist/esm/lib/hasher.class.d.ts +58 -0
- package/dist/esm/lib/hasher.class.js +77 -0
- package/dist/esm/lib/hmac.class.d.ts +11 -0
- package/dist/esm/lib/hmac.class.js +39 -0
- package/dist/esm/lib/index.d.ts +8 -0
- package/dist/esm/lib/index.js +18 -0
- package/dist/esm/lib/password-based-cipher.class.d.ts +54 -0
- package/dist/esm/lib/password-based-cipher.class.js +92 -0
- package/dist/esm/lib/serializable-cipher.class.d.ts +73 -0
- package/dist/esm/lib/serializable-cipher.class.js +108 -0
- package/dist/esm/lib/word-array.class.d.ts +75 -0
- package/dist/esm/lib/word-array.class.js +123 -0
- package/dist/esm/lib/x64-word-array.class.d.ts +10 -0
- package/dist/esm/lib/x64-word-array.class.js +25 -0
- package/dist/esm/lib/x64-word.class.d.ts +14 -0
- package/dist/esm/lib/x64-word.class.js +47 -0
- package/dist/esm/mode/block-cipher-mode-algorithm.class.d.ts +20 -0
- package/dist/esm/mode/block-cipher-mode-algorithm.class.js +22 -0
- package/dist/esm/mode/block-cipher-mode.class.d.ts +28 -0
- package/dist/esm/mode/block-cipher-mode.class.js +36 -0
- package/dist/esm/mode/cbc/cbc-decryptor.class.d.ts +17 -0
- package/dist/esm/mode/cbc/cbc-decryptor.class.js +40 -0
- package/dist/esm/mode/cbc/cbc-encryptor.class.d.ts +17 -0
- package/dist/esm/mode/cbc/cbc-encryptor.class.js +39 -0
- package/dist/esm/mode/cbc/cbc.class.d.ts +10 -0
- package/dist/esm/mode/cbc/cbc.class.js +10 -0
- package/dist/esm/mode/ctr/ctr-decryptor.class.d.ts +3 -0
- package/dist/esm/mode/ctr/ctr-decryptor.class.js +6 -0
- package/dist/esm/mode/ctr/ctr-encryptor.class.d.ts +16 -0
- package/dist/esm/mode/ctr/ctr-encryptor.class.js +33 -0
- package/dist/esm/mode/ctr/ctr.class.d.ts +10 -0
- package/dist/esm/mode/ctr/ctr.class.js +10 -0
- package/dist/esm/mode/ecb/ecb-decryptor.class.d.ts +14 -0
- package/dist/esm/mode/ecb/ecb-decryptor.class.js +19 -0
- package/dist/esm/mode/ecb/ecb-encryptor.class.d.ts +14 -0
- package/dist/esm/mode/ecb/ecb-encryptor.class.js +19 -0
- package/dist/esm/mode/ecb/ecb.class.d.ts +10 -0
- package/dist/esm/mode/ecb/ecb.class.js +10 -0
- package/dist/esm/mode/index.d.ts +11 -0
- package/dist/esm/mode/index.js +24 -0
- package/dist/esm/pad/index.d.ts +4 -0
- package/dist/esm/pad/index.js +8 -0
- package/dist/esm/pad/no-padding.class.d.ts +25 -0
- package/dist/esm/pad/no-padding.class.js +28 -0
- package/dist/esm/pad/padding.model.d.ts +6 -0
- package/dist/esm/pad/padding.model.js +5 -0
- package/dist/esm/pad/pkcs7.class.d.ts +25 -0
- package/dist/esm/pad/pkcs7.class.js +40 -0
- package/dist/esm/pad/zero-padding.class.d.ts +25 -0
- package/dist/esm/pad/zero-padding.class.js +38 -0
- package/package.json +82 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { WordArray } from "./word-array.class.js";
|
|
2
|
+
import { OpenSSL } from "../format/open-ssl.class.js";
|
|
3
|
+
import { CipherParams } from "./cipher-params.class.js";
|
|
4
|
+
class SerializableCipher {
|
|
5
|
+
/**
|
|
6
|
+
* Decrypts serialized ciphertext.
|
|
7
|
+
*
|
|
8
|
+
* @param cipher The cipher algorithm to use.
|
|
9
|
+
* @param ciphertext The ciphertext to decrypt.
|
|
10
|
+
* @param key The key.
|
|
11
|
+
* @param optionalCfg The configuration options to use for this operation.
|
|
12
|
+
*
|
|
13
|
+
* @return The plaintext.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
*
|
|
17
|
+
* let plaintext = SerializableCipher.decrypt(
|
|
18
|
+
* AESAlgorithm,
|
|
19
|
+
* formattedCiphertext,
|
|
20
|
+
* key, {
|
|
21
|
+
* iv: iv,
|
|
22
|
+
* format: CryptoJS.format.OpenSSL
|
|
23
|
+
* }
|
|
24
|
+
* );
|
|
25
|
+
*
|
|
26
|
+
* let plaintext = SerializableCipher.decrypt(
|
|
27
|
+
* AESAlgorithm,
|
|
28
|
+
* ciphertextParams,
|
|
29
|
+
* key, {
|
|
30
|
+
* iv: iv,
|
|
31
|
+
* format: CryptoJS.format.OpenSSL
|
|
32
|
+
* }
|
|
33
|
+
* );
|
|
34
|
+
*/
|
|
35
|
+
static decrypt(cipher, ciphertext, key, optionalCfg) {
|
|
36
|
+
const cfg = Object.assign({}, this.cfg, optionalCfg);
|
|
37
|
+
if (!cfg.format) {
|
|
38
|
+
throw new Error("could not determine format");
|
|
39
|
+
}
|
|
40
|
+
ciphertext = this._parse(ciphertext, cfg.format);
|
|
41
|
+
if (!ciphertext.ciphertext) {
|
|
42
|
+
throw new Error("could not determine ciphertext");
|
|
43
|
+
}
|
|
44
|
+
return cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Encrypts a message.
|
|
48
|
+
*
|
|
49
|
+
* @param cipher The cipher algorithm to use.
|
|
50
|
+
* @param message The message to encrypt.
|
|
51
|
+
* @param key The key.
|
|
52
|
+
* @param cfg (Optional) The configuration options to use for this operation.
|
|
53
|
+
*
|
|
54
|
+
* @return A cipher params object.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
*
|
|
58
|
+
* let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
|
|
59
|
+
* let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
|
|
60
|
+
* let ciphertextParams = SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, {
|
|
61
|
+
* iv: iv,
|
|
62
|
+
* format: CryptoJS.format.OpenSSL
|
|
63
|
+
* });
|
|
64
|
+
*/
|
|
65
|
+
static encrypt(cipher, message, key, cfg) {
|
|
66
|
+
const config = Object.assign({}, this.cfg, cfg);
|
|
67
|
+
const encryptor = cipher.createEncryptor(key, config);
|
|
68
|
+
const ciphertext = encryptor.finalize(message);
|
|
69
|
+
return new CipherParams({
|
|
70
|
+
ciphertext,
|
|
71
|
+
key,
|
|
72
|
+
iv: encryptor.cfg.iv,
|
|
73
|
+
algorithm: cipher,
|
|
74
|
+
mode: encryptor.cfg.mode,
|
|
75
|
+
padding: encryptor.cfg.padding,
|
|
76
|
+
blockSize: encryptor.cfg.blockSize,
|
|
77
|
+
formatter: config.format
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Converts serialized ciphertext to CipherParams,
|
|
82
|
+
* else assumed CipherParams already and returns ciphertext unchanged.
|
|
83
|
+
*
|
|
84
|
+
* @param ciphertext The ciphertext.
|
|
85
|
+
* @param format The formatting strategy to use to parse serialized ciphertext.
|
|
86
|
+
*
|
|
87
|
+
* @return The deserialized ciphertext.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
*
|
|
91
|
+
* var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
|
|
92
|
+
*/
|
|
93
|
+
static _parse(ciphertext, format) {
|
|
94
|
+
if (typeof ciphertext === "string") {
|
|
95
|
+
return format.parse(ciphertext);
|
|
96
|
+
} else {
|
|
97
|
+
return ciphertext;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
SerializableCipher.cfg = {
|
|
102
|
+
blockSize: 4,
|
|
103
|
+
iv: new WordArray([]),
|
|
104
|
+
format: OpenSSL
|
|
105
|
+
};
|
|
106
|
+
export {
|
|
107
|
+
SerializableCipher
|
|
108
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export declare class WordArray {
|
|
2
|
+
sigBytes: number;
|
|
3
|
+
words: number[];
|
|
4
|
+
/**
|
|
5
|
+
* Initializes a newly created word array.
|
|
6
|
+
*
|
|
7
|
+
* @param words (Optional) An array of 32-bit words.
|
|
8
|
+
* @param sigBytes (Optional) The number of significant bytes in the words.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* let wordArray = new WordArray();
|
|
13
|
+
* let wordArray = new WordArray([0x00010203, 0x04050607]);
|
|
14
|
+
* let wordArray = new WordArray([0x00010203, 0x04050607], 6);
|
|
15
|
+
*/
|
|
16
|
+
constructor(words?: number[], sigBytes?: number);
|
|
17
|
+
/**
|
|
18
|
+
* Creates a word array filled with random bytes.
|
|
19
|
+
*
|
|
20
|
+
* @param nBytes The number of random bytes to generate.
|
|
21
|
+
*
|
|
22
|
+
* @return The random word array.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
*
|
|
26
|
+
* let wordArray = WordArray.random(16);
|
|
27
|
+
*/
|
|
28
|
+
static random(nBytes: number): WordArray;
|
|
29
|
+
/**
|
|
30
|
+
* Removes insignificant bits.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
*
|
|
34
|
+
* wordArray.clamp();
|
|
35
|
+
*/
|
|
36
|
+
clamp(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a copy of this word array.
|
|
39
|
+
*
|
|
40
|
+
* @return The clone.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
*
|
|
44
|
+
* let clone = wordArray.clone();
|
|
45
|
+
*/
|
|
46
|
+
clone(): WordArray;
|
|
47
|
+
/**
|
|
48
|
+
* Concatenates a word array to this word array.
|
|
49
|
+
*
|
|
50
|
+
* @param wordArray The word array to append.
|
|
51
|
+
*
|
|
52
|
+
* @return This word array.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
*
|
|
56
|
+
* wordArray1.concat(wordArray2);
|
|
57
|
+
*/
|
|
58
|
+
concat(wordArray: WordArray): WordArray;
|
|
59
|
+
/**
|
|
60
|
+
* Converts this word array to a string.
|
|
61
|
+
*
|
|
62
|
+
* @param encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
|
|
63
|
+
*
|
|
64
|
+
* @return The stringified word array.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* let string = wordArray + '';
|
|
69
|
+
* let string = wordArray.toString();
|
|
70
|
+
* let string = wordArray.toString(CryptoJS.enc.Utf8);
|
|
71
|
+
*/
|
|
72
|
+
toString<T extends {
|
|
73
|
+
stringify: (arg: WordArray) => string;
|
|
74
|
+
}>(encoder?: T): string;
|
|
75
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Hex } from "../enc/hex.class.js";
|
|
2
|
+
class WordArray {
|
|
3
|
+
/**
|
|
4
|
+
* Initializes a newly created word array.
|
|
5
|
+
*
|
|
6
|
+
* @param words (Optional) An array of 32-bit words.
|
|
7
|
+
* @param sigBytes (Optional) The number of significant bytes in the words.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* let wordArray = new WordArray();
|
|
12
|
+
* let wordArray = new WordArray([0x00010203, 0x04050607]);
|
|
13
|
+
* let wordArray = new WordArray([0x00010203, 0x04050607], 6);
|
|
14
|
+
*/
|
|
15
|
+
constructor(words, sigBytes) {
|
|
16
|
+
this.words = words || [];
|
|
17
|
+
if (sigBytes !== void 0) {
|
|
18
|
+
this.sigBytes = sigBytes;
|
|
19
|
+
} else {
|
|
20
|
+
this.sigBytes = this.words.length * 4;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates a word array filled with random bytes.
|
|
25
|
+
*
|
|
26
|
+
* @param nBytes The number of random bytes to generate.
|
|
27
|
+
*
|
|
28
|
+
* @return The random word array.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* let wordArray = WordArray.random(16);
|
|
33
|
+
*/
|
|
34
|
+
static random(nBytes) {
|
|
35
|
+
const words = [];
|
|
36
|
+
const r = function(mw) {
|
|
37
|
+
let mz = 987654321;
|
|
38
|
+
const mask = 4294967295;
|
|
39
|
+
return function() {
|
|
40
|
+
mz = 36969 * (mz & 65535) + (mz >> 16) & mask;
|
|
41
|
+
mw = 18e3 * (mw & 65535) + (mw >> 16) & mask;
|
|
42
|
+
let result = (mz << 16) + mw & mask;
|
|
43
|
+
result /= 4294967296;
|
|
44
|
+
result += 0.5;
|
|
45
|
+
return result * (Math.random() > 0.5 ? 1 : -1);
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
for (let i = 0, rcache; i < nBytes; i += 4) {
|
|
49
|
+
const rnd1 = r((rcache || Math.random()) * 4294967296);
|
|
50
|
+
rcache = rnd1() * 987654071;
|
|
51
|
+
words.push(rnd1() * 4294967296 | 0);
|
|
52
|
+
}
|
|
53
|
+
return new WordArray(words, nBytes);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Removes insignificant bits.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
*
|
|
60
|
+
* wordArray.clamp();
|
|
61
|
+
*/
|
|
62
|
+
clamp() {
|
|
63
|
+
this.words[this.sigBytes >>> 2] &= 4294967295 << 32 - this.sigBytes % 4 * 8;
|
|
64
|
+
this.words.length = Math.ceil(this.sigBytes / 4);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Creates a copy of this word array.
|
|
68
|
+
*
|
|
69
|
+
* @return The clone.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
*
|
|
73
|
+
* let clone = wordArray.clone();
|
|
74
|
+
*/
|
|
75
|
+
clone() {
|
|
76
|
+
return new WordArray(this.words.slice(0), this.sigBytes);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Concatenates a word array to this word array.
|
|
80
|
+
*
|
|
81
|
+
* @param wordArray The word array to append.
|
|
82
|
+
*
|
|
83
|
+
* @return This word array.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
*
|
|
87
|
+
* wordArray1.concat(wordArray2);
|
|
88
|
+
*/
|
|
89
|
+
concat(wordArray) {
|
|
90
|
+
this.clamp();
|
|
91
|
+
if (this.sigBytes % 4) {
|
|
92
|
+
for (let i = 0; i < wordArray.sigBytes; i++) {
|
|
93
|
+
const thatByte = wordArray.words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
|
|
94
|
+
this.words[this.sigBytes + i >>> 2] |= thatByte << 24 - (this.sigBytes + i) % 4 * 8;
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
for (let i = 0; i < wordArray.sigBytes; i += 4) {
|
|
98
|
+
this.words[this.sigBytes + i >>> 2] = wordArray.words[i >>> 2];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
this.sigBytes += wordArray.sigBytes;
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Converts this word array to a string.
|
|
106
|
+
*
|
|
107
|
+
* @param encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
|
|
108
|
+
*
|
|
109
|
+
* @return The stringified word array.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
*
|
|
113
|
+
* let string = wordArray + '';
|
|
114
|
+
* let string = wordArray.toString();
|
|
115
|
+
* let string = wordArray.toString(CryptoJS.enc.Utf8);
|
|
116
|
+
*/
|
|
117
|
+
toString(encoder) {
|
|
118
|
+
return (encoder || Hex).stringify(this);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
export {
|
|
122
|
+
WordArray
|
|
123
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { X64Word } from "./x64-word.class.js";
|
|
2
|
+
import { WordArray } from "./word-array.class.js";
|
|
3
|
+
export declare class X64WordArray {
|
|
4
|
+
sigBytes: number;
|
|
5
|
+
words: X64Word[];
|
|
6
|
+
constructor(words?: X64Word[], sigBytes?: number);
|
|
7
|
+
static create(words?: X64Word[], sigBytes?: number): X64WordArray;
|
|
8
|
+
clone(): X64WordArray;
|
|
9
|
+
toX32(): WordArray;
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { X64Word } from "./x64-word.class.js";
|
|
2
|
+
import { WordArray } from "./word-array.class.js";
|
|
3
|
+
class X64WordArray {
|
|
4
|
+
constructor(words = [], sigBytes) {
|
|
5
|
+
this.words = words;
|
|
6
|
+
this.sigBytes = sigBytes !== void 0 ? sigBytes : words.length * 8;
|
|
7
|
+
}
|
|
8
|
+
static create(words = [], sigBytes) {
|
|
9
|
+
return new X64WordArray(words, sigBytes);
|
|
10
|
+
}
|
|
11
|
+
clone() {
|
|
12
|
+
const clonedWords = this.words.map((word) => new X64Word(word.high, word.low));
|
|
13
|
+
return new X64WordArray(clonedWords, this.sigBytes);
|
|
14
|
+
}
|
|
15
|
+
toX32() {
|
|
16
|
+
const x32Words = [];
|
|
17
|
+
for (const word of this.words) {
|
|
18
|
+
x32Words.push(word.high, word.low);
|
|
19
|
+
}
|
|
20
|
+
return new WordArray(x32Words, this.sigBytes);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
X64WordArray
|
|
25
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare class X64Word {
|
|
2
|
+
high: number;
|
|
3
|
+
low: number;
|
|
4
|
+
constructor(high: number, low: number);
|
|
5
|
+
add(word: X64Word): X64Word;
|
|
6
|
+
and(word: X64Word): X64Word;
|
|
7
|
+
not(): X64Word;
|
|
8
|
+
or(word: X64Word): X64Word;
|
|
9
|
+
rotL(n: number): X64Word;
|
|
10
|
+
rotR(n: number): X64Word;
|
|
11
|
+
shiftL(n: number): X64Word;
|
|
12
|
+
shiftR(n: number): X64Word;
|
|
13
|
+
xor(word: X64Word): X64Word;
|
|
14
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
class X64Word {
|
|
2
|
+
constructor(high, low) {
|
|
3
|
+
this.high = high;
|
|
4
|
+
this.low = low;
|
|
5
|
+
}
|
|
6
|
+
add(word) {
|
|
7
|
+
const low = this.low + word.low | 0;
|
|
8
|
+
const carry = low >>> 0 < this.low >>> 0 ? 1 : 0;
|
|
9
|
+
const high = this.high + word.high + carry | 0;
|
|
10
|
+
return new X64Word(high, low);
|
|
11
|
+
}
|
|
12
|
+
and(word) {
|
|
13
|
+
return new X64Word(this.high & word.high, this.low & word.low);
|
|
14
|
+
}
|
|
15
|
+
not() {
|
|
16
|
+
return new X64Word(~this.high, ~this.low);
|
|
17
|
+
}
|
|
18
|
+
or(word) {
|
|
19
|
+
return new X64Word(this.high | word.high, this.low | word.low);
|
|
20
|
+
}
|
|
21
|
+
rotL(n) {
|
|
22
|
+
return this.shiftL(n).or(this.shiftR(64 - n));
|
|
23
|
+
}
|
|
24
|
+
rotR(n) {
|
|
25
|
+
return this.shiftR(n).or(this.shiftL(64 - n));
|
|
26
|
+
}
|
|
27
|
+
shiftL(n) {
|
|
28
|
+
if (n < 32) {
|
|
29
|
+
return new X64Word(this.high << n | this.low >>> 32 - n, this.low << n);
|
|
30
|
+
} else {
|
|
31
|
+
return new X64Word(this.low << n - 32, 0);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
shiftR(n) {
|
|
35
|
+
if (n < 32) {
|
|
36
|
+
return new X64Word(this.high >>> n, this.low >>> n | this.high << 32 - n);
|
|
37
|
+
} else {
|
|
38
|
+
return new X64Word(0, this.high >>> n - 32);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
xor(word) {
|
|
42
|
+
return new X64Word(this.high ^ word.high, this.low ^ word.low);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export {
|
|
46
|
+
X64Word
|
|
47
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { BlockCipher } from "../lib/block-cipher.class.js";
|
|
2
|
+
import { BlockCipherMode } from "./block-cipher-mode.class.js";
|
|
3
|
+
export declare abstract class BlockCipherModeAlgorithm {
|
|
4
|
+
__creator: ((cipher: BlockCipher, iv: number[]) => BlockCipherMode) | undefined;
|
|
5
|
+
protected _cipher: BlockCipher;
|
|
6
|
+
protected _iv: number[] | undefined;
|
|
7
|
+
constructor(cipher: BlockCipher, iv: number[]);
|
|
8
|
+
abstract processBlock(words: number[], offset: number): void;
|
|
9
|
+
/**
|
|
10
|
+
* Initializes a newly created mode.
|
|
11
|
+
*
|
|
12
|
+
* @param cipher A block cipher instance.
|
|
13
|
+
* @param iv The IV words.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
*
|
|
17
|
+
* var mode = CBC.Encryptor.create(cipher, iv.words);
|
|
18
|
+
*/
|
|
19
|
+
init(cipher: BlockCipher, iv?: number[]): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
class BlockCipherModeAlgorithm {
|
|
2
|
+
constructor(cipher, iv) {
|
|
3
|
+
this.init(cipher, iv);
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Initializes a newly created mode.
|
|
7
|
+
*
|
|
8
|
+
* @param cipher A block cipher instance.
|
|
9
|
+
* @param iv The IV words.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* var mode = CBC.Encryptor.create(cipher, iv.words);
|
|
14
|
+
*/
|
|
15
|
+
init(cipher, iv) {
|
|
16
|
+
this._cipher = cipher;
|
|
17
|
+
this._iv = iv;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
BlockCipherModeAlgorithm
|
|
22
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { BlockCipher } from "../lib/block-cipher.class.js";
|
|
2
|
+
import { BlockCipherModeAlgorithm } from "./block-cipher-mode-algorithm.class.js";
|
|
3
|
+
export declare abstract class BlockCipherMode {
|
|
4
|
+
static Decryptor: any;
|
|
5
|
+
static Encryptor: any;
|
|
6
|
+
/**
|
|
7
|
+
* Creates this mode for decryption.
|
|
8
|
+
*
|
|
9
|
+
* @param cipher A block cipher instance.
|
|
10
|
+
* @param iv The IV words.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* const mode = CBC.createDecryptor(cipher, iv.words);
|
|
15
|
+
*/
|
|
16
|
+
static createDecryptor(cipher: BlockCipher, iv: number[]): BlockCipherModeAlgorithm;
|
|
17
|
+
/**
|
|
18
|
+
* Creates this mode for encryption.
|
|
19
|
+
*
|
|
20
|
+
* @param cipher A block cipher instance.
|
|
21
|
+
* @param iv The IV words.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
*
|
|
25
|
+
* var mode = CBC.createEncryptor(cipher, iv.words);
|
|
26
|
+
*/
|
|
27
|
+
static createEncryptor(cipher: BlockCipher, iv: number[]): BlockCipherModeAlgorithm;
|
|
28
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { BlockCipherModeAlgorithm } from "./block-cipher-mode-algorithm.class.js";
|
|
2
|
+
class BlockCipherMode {
|
|
3
|
+
/**
|
|
4
|
+
* Creates this mode for decryption.
|
|
5
|
+
*
|
|
6
|
+
* @param cipher A block cipher instance.
|
|
7
|
+
* @param iv The IV words.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* const mode = CBC.createDecryptor(cipher, iv.words);
|
|
12
|
+
*/
|
|
13
|
+
static createDecryptor(cipher, iv) {
|
|
14
|
+
const decryptorClass = this.Decryptor;
|
|
15
|
+
return new decryptorClass(cipher, iv);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates this mode for encryption.
|
|
19
|
+
*
|
|
20
|
+
* @param cipher A block cipher instance.
|
|
21
|
+
* @param iv The IV words.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
*
|
|
25
|
+
* var mode = CBC.createEncryptor(cipher, iv.words);
|
|
26
|
+
*/
|
|
27
|
+
static createEncryptor(cipher, iv) {
|
|
28
|
+
const encryptorClass = this.Encryptor;
|
|
29
|
+
return new encryptorClass(cipher, iv);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
BlockCipherMode.Decryptor = BlockCipherModeAlgorithm;
|
|
33
|
+
BlockCipherMode.Encryptor = BlockCipherModeAlgorithm;
|
|
34
|
+
export {
|
|
35
|
+
BlockCipherMode
|
|
36
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
|
|
2
|
+
export declare class CBCDecryptor extends BlockCipherModeAlgorithm {
|
|
3
|
+
get prevBlock(): number[];
|
|
4
|
+
protected _prevBlock: number[] | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Processes the data block at offset.
|
|
7
|
+
*
|
|
8
|
+
* @param words The data words to operate on.
|
|
9
|
+
* @param offset The offset where the block starts.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* mode.processBlock(data.words, offset);
|
|
14
|
+
*/
|
|
15
|
+
processBlock(words: number[], offset: number): void;
|
|
16
|
+
xorBlock(words: number[], offset: number, blockSize: number): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
|
|
2
|
+
class CBCDecryptor extends BlockCipherModeAlgorithm {
|
|
3
|
+
get prevBlock() {
|
|
4
|
+
return this._prevBlock;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Processes the data block at offset.
|
|
8
|
+
*
|
|
9
|
+
* @param words The data words to operate on.
|
|
10
|
+
* @param offset The offset where the block starts.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* mode.processBlock(data.words, offset);
|
|
15
|
+
*/
|
|
16
|
+
processBlock(words, offset) {
|
|
17
|
+
if (this._cipher.cfg.blockSize === void 0) {
|
|
18
|
+
throw new Error("missing blockSize in cipher config");
|
|
19
|
+
}
|
|
20
|
+
const thisBlock = words.slice(offset, offset + this._cipher.cfg.blockSize);
|
|
21
|
+
this._cipher.decryptBlock(words, offset);
|
|
22
|
+
this.xorBlock(words, offset, this._cipher.cfg.blockSize);
|
|
23
|
+
this._prevBlock = thisBlock;
|
|
24
|
+
}
|
|
25
|
+
xorBlock(words, offset, blockSize) {
|
|
26
|
+
let block;
|
|
27
|
+
if (this._iv) {
|
|
28
|
+
block = this._iv;
|
|
29
|
+
this._iv = void 0;
|
|
30
|
+
} else {
|
|
31
|
+
block = this._prevBlock;
|
|
32
|
+
}
|
|
33
|
+
for (let i = 0; i < blockSize; i++) {
|
|
34
|
+
words[offset + i] ^= block[i];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
CBCDecryptor
|
|
40
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
|
|
2
|
+
export declare class CBCEncryptor extends BlockCipherModeAlgorithm {
|
|
3
|
+
get prevBlock(): number[];
|
|
4
|
+
protected _prevBlock: number[] | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Processes the data block at offset.
|
|
7
|
+
*
|
|
8
|
+
* @param words The data words to operate on.
|
|
9
|
+
* @param offset The offset where the block starts.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* mode.processBlock(data.words, offset);
|
|
14
|
+
*/
|
|
15
|
+
processBlock(words: number[], offset: number): void;
|
|
16
|
+
xorBlock(words: number[], offset: number, blockSize: number): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
|
|
2
|
+
class CBCEncryptor extends BlockCipherModeAlgorithm {
|
|
3
|
+
get prevBlock() {
|
|
4
|
+
return this._prevBlock;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Processes the data block at offset.
|
|
8
|
+
*
|
|
9
|
+
* @param words The data words to operate on.
|
|
10
|
+
* @param offset The offset where the block starts.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* mode.processBlock(data.words, offset);
|
|
15
|
+
*/
|
|
16
|
+
processBlock(words, offset) {
|
|
17
|
+
if (this._cipher.cfg.blockSize === void 0) {
|
|
18
|
+
throw new Error("missing blockSize in cipher config");
|
|
19
|
+
}
|
|
20
|
+
this.xorBlock(words, offset, this._cipher.cfg.blockSize);
|
|
21
|
+
this._cipher.encryptBlock(words, offset);
|
|
22
|
+
this._prevBlock = words.slice(offset, offset + this._cipher.cfg.blockSize);
|
|
23
|
+
}
|
|
24
|
+
xorBlock(words, offset, blockSize) {
|
|
25
|
+
let block;
|
|
26
|
+
if (this._iv) {
|
|
27
|
+
block = this._iv;
|
|
28
|
+
this._iv = void 0;
|
|
29
|
+
} else {
|
|
30
|
+
block = this._prevBlock;
|
|
31
|
+
}
|
|
32
|
+
for (let i = 0; i < blockSize; i++) {
|
|
33
|
+
words[offset + i] ^= block[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
CBCEncryptor
|
|
39
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BlockCipherMode } from "../block-cipher-mode.class.js";
|
|
2
|
+
import { CBCEncryptor } from "./cbc-encryptor.class.js";
|
|
3
|
+
import { CBCDecryptor } from "./cbc-decryptor.class.js";
|
|
4
|
+
/**
|
|
5
|
+
* Cipher Block Chaining mode.
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class CBC extends BlockCipherMode {
|
|
8
|
+
static Decryptor: typeof CBCDecryptor;
|
|
9
|
+
static Encryptor: typeof CBCEncryptor;
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BlockCipherMode } from "../block-cipher-mode.class.js";
|
|
2
|
+
import { CBCEncryptor } from "./cbc-encryptor.class.js";
|
|
3
|
+
import { CBCDecryptor } from "./cbc-decryptor.class.js";
|
|
4
|
+
class CBC extends BlockCipherMode {
|
|
5
|
+
}
|
|
6
|
+
CBC.Decryptor = CBCDecryptor;
|
|
7
|
+
CBC.Encryptor = CBCEncryptor;
|
|
8
|
+
export {
|
|
9
|
+
CBC
|
|
10
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { BlockCipherModeAlgorithm } from "../block-cipher-mode-algorithm.class.js";
|
|
2
|
+
export declare class CTREncryptor extends BlockCipherModeAlgorithm {
|
|
3
|
+
protected _counter: number[];
|
|
4
|
+
protected _prevBlock: number[] | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Processes the data block at offset.
|
|
7
|
+
*
|
|
8
|
+
* @param words The data words to operate on.
|
|
9
|
+
* @param offset The offset where the block starts.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* mode.processBlock(data.words, offset);
|
|
14
|
+
*/
|
|
15
|
+
processBlock(words: number[], offset: number): void;
|
|
16
|
+
}
|