-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkdf.mjs
32 lines (24 loc) · 878 Bytes
/
kdf.mjs
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
// Documentation in https://nodejs.org/api/crypto.htm
import * as crypto from 'crypto'
/////////////////////////
// Node-Crypto library //
/////////////////////////
export function deriveKey(userKey, salt = 'csc-354-cji32-rjwiro', size = 32) {
// Uses a key derivation algorithm to transform a user-provided key
// into a key with the same size the algorithm requires
//
// Available algorithms:
// scrypt, pbkdf2
// let key = crypto.scryptSync(userKey, salt, size)
let key = crypto.pbkdf2Sync(userKey, salt, 131072, size, 'sha256')
return key
}
///////////////////
// Noble library //
///////////////////
import { pbkdf2 } from '@noble/hashes/pbkdf2'
import { sha256 } from '@noble/hashes/sha256'
export function deriveKey2(userKey, salt = 'csc-354-cji32-rjwiro', size = 32) {
let key = pbkdf2(sha256, userKey, salt, { c: 131072, dkLen: size })
return key
}