-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.js
31 lines (26 loc) · 953 Bytes
/
crypto.js
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
const crypto = require('crypto');
const password = 'qwerty';
const hex = crypto.createHash('sha512').update(password).digest('hex');
const base64 = crypto.createHash('sha512').update(password).digest('base64');
console.log('hex: ', hex);
console.log('base64: ', base64);
const salt = crypto.randomBytes(32).toString('hex');
const iterations = 100000;
const keylen = 64;
const digest = 'sha512';
const callback = (err, derivedKey) => {
if (err) throw err;
console.log(derivedKey.toString('hex'));
};
crypto.pbkdf2(password, salt, iterations, keylen, digest, callback);
const encrypt = (salt, password) => {
crypto.pbkdf2(password, salt.toString(), 1, 32, 'sha512', (err, derivedKey) => {
if (err) throw err;
const hashed = derivedKey.toString('hex');
console.log('salt: ', salt);
console.log('hashed: ', hashed);
});
};
const password = 'fl0wer';
const salt = crypto.randomBytes(32).toString('hex');
encrypt(salt, password);