Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!session || !session.changed) {
// nothing to do
next()
return
} else if (session.deleted) {
const tmpCookieOptions = Object.assign({}, cookieOptions, { expires: new Date(0), maxAge: 0 })
reply.setCookie(cookieName, '', tmpCookieOptions)
next()
return
}
const nonce = genNonce()
const msg = Buffer.from(JSON.stringify(session[kObj]))
const cipher = Buffer.allocUnsafe(msg.length + sodium.crypto_secretbox_MACBYTES)
sodium.crypto_secretbox_easy(cipher, msg, nonce, key)
reply.setCookie(cookieName, cipher.toString('base64') + ';' + nonce.toString('base64'), cookieOptions)
next()
})
const vaultString = JSON.stringify(vault);
const plaintextBuffer = Buffer.from(vaultString);
// The nonce/salt will be prepended to the ciphertext:
const dataBuffer = Buffer.alloc(
sodium.crypto_secretbox_NONCEBYTES + sodium.crypto_secretbox_MACBYTES + vaultString.length,
);
// A new nonce/salt is used every time the vault is updated:
const nonceBuffer = dataBuffer.slice(0, sodium.crypto_secretbox_NONCEBYTES);
sodium.randombytes_buf(nonceBuffer);
const keyBuffer = toKeyBuffer(organizationVaultSecret);
const cipherBuffer = dataBuffer.slice(sodium.crypto_secretbox_NONCEBYTES);
sodium.crypto_secretbox_easy(cipherBuffer, plaintextBuffer, nonceBuffer, keyBuffer);
return dataBuffer.toString("hex");
}
export function encrypt(organizationSecret: string, plaintext: string): string {
const plaintextBuffer = Buffer.from(plaintext);
// The nonce/salt will be prepended to the ciphertext:
const dataBuffer = Buffer.alloc(
sodium.crypto_secretbox_NONCEBYTES + sodium.crypto_secretbox_MACBYTES + plaintextBuffer.length,
);
// A new nonce/salt is used every time the vault is updated:
const nonceBuffer = dataBuffer.slice(0, sodium.crypto_secretbox_NONCEBYTES);
sodium.randombytes_buf(nonceBuffer);
const keyBuffer = toKeyBuffer(organizationSecret);
const cipherBuffer = dataBuffer.slice(sodium.crypto_secretbox_NONCEBYTES);
sodium.crypto_secretbox_easy(cipherBuffer, plaintextBuffer, nonceBuffer, keyBuffer);
return dataBuffer.toString("hex");
}
exports.encrypt = function (msg, nonce, key) {
var cipher = new Buffer(msg.length + sodium.crypto_secretbox_MACBYTES)
sodium.crypto_secretbox_easy(cipher, msg, nonce, key)
return cipher
}
exports.encrypt = function (msg, nonce, key) {
var cipher = Buffer.alloc(msg.length + sodium.crypto_secretbox_MACBYTES)
sodium.crypto_secretbox_easy(cipher, msg, nonce, key)
return cipher
}