Repository navigation
Expand file tree
/
Copy pathweb.js
More file actions
111 lines (80 loc) · 3.13 KB
/
Copy pathweb.js
File metadata and controls
111 lines (80 loc) · 3.13 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
101
102
103
104
105
106
107
108
109
110
111
/* global crypto */
function concatArrayBuffer(ab1, ab2) {
const tmp = new Uint8Array(ab1.byteLength + ab2.byteLength);
tmp.set(new Uint8Array(ab1), 0);
tmp.set(new Uint8Array(ab2), ab1.byteLength);
return tmp.buffer;
}
export default class WebCryptography {
static IV_LENGTH = 16;
get algo() {
return 'aes-256-cbc';
}
async encrypt(key, input) {
const cKey = await this.getKey(key);
if (input instanceof ArrayBuffer) {
return this.encryptArrayBuffer(cKey, input);
}
if (typeof input === 'string') {
return this.encryptString(cKey, input);
}
throw new Error('Cannot encrypt this file. In browsers file encryption supports only string or ArrayBuffer');
}
async decrypt(key, input) {
const cKey = await this.getKey(key);
if (input instanceof ArrayBuffer) {
return this.decryptArrayBuffer(cKey, input);
}
if (typeof input === 'string') {
return this.decryptString(cKey, input);
}
throw new Error('Cannot decrypt this file. In browsers file decryption supports only string or ArrayBuffer');
}
async encryptFile(key, file, File) {
const bKey = await this.getKey(key);
const abPlaindata = await file.toArrayBuffer();
const abCipherdata = await this.encryptArrayBuffer(bKey, abPlaindata);
return File.create({
name: file.name,
mimeType: 'application/octet-stream',
data: abCipherdata,
});
}
async decryptFile(key, file, File) {
const bKey = await this.getKey(key);
const abCipherdata = await file.toArrayBuffer();
const abPlaindata = await this.decryptArrayBuffer(bKey, abCipherdata);
return File.create({
name: file.name,
data: abPlaindata,
});
}
async getKey(key) {
const bKey = Buffer.from(key);
const abHash = await crypto.subtle.digest('SHA-256', bKey.buffer);
const abKey = Buffer.from(Buffer.from(abHash).toString('hex').slice(0, 32), 'utf8').buffer;
return crypto.subtle.importKey('raw', abKey, 'AES-CBC', true, ['encrypt', 'decrypt']);
}
async encryptArrayBuffer(key, plaintext) {
const abIv = crypto.getRandomValues(new Uint8Array(16));
return concatArrayBuffer(abIv.buffer, await crypto.subtle.encrypt({ name: 'AES-CBC', iv: abIv }, key, plaintext));
}
async decryptArrayBuffer(key, ciphertext) {
const abIv = ciphertext.slice(0, 16);
return crypto.subtle.decrypt({ name: 'AES-CBC', iv: abIv }, key, ciphertext.slice(16));
}
async encryptString(key, plaintext) {
const abIv = crypto.getRandomValues(new Uint8Array(16));
const abPlaintext = Buffer.from(plaintext).buffer;
const abPayload = await crypto.subtle.encrypt({ name: 'AES-CBC', iv: abIv }, key, abPlaintext);
const ciphertext = concatArrayBuffer(abIv.buffer, abPayload);
return Buffer.from(ciphertext).toString('utf8');
}
async decryptString(key, ciphertext) {
const abCiphertext = Buffer.from(ciphertext);
const abIv = abCiphertext.slice(0, 16);
const abPayload = abCiphertext.slice(16);
const abPlaintext = await crypto.subtle.decrypt({ name: 'AES-CBC', iv: abIv }, key, abPayload);
return Buffer.from(abPlaintext).toString('utf8');
}
}