-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAES.js
156 lines (132 loc) · 3.91 KB
/
AES.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// const CryptoJS = require("crypto-js");
// const Convert = require('./Convert.js');
import cryptoJs from "crypto-js";
const CryptoJS = cryptoJs;
export default class AES {
constructor(params = {}) {
this._key = params.key || null;
this._password = params.password || null;
this._kdfSalt = params.kdfSalt || 'todo';
this._iv = params.iv || null;
if (!this._key && this._password) {
this.genKeyFromPassword();
}
if (this._key.constructor === Uint8Array) {
// It's a Uint8Array
this.getKeyFromUint8Array();
}
if (!this._iv) {
this.genIV();
} else if (!this._iv.words) {
this._iv = AES.convertUint8ArrayToWordArray(this._iv);
}
// console.error('AES', this._key, this._password, this._iv);
}
get ivByteLength() {
return AES.ivByteLength();
}
static ivByteLength() {
return (CryptoJS.algo.AES.ivSize * 4);
}
get iv() {
return AES.cryptJsWordArrayToUint8Array(this._iv);
}
getCryptoParams() {
return {
mode: CryptoJS.mode.CTR,
padding: CryptoJS.pad.NoPadding, // CTR + NoPadding = same size of cipher and input
iv: this._iv,
};
}
genKeyFromPassword() {
this._key = CryptoJS.PBKDF2(this._password, this._kdfSalt, { keySize: CryptoJS.algo.AES.keySize, iterations: 1000 });
}
getKeyFromUint8Array() {
this._key = CryptoJS.PBKDF2(this._key.join(''), this._kdfSalt, { keySize: CryptoJS.algo.AES.keySize, iterations: 1000 });
}
genIV() {
const salt = CryptoJS.lib.WordArray.random(CryptoJS.algo.AES.ivSize);
this._iv = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: CryptoJS.algo.AES.ivSize, iterations: 1000 });
this._iv.clamp();
}
encrypt(input, finalize = false) {
const wordArray = input ? AES.convertUint8ArrayToWordArray(input) : null;
if (!this._encryptor) {
this._encryptor = CryptoJS.algo.AES.createEncryptor(this._key, this.getCryptoParams());
}
if (finalize) {
if (wordArray) {
return AES.cryptJsWordArrayToUint8Array(this._encryptor.finalize(wordArray));
} else {
return AES.cryptJsWordArrayToUint8Array(this._encryptor.finalize());
}
} else {
return AES.cryptJsWordArrayToUint8Array(this._encryptor.process(wordArray));
}
}
decrypt(input, finalize = false) {
const wordArray = input ? AES.convertUint8ArrayToWordArray(input) : null;
if (!this._decryptor) {
this._decryptor = CryptoJS.algo.AES.createDecryptor(this._key, this.getCryptoParams());
}
if (finalize) {
if (wordArray) {
return AES.cryptJsWordArrayToUint8Array(this._decryptor.finalize(wordArray));
} else {
return AES.cryptJsWordArrayToUint8Array(this._decryptor.finalize());
}
} else {
return AES.cryptJsWordArrayToUint8Array(this._decryptor.process(wordArray));
}
}
static convertUint8ArrayToWordArray(u8Array) {
let words = [];
let i = 0, len = u8Array.length;
while (i < len) {
words.push(
(u8Array[i++] << 24) |
(u8Array[i++] << 16) |
(u8Array[i++] << 8) |
(u8Array[i++])
);
}
return {
sigBytes: len,
words: words
};
}
/**
* https://github.com/brix/crypto-js/issues/274#issuecomment-600039187
* @param {[type]} wordArray [description]
* @return {[type]} [description]
*/
static cryptJsWordArrayToUint8Array(wordArray) {
if (!wordArray.sigBytes && wordArray.sigBytes !== 0) {
throw new Error('Invalid WordArray');
}
const l = wordArray.sigBytes;
const words = wordArray.words;
const result = new Uint8Array(l);
var i=0 /*dst*/, j=0 /*src*/;
while(true) {
// here i is a multiple of 4
if (i==l)
break;
var w = words[j++];
result[i++] = (w & 0xff000000) >>> 24;
if (i==l)
break;
result[i++] = (w & 0x00ff0000) >>> 16;
if (i==l)
break;
result[i++] = (w & 0x0000ff00) >>> 8;
if (i==l)
break;
result[i++] = (w & 0x000000ff);
}
return result;
}
}
// AES.CryptoJS = CryptoJS;
// AES.ivByteLength = CryptoJS.algo.AES.ivSize * 4;
// module.exports = AES;