Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!Buffer.isBuffer(aad)) {
aad = await Util.toBuffer(aad);
}
aad = Buffer.concat([nonce, aad]);
} else {
aad = nonce;
}
let ciphertext = await sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
plaintext,
nonce,
new CryptographyKey(encKey),
aad
);
await sodium.sodium_memzero(encKey);
return MAGIC_HEADER + base64url.stringify(
Buffer.concat([nonce, ciphertext])
);
}
} else {
mac = await Util.hmac(
'sha384',
Util.pack([
Buffer.from(MAGIC_HEADER, 'binary'),
hkdfSalt,
ctrNonce,
ciphertext
]),
macKey,
true
);
}
await sodium.sodium_memzero(macKey);
return MAGIC_HEADER + base64url.stringify(
Buffer.concat([
hkdfSalt,
ctrNonce,
mac,
ciphertext
])
);
}
static packString(str)
{
return Util.store64_le(str.length).toString('hex') +
base64url.stringify(Buffer.from(str));
}
};