Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const nlen = sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES;
const nonce = Buffer.from(payload).slice(0, nlen);
// decrypt and verify
let ad;
try {
ad = utils.pae(header, nonce, footer);
} catch (ex) {
return done(ex);
}
const ciphertext = Buffer.from(payload).slice(nlen, plen);
const _plaintext = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(null, ciphertext, ad, nonce, key.raw());
const plaintext = Buffer.from(_plaintext);
// format
return plaintext.toString('utf-8');
}
export function decryptAEAD(key: Uint8Array, iv: Uint8Array, ciphertext: Uint8Array, associatedData?: Uint8Array): Uint8Array {
return sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(null, ciphertext, associatedData, iv, key);
}
'xchacha20-poly1305-decrypt' (ciphertext, nonce, key, preAuth) {
try {
return sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(undefined, ciphertext, preAuth, nonce, key)
} catch (err) {
return false
}
}
}