-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (33 loc) · 856 Bytes
/
index.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
32
33
34
35
36
37
/**
* Dependencies
*/
const sodium = require('sodium-universal')
const alloc = require('buffer-alloc')
/**
* Hash password or verify password against a buffer encoded
* with argon2 algorithm.
*
* @param {String} password (password to hash or to verify)
* @param {Buffer?} stored
* @return {Buffer} (or null othwise)
* @api public
*/
module.exports = function (pass, ...args) {
const arg = args[0]
if (arg) {
const bool = sodium.crypto_pwhash_str_verify(
alloc(sodium.crypto_pwhash_STRBYTES, arg),
new Buffer(pass)
)
return bool && arg
} else {
var output = alloc(sodium.crypto_pwhash_STRBYTES)
sodium.crypto_pwhash_str(
output,
new Buffer(pass),
arg || sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
args[1] || sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE
)
return output
}
}