@@ -12,9 +12,15 @@ As an example, let's use AES (Advanced Encryption System) `'aes-256-ctr'` algori
12
12
13
13
``` typescript
14
14
import { createCipheriv , randomBytes } from ' crypto' ;
15
+ import { promisify } from ' util' ;
15
16
16
17
const iv = randomBytes (16 );
17
- const cipher = createCipheriv (' aes-256-ctr' , ' secretKey' , iv );
18
+ const password = ' Password used to generate key' ;
19
+
20
+ // The key length is dependent on the algorithm.
21
+ // In this case for aes256, it is 32 bytes.
22
+ const key = (await promisify (scrypt )(password , ' salt' , 32 )) as Buffer ;
23
+ const cipher = createCipheriv (' aes-256-ctr' , key , iv );
18
24
19
25
const textToEncrypt = ' Nest' ;
20
26
const encryptedText = Buffer .concat ([
@@ -28,7 +34,7 @@ Now to decrypt `encryptedText` value:
28
34
``` typescript
29
35
import { createDecipheriv } from ' crypto' ;
30
36
31
- const decipher = createDecipheriv (' aes-256-ctr' , ' secretKey ' , iv );
37
+ const decipher = createDecipheriv (' aes-256-ctr' , key , iv );
32
38
const decryptedText = Buffer .concat ([
33
39
decipher .update (encryptedText ),
34
40
decipher .final (),
0 commit comments