CipherParams.js
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { OpenSSLFormatter } from "./formatter/OpenSSLFormatter";
/**
* A collection of cipher parameters.
*
* @property {Word32Array} ciphertext The raw ciphertext.
* @property {Word32Array} key The key to this ciphertext.
* @property {Word32Array} iv The IV used in the ciphering operation.
* @property {Word32Array} salt The salt used with a key derivation function.
* @property {typeof Cipher} algorithm The cipher algorithm.
* @property {BlockCipherMode} mode The block mode used in the ciphering operation.
* @property {Pad} padding The padding scheme used in the ciphering operation.
* @property {number} blockSize The block size of the cipher.
* @property {Formatter} formatter The default formatting strategy to convert this cipher params object to a string.
*/
export class CipherParams {
/**
* Initializes a newly created cipher params object.
*
* @param {Partial<CipherParams>} cp An object with any of the possible cipher parameters.
* @example
* var cipherParams = new CipherParams({
* ciphertext: ciphertextWordArray,
* key: keyWordArray,
* iv: ivWordArray,
* salt: saltWordArray,
* algorithm: AES,
* mode: CBC,
* padding: PKCS7,
* blockSize: 4,
* formatter: OpenSSLFormatter
* });
*/
constructor(cp) {
this.formatter = OpenSSLFormatter;
if (cp) {
this.cipherText = cp.cipherText;
this.key = cp.key;
this.iv = cp.iv;
this.salt = cp.salt;
this.Algorithm = cp.Algorithm;
this.mode = cp.mode;
this.padding = cp.padding;
this.blockSize = cp.blockSize;
this.formatter = cp.formatter || OpenSSLFormatter;
}
}
/**
* Converts this cipher params object to a string.
*
* @param {Formatter?} formatter (Optional) The formatting strategy to use.
* @return {string} The stringified cipher params.
* @throws Error If neither the formatter nor the default formatter is set.
* @example
* var string = cipherParams + '';
* var string = cipherParams.toString();
* var string = cipherParams.toString(OpenSSLFormatter);
*/
toString(formatter) {
return (formatter || this.formatter).stringify(this);
}
}