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.initPwd) {
// Get salt from stream to re-generate key
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);
const salt = async () => {
return rand(sodiumsumo.crypto_pwhash_SALTBYTES);
}
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);
})
};
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);
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);
})
};