Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function pw(password, cb) {
const done = ret(cb);
if (!password) { return done(new Error('Empty password')); }
let hash;
try {
// generates the salt itself
hash = sodium.crypto_pwhash_str(password, sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE, sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE);
} catch(ex) {
return done(new Error('Libsodium error: ' + ex));
}
return done(null, convert({
alg: 'argon2id',
hash: hash
}));
}
const hashPassword = (password) => sodium.crypto_pwhash_str(
password,
sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
);
if (typeof p === 'function') {
cb = p;
p = null;
}
const done = ret(cb);
if (!p) { return done(new Error('Cannot encrypt without a password')); }
let sk;
const salt = crypto.randomBytes(sodium.crypto_pwhash_SALTBYTES)
try {
sk = sodium.crypto_pwhash(
KEY_SIZE,
p,
salt,
sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
sodium.crypto_pwhash_ALG_DEFAULT
);
} catch(ex) {
return done(new Error('Libsodium error: ' + ex))
}
return sync(m, Buffer.from(sk), (err, aead) => {
const done = ret(cb);
if (err) {
return err;
}
aead.ciphertext = Buffer.concat([salt, aead.ciphertext])
return done(null, aead);
})
});