Skip to content

Commit 5a39f75

Browse files
Update encryption-hashing.md
1 parent 0e9a537 commit 5a39f75

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

content/security/encryption-hashing.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ As an example, let's use AES (Advanced Encryption System) `'aes-256-ctr'` algori
1212

1313
```typescript
1414
import { createCipheriv, randomBytes } from 'crypto';
15+
import { promisify } from 'util';
1516

1617
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);
1824

1925
const textToEncrypt = 'Nest';
2026
const encryptedText = Buffer.concat([
@@ -28,7 +34,7 @@ Now to decrypt `encryptedText` value:
2834
```typescript
2935
import { createDecipheriv } from 'crypto';
3036

31-
const decipher = createDecipheriv('aes-256-ctr', 'secretKey', iv);
37+
const decipher = createDecipheriv('aes-256-ctr', key, iv);
3238
const decryptedText = Buffer.concat([
3339
decipher.update(encryptedText),
3440
decipher.final(),

0 commit comments

Comments
 (0)