Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sodium.ready.then(() => {
if (!this.key) {
const salt = crypto.randomBytes(sodium.crypto_pwhash_SALTBYTES)
try {
this.key = sodium.crypto_pwhash(
KEY_SIZE,
this.pwd,
salt,
sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
sodium.crypto_pwhash_ALG_DEFAULT
);
} catch(ex) {
return callback(new Error('Libsodium error: ' + ex))
}
this.push(Buffer.from([PWD_STREAM_VERSION]));
this.push(salt);
}
super._transform(data, encoding, callback);
});
};
return sodium.ready.then(() => {
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;
}
if (!this.pwdHeader) {
this.pwdHeader = Buffer.alloc(sodium.crypto_pwhash_SALTBYTES + 1);
}
if (!this.key) {
const saltBytesCopied = data.copy(this.pwdHeader, this.pwdHeaderOffset);
this.pwdHeaderOffset += saltBytesCopied;
if (this.pwdHeaderOffset < sodium.crypto_pwhash_SALTBYTES) {
return callback(null, null);
}
if (this.pwdHeader[0] !== PWD_STREAM_VERSION) {
return callback(new Error('Unsupported PwdEncryptionStream version'));
}
this.key = sodium.crypto_pwhash(
KEY_SIZE,
this.pwd,
this.pwdHeader.slice(1),
sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
sodium.crypto_pwhash_ALG_DEFAULT
);
this.initPwd = true;
data = data.slice(saltBytesCopied);
}
}
super._transform(data, encoding, callback);
});
}
const pwhash = async (passphrase, salt) => {
await sodiumsumo.ready;
return sodiumsumo.crypto_pwhash(sodiumsumo.crypto_box_SEEDBYTES, passphrase, salt, 4, 33554432, sodiumsumo.crypto_pwhash_ALG_ARGON2I13);
}
return sodium.ready.then(() => {
if (typeof p === 'function') {
cb = p;
p = null;
}
const done = ret(cb);
if (!p) { return done(new Error('Cannot decrypt without a password')); }
let s, salt;
[ c ] = cparse(c);
salt = c.slice(0, sodium.crypto_pwhash_SALTBYTES);
try {
s = 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));
}
c = c.slice(sodium.crypto_pwhash_SALTBYTES);
return dsync(s, c, n, cb);
})
};