-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.js
139 lines (121 loc) · 3.42 KB
/
crypto.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
const ENC_ALGORITHM = 'AES-GCM';
const KEY_DER_ALGORITHM = 'PBKDF2';
const KEY_DER_HASH = 'SHA-256';
const TAG_LENGTH = 128;
const ITERATIONS = 10000;
const EXTRACT_KEY = false;
const SALT_LENGTH_BYTES = 16;
const IV_LENGTH_BYTES = 12;
async function encrypt(data, iv) {
// import our key and convert into CryptoKey
const keyArray = fromDecString(AES_KEY);
const key = await importAESKey(keyArray);
// txt -> ArrayBuffer
let dataToEncrypt = new TextEncoder().encode(data);
return crypto.subtle.encrypt(
{
name: ENC_ALGORITHM,
iv,
tagLength: TAG_LENGTH,
},
key,
dataToEncrypt
).then(arrayBuffer => {
return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
}).catch(e => {
throw Error('Failed to encrypt:' + e);
});
}
async function decrypt(data, iv) {
// import our key and convert into CryptoKey
const keyArray = fromDecString(AES_KEY);
const key = await importAESKey(keyArray);
// base64 -> ArrayBuffer
const dataToDecrypt = base64ToArrayBuffer(data);
return crypto.subtle.decrypt(
{
name: ENC_ALGORITHM,
iv,
tagLength: TAG_LENGTH,
},
key,
dataToDecrypt
).then(arrayBuffer => {
return new TextDecoder().decode(arrayBuffer)
}).catch(e => {
throw Error('Failed to decrypt:' + e);
});
}
async function encryptUsingPassword(password, data, iv, salt) {
const keyMaterial = await passwordToKeyMaterial(password);
const key = await deriveKeyFromKeyMaterial(keyMaterial, salt);
return encrypt(data, iv, key)
};
async function decryptUsingPassword(password, data, iv, salt) {
const keyMaterial = await passwordToKeyMaterial(password);
const key = await deriveKeyFromKeyMaterial(keyMaterial, salt);
return decrypt(data, iv, key);
};
async function deriveKeyFromKeyMaterial(keyMaterial, salt){
return crypto.subtle.deriveKey(
{
name: KEY_DER_ALGORITHM,
salt,
iterations: ITERATIONS,
hash: { name: KEY_DER_HASH },
},
keyMaterial,
{
name: ENC_ALGORITHM,
length: 256,
},
EXTRACT_KEY,
["encrypt", "decrypt"]
);
};
async function passwordToKeyMaterial(password){
const encoder = new TextEncoder();
return crypto.subtle.importKey(
"raw",
encoder.encode(password),
KEY_DER_ALGORITHM,
EXTRACT_KEY,
["deriveKey"]
);
};
async function importAESKey(raw) {
return crypto.subtle.importKey(
"raw", raw, { name: ENC_ALGORITHM }, EXTRACT_KEY, ["encrypt", "decrypt"]
);
};
function base64ToArrayBuffer(data) {
const characters = atob(data);
let array = new Uint8Array(characters.length);
for (var i = 0; i < characters.length; i++) {
array[i] = characters.charCodeAt(i);
}
return new Uint8Array(array);
};
const dec2hex = dec => dec < 10 ? '0' + String(dec) : dec.toString(16);
const fromHexString = hexString => new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
const fromDecString = decStringArray => new Uint8Array(decStringArray.split(',').map(byte => parseInt(byte, 16)));
function generateRandomHexStringArray(bytes) {
let array = new Uint8Array(bytes);
crypto.getRandomValues(array);
return Array.from(array, dec2hex).join('');
};
async function setRandomValues(array){
await crypto.getRandomValues(array);
};
export {
IV_LENGTH_BYTES,
SALT_LENGTH_BYTES,
encrypt,
decrypt,
generateRandomHexStringArray,
setRandomValues,
encryptUsingPassword,
decryptUsingPassword,
dec2hex,
fromHexString
}