Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function crypto_setup (cb) {
sodium.crypto_pwhash_async(
key,
password,
nonce.slice(MAGIC_BYTES.length, MAGIC_BYTES.length + sodium.crypto_pwhash_SALTBYTES),
sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
sodium.crypto_pwhash_ALG_DEFAULT,
function (err) {
if (err) return cb(err)
instance = sodium.crypto_stream_xor_instance(nonce.slice(MAGIC_BYTES.length + sodium.crypto_passwd_SALTBYTES), key)
cb()
}
)
}
}
return new Promise((resolve, reject) => {
const passwordBuffer = Buffer.from(password);
const hashBuffer = Buffer.allocUnsafe(meta.hashBytes);
const saltBuffer = Buffer.from(meta.salt, 'hex');
sodium.crypto_pwhash_async(hashBuffer, passwordBuffer, saltBuffer, meta.opslimit, meta.memlimit, meta.algorithm, (err) => {
if (err != null) {
return reject(err);
}
const hash = hashBuffer.toString('hex');
resolve({
hash,
meta
});
});
});
}
return new Promise((resolve, reject) => {
const passwordBuffer = Buffer.from(password);
const hashBuffer = Buffer.allocUnsafe(config.hashBytes);
const saltBuffer = Buffer.allocUnsafe(config.saltBytes);
sodium.randombytes_buf(saltBuffer);
const meta = {
salt: saltBuffer.toString('hex'),
hashBytes: config.hashBytes,
opslimit: config.opslimit,
memlimit: config.memlimit,
algorithm: config.algorithm
};
sodium.crypto_pwhash_async(hashBuffer, passwordBuffer, saltBuffer, config.opslimit, config.memlimit, config.algorithm, (err) => {
if (err != null) {
return reject(err);
}
const hash = hashBuffer.toString('hex');
resolve({
hash,
meta
});
});
});
}
const hashBuffer = Buffer.allocUnsafe(sodiumConfig.hashBytes);
const saltBuffer = Buffer.allocUnsafe(sodiumConfig.saltBytes);
sodium.randombytes_buf(saltBuffer);
const meta = {
salt: saltBuffer.toString("hex"),
hashBytes: sodiumConfig.hashBytes,
opslimit: sodiumConfig.opslimit,
memlimit: sodiumConfig.memlimit,
algorithm: sodiumConfig.algorithm
};
sodium.crypto_pwhash_async(
hashBuffer,
passwordBuffer,
saltBuffer,
sodiumConfig.opslimit,
sodiumConfig.memlimit,
sodiumConfig.algorithm,
(err) => {
if (err != null) {
return reject(err);
}
const hash = hashBuffer.toString("hex");
resolve({
hash,
meta
return new Promise((resolve, reject) => {
const passwordBuffer = Buffer.from(password);
const hashBuffer = Buffer.allocUnsafe(meta.hashBytes);
const saltBuffer = Buffer.from(meta.salt, "hex");
sodium.crypto_pwhash_async(hashBuffer, passwordBuffer, saltBuffer, meta.opslimit, meta.memlimit, meta.algorithm, (err) => {
if (err != null) {
return reject(err);
}
const hash = hashBuffer.toString("hex");
resolve({
hash,
meta
});
});
});
}
return new Promise((resolve, reject) => {
const finalize = () => {
password.$restoreProtection()
hash.$restoreProtection()
}
try {
password.$makeReadable()
hash.$makeWritable()
sodium.crypto_pwhash_async(
hash._, password._, opts.salt,
opts.opslimit, opts.memlimit, opts.algorithm,
(err) => {
try {
finalize()
if (err) return reject(err)
resolve({
salt: opts.salt,
hash
})
} catch (e) {
reject(e)
}
})
} catch (e) {
reject(e)