Cipher.js
3.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { BufferedBlockAlgorithm } from "../BufferedBlockAlgorithm";
export class Cipher extends BufferedBlockAlgorithm {
constructor(props) {
super(props);
this._transformMode = 1;
this._props = props;
this._key = props.key;
this._iv = typeof props.iv !== "undefined" ? props.iv : this._iv;
this._transformMode = typeof props.transformMode !== "undefined" ? props.transformMode : this._transformMode;
}
get iv() {
return this._iv;
}
/**
* Resets this cipher to its initial state.
* @example
* cipher.reset();
*/
reset(data, nBytes) {
super.reset(data, nBytes);
this._doReset();
}
/**
* Adds data to be encrypted or decrypted.
* @param {Word32Array|string} dataUpdate The data to encrypt or decrypt.
* @return {Word32Array} The data after processing.
* @example
* var encrypted = cipher.process('data');
* var encrypted = cipher.process(wordArray);
*/
process(dataUpdate) {
this._append(dataUpdate);
return this._process();
}
/**
* Finalizes the encryption or decryption process.
* Note that the finalize operation is effectively a destructive, read-once operation.
* @param {Word32Array|string?} dataUpdate The final data to encrypt or decrypt.
* @return {Word32Array} The data after final processing.
* @example
* var encrypted = cipher.finalize();
* var encrypted = cipher.finalize('data');
* var encrypted = cipher.finalize(wordArray);
*/
finalize(dataUpdate) {
// Final data update
if (dataUpdate) {
this._append(dataUpdate);
}
// Perform concrete-cipher logic
return this._doFinalize();
}
/**
* @abstract
*/
_doReset() {
throw new Error("Not implemented");
}
/**
* @abstract
*/
_doProcessBlock(words, offset) {
throw new Error("Not implemented");
}
/**
* @abstract
*/
_doFinalize() {
throw new Error("Not implemented");
}
/**
* Creates this cipher in encryption mode.
*
* @param {Word32Array} key The key.
* @param {Partial<CipherProps>?} props (Optional) The configuration options to use for this operation.
* @return {Cipher} A cipher instance.
* @example
* var cipher = AES.createEncryptor(keyWordArray, { iv: ivWordArray });
*/
static createEncryptor(key, props) {
props = typeof props === "undefined" ? {} : props;
return new Cipher(Object.assign(Object.assign({}, props), { key, transformMode: Cipher.ENC_TRANSFORM_MODE }));
}
/**
* Creates this cipher in decryption mode.
* @param {Word32Array} key The key.
* @param {Partial<CipherProps>?} props (Optional) The configuration options to use for this operation.
* @return {Cipher} A cipher instance.
* @example
* var cipher = AES.createDecryptor(keyWordArray, { iv: ivWordArray });
*/
static createDecryptor(key, props) {
props = typeof props === "undefined" ? {} : props;
return new Cipher(Object.assign(Object.assign({}, props), { key, transformMode: Cipher.DEC_TRANSFORM_MODE }));
}
}
Cipher.ENC_TRANSFORM_MODE = 1;
Cipher.DEC_TRANSFORM_MODE = 2;
Cipher.keySize = 128 / 32;
Cipher.ivSize = 128 / 32;