|
1 | 1 | import AesProviderBase from '@beak/common-host/providers/encryption-aes';
|
2 |
| -import crypto, { Cipher, Decipher } from 'crypto'; |
3 |
| -import { promisify } from 'util'; |
| 2 | +import crypto, { Cipher, Decipher } from 'node:crypto'; |
| 3 | +import { promisify } from 'node:util'; |
4 | 4 |
|
5 | 5 | const scrypt = promisify(crypto.scrypt);
|
6 | 6 |
|
7 | 7 | export default class AesProvider extends AesProviderBase {
|
8 |
| - async generateKey(): Promise<string> { |
9 |
| - const password = crypto.randomBytes(32); |
10 |
| - const salt = crypto.randomBytes(16); |
11 |
| - const key = await scrypt(password, salt, 32) as Buffer; |
| 8 | + async generateKey(): Promise<string> { |
| 9 | + const password = crypto.randomBytes(32); |
| 10 | + const salt = crypto.randomBytes(16); |
| 11 | + const key = await scrypt(password, salt, 32) as Buffer; |
12 | 12 |
|
13 |
| - return key.toString('base64'); |
14 |
| - } |
| 13 | + return key.toString('base64'); |
| 14 | + } |
15 | 15 |
|
16 |
| - async generateIv(): Promise<string> { |
17 |
| - const iv = crypto.randomBytes(16); |
| 16 | + async generateIv(): Promise<string> { |
| 17 | + const iv = crypto.randomBytes(16); |
18 | 18 |
|
19 |
| - return iv.toString('base64'); |
20 |
| - } |
| 19 | + return iv.toString('base64'); |
| 20 | + } |
21 | 21 |
|
22 |
| - async encrypt(payload: Uint8Array, key: string, iv: string): Promise<string> { |
23 |
| - const keyBuffer = Buffer.from(key, 'base64'); |
24 |
| - const ivBuffer = Buffer.from(iv, 'base64'); |
| 22 | + async encrypt(payload: Uint8Array, key: string, iv: string): Promise<string> { |
| 23 | + const keyBuffer = Buffer.from(key, 'base64'); |
| 24 | + const ivBuffer = Buffer.from(iv, 'base64'); |
25 | 25 |
|
26 |
| - const cipher = crypto.createCipheriv(this.aesAlgo, keyBuffer, ivBuffer, void 0) as Cipher; |
27 |
| - const update = cipher.update(payload); |
28 |
| - const final = Buffer.concat([update, cipher.final()]); |
| 26 | + const cipher = crypto.createCipheriv(this.aesAlgo, keyBuffer, ivBuffer, void 0) as Cipher; |
| 27 | + const update = cipher.update(payload); |
| 28 | + const final = Buffer.concat([update, cipher.final()]); |
29 | 29 |
|
30 |
| - return final.toString('base64'); |
31 |
| - } |
| 30 | + return final.toString('base64'); |
| 31 | + } |
32 | 32 |
|
33 |
| - async decrypt(payload: Uint8Array, key: string, iv: string): Promise<string> { |
34 |
| - const keyBuffer = Buffer.from(key, 'base64'); |
35 |
| - const ivBuffer = Buffer.from(iv, 'base64'); |
| 33 | + async decrypt(payload: Uint8Array, key: string, iv: string): Promise<string> { |
| 34 | + const keyBuffer = Buffer.from(key, 'base64'); |
| 35 | + const ivBuffer = Buffer.from(iv, 'base64'); |
36 | 36 |
|
37 |
| - const decipher = crypto.createDecipheriv(this.aesAlgo, keyBuffer, ivBuffer, void 0) as Decipher; |
38 |
| - const update = decipher.update(payload); |
39 |
| - const final = Buffer.concat([update, decipher.final()]); |
| 37 | + const decipher = crypto.createDecipheriv(this.aesAlgo, keyBuffer, ivBuffer, void 0) as Decipher; |
| 38 | + const update = decipher.update(payload); |
| 39 | + const final = Buffer.concat([update, decipher.final()]); |
40 | 40 |
|
41 |
| - return final.toString('utf-8'); |
42 |
| - } |
| 41 | + return final.toString('utf-8'); |
| 42 | + } |
43 | 43 |
|
44 |
| - async encryptString(payload: string, key: string, iv: string): Promise<string> { |
45 |
| - const buf = Buffer.from(payload, 'utf-8'); |
| 44 | + async encryptString(payload: string, key: string, iv: string): Promise<string> { |
| 45 | + const buf = Buffer.from(payload, 'utf-8'); |
46 | 46 |
|
47 |
| - return await this.encrypt(buf, key, iv); |
48 |
| - } |
| 47 | + return await this.encrypt(buf, key, iv); |
| 48 | + } |
49 | 49 |
|
50 |
| - async decryptString(payload: string, key: string, iv: string): Promise<string> { |
51 |
| - if (payload === '') |
52 |
| - return ''; |
| 50 | + async decryptString(payload: string, key: string, iv: string): Promise<string> { |
| 51 | + if (payload === '') |
| 52 | + return ''; |
53 | 53 |
|
54 |
| - const buf = Buffer.from(payload, 'base64'); |
| 54 | + const buf = Buffer.from(payload, 'base64'); |
55 | 55 |
|
56 |
| - return await this.decrypt(buf, key, iv); |
57 |
| - } |
| 56 | + return await this.decrypt(buf, key, iv); |
| 57 | + } |
58 | 58 | }
|
0 commit comments