Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function aeadEncrypt(key, header, plaintext, footer, nonce) {
// build nonce
const nlen = sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
const nkey = nonce || sodium.randombytes_buf(nlen);
const _nonce = sodium.crypto_generichash(nlen, plaintext, nkey);
nonce = Buffer.from(_nonce);
// encrypt
let ad;
try {
ad = utils.pae(header, nonce, footer);
} catch (ex) {
return done(ex);
}
const _ciphertext = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(plaintext, ad, null, nonce, key.raw());
const ciphertext = Buffer.from(_ciphertext);
encrypt(key, message)
{
var nonce = _sodium.randombytes_buf(_sodium.crypto_secretbox_NONCEBYTES);
try
{
return '';
//return _sodium.to_hex(_concat(Uint8Array, nonce, _sodium.crypto_secretbox_easy(message, nonce, _sodium.from_hex(key))));
}
catch(e)
{
this._adapter.log.warn(e.message);
return false;
}
}
constructor(transport: Transport, connection: Connection) {
this.transport = transport;
this.connection = connection;
this.clientNonce = sodium.randombytes_buf(NONCE_NUM_BYTES);
this.kxKeypair = sodium.crypto_kx_keypair();
}
header: {
kid: bs58.encode(targetVKey),
sender: encSender ? b64url(encSender) : null,
iv: nonce ? b64url(nonce) : null
}
}
})
const recipsB64 = b64url(JSON.stringify({
enc: 'xchacha20poly1305_ietf',
typ: 'JWM/1.0',
alg: sender ? 'Authcrypt' : 'Anoncrypt',
recipients
}))
const iv = sodium.randombytes_buf(sodium.crypto_aead_chacha20poly1305_ietf_NPUBBYTES)
const out = sodium.crypto_aead_chacha20poly1305_ietf_encrypt_detached(message, recipsB64, null, iv, cek)
return JSON.stringify({
protected: recipsB64,
iv: b64url(iv),
ciphertext: b64url(out.ciphertext),
tag: b64url(out.mac)
})
})