-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptolib.js
60 lines (52 loc) · 1.88 KB
/
cryptolib.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
const kdfAlgo = "PBKDF2";
const cipherAlgo = "AES-GCM";
const hashName = "SHA-256";
function strToBytes(str) {
return new TextEncoder().encode(str);
}
function bytesToStr(buff) {
return new TextDecoder().decode(buff);
}
function b64ToBytes(b64) {
const str = atob(b64);
const buff = new Uint8Array(str.length);
for (let i = 0; i < buff.length; ++i) {
buff[i] = str.charCodeAt(i);
}
return buff;
}
function bytesToB64(buff) {
let str = '';
for (let i = 0; i < buff.length; ++i) {
str += String.fromCharCode(buff[i]);
}
return btoa(str);
}
async function pbkdf2(passphrase, salt, iterations, nBytes, hashname) {
const passphraseKey = await crypto.subtle.importKey('raw', strToBytes(passphrase), { 'name': kdfAlgo }, false, ['deriveKey']);
const kdAlgorithm = { 'name': kdfAlgo, 'salt': salt, 'iterations': iterations, 'hash': hashname };
const aesAlgorithm = { 'name': cipherAlgo, 'length': nBytes * 8 };
const operations = ['encrypt', 'decrypt'];
return crypto.subtle.deriveKey(kdAlgorithm, passphraseKey, aesAlgorithm, false, operations);
}
function makeKey(password, salt) {
return pbkdf2(password, salt, 4096, 32, hashName);
}
export async function encrypt(password, data) {
const salt = crypto.getRandomValues(new Uint8Array(16));
const iv = crypto.getRandomValues(new Uint8Array(12));
const key = await makeKey(password, salt);
const encrypted = await crypto.subtle.encrypt(
{ 'name': cipherAlgo, 'iv': iv }, key, strToBytes(data));
return ({
'ct': bytesToB64(new Uint8Array(encrypted)),
'iv': bytesToB64(iv),
'salt': bytesToB64(salt)
});
}
export async function decrypt(password, data) {
const key = await makeKey(password, b64ToBytes(data['salt']));
const decrypted = await crypto.subtle.decrypt(
{ 'name': cipherAlgo, 'iv': b64ToBytes(data['iv']) }, key, b64ToBytes(data['ct']));
return bytesToStr(new Uint8Array(decrypted));
}