The Sodium cryptographic library compiled to pure JavaScript (wrappers, sumo variant)
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
AEAD functions provide encryption that ensures both confidentiality and authenticity. They encrypt the plaintext and authenticate both the encrypted data and optional associated data in a single operation.
The extended nonce variant of ChaCha20-Poly1305, allowing for random nonce generation without collision concerns.
/**
* Generate a random key for XChaCha20-Poly1305 AEAD
* @returns Uint8Array - 32-byte key
*/
function crypto_aead_xchacha20poly1305_ietf_keygen(): Uint8Array;/**
* Encrypt and authenticate data using XChaCha20-Poly1305
* @param message - The plaintext data to encrypt
* @param additional_data - Additional data to authenticate (not encrypted)
* @param secret_nonce - Secret nonce (usually null)
* @param public_nonce - 24-byte nonce (can be random)
* @param key - 32-byte encryption key
* @returns Uint8Array - Encrypted data with authentication tag
*/
function crypto_aead_xchacha20poly1305_ietf_encrypt(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;/**
* Decrypt and verify data using XChaCha20-Poly1305
* @param secret_nonce - Secret nonce (usually null)
* @param ciphertext - Encrypted data with authentication tag
* @param additional_data - Additional authenticated data
* @param public_nonce - 24-byte nonce used for encryption
* @param key - 32-byte decryption key
* @returns Uint8Array - Decrypted plaintext
* @throws Error if authentication fails
*/
function crypto_aead_xchacha20poly1305_ietf_decrypt(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;/**
* Encrypt data with detached authentication tag
* @param message - Plaintext to encrypt
* @param additional_data - Additional authenticated data
* @param secret_nonce - Secret nonce (usually null)
* @param public_nonce - 24-byte nonce
* @param key - 32-byte encryption key
* @returns Object with ciphertext and mac properties
*/
function crypto_aead_xchacha20poly1305_ietf_encrypt_detached(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): { ciphertext: Uint8Array; mac: Uint8Array };
/**
* Decrypt data with separate authentication tag
* @param secret_nonce - Secret nonce (usually null)
* @param ciphertext - Encrypted data (without tag)
* @param mac - Authentication tag
* @param additional_data - Additional authenticated data
* @param public_nonce - 24-byte nonce
* @param key - 32-byte decryption key
* @returns Uint8Array - Decrypted plaintext
* @throws Error if authentication fails
*/
function crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
mac: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;const crypto_aead_xchacha20poly1305_ietf_KEYBYTES: number; // 32
const crypto_aead_xchacha20poly1305_ietf_NPUBBYTES: number; // 24
const crypto_aead_xchacha20poly1305_ietf_ABYTES: number; // 16
const crypto_aead_xchacha20poly1305_ietf_NSECBYTES: number; // 0
const crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX: number; // Large valueStandard ChaCha20-Poly1305 with IETF-compatible nonce size.
function crypto_aead_chacha20poly1305_ietf_keygen(): Uint8Array;function crypto_aead_chacha20poly1305_ietf_encrypt(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array, // 12 bytes
key: Uint8Array
): Uint8Array;
function crypto_aead_chacha20poly1305_ietf_decrypt(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array, // 12 bytes
key: Uint8Array
): Uint8Array;function crypto_aead_chacha20poly1305_ietf_encrypt_detached(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): { ciphertext: Uint8Array; mac: Uint8Array };
function crypto_aead_chacha20poly1305_ietf_decrypt_detached(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
mac: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;const crypto_aead_chacha20poly1305_ietf_KEYBYTES: number; // 32
const crypto_aead_chacha20poly1305_ietf_NPUBBYTES: number; // 12
const crypto_aead_chacha20poly1305_ietf_ABYTES: number; // 16
const crypto_aead_chacha20poly1305_ietf_NSECBYTES: number; // 0High-performance AEAD cipher with excellent security properties.
function crypto_aead_aegis256_keygen(): Uint8Array;function crypto_aead_aegis256_encrypt(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array, // 32 bytes
key: Uint8Array
): Uint8Array;
function crypto_aead_aegis256_decrypt(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;function crypto_aead_aegis256_encrypt_detached(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): { ciphertext: Uint8Array; mac: Uint8Array };
function crypto_aead_aegis256_decrypt_detached(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
mac: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;const crypto_aead_aegis256_KEYBYTES: number; // 32
const crypto_aead_aegis256_NPUBBYTES: number; // 32
const crypto_aead_aegis256_ABYTES: number; // 32
const crypto_aead_aegis256_NSECBYTES: number; // 0Faster variant of AEGIS with 128-bit security level.
function crypto_aead_aegis128l_keygen(): Uint8Array;
function crypto_aead_aegis128l_encrypt(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array, // 16 bytes
key: Uint8Array
): Uint8Array;
function crypto_aead_aegis128l_decrypt(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;
function crypto_aead_aegis128l_encrypt_detached(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): { ciphertext: Uint8Array; mac: Uint8Array };
function crypto_aead_aegis128l_decrypt_detached(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
mac: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;const crypto_aead_aegis128l_KEYBYTES: number; // 16
const crypto_aead_aegis128l_NPUBBYTES: number; // 16
const crypto_aead_aegis128l_ABYTES: number; // 32
const crypto_aead_aegis128l_NSECBYTES: number; // 0Original ChaCha20-Poly1305 with 8-byte nonce.
function crypto_aead_chacha20poly1305_keygen(): Uint8Array;
function crypto_aead_chacha20poly1305_encrypt(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array, // 8 bytes
key: Uint8Array
): Uint8Array;
function crypto_aead_chacha20poly1305_decrypt(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;
function crypto_aead_chacha20poly1305_encrypt_detached(
message: Uint8Array,
additional_data: Uint8Array | null,
secret_nonce: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): { ciphertext: Uint8Array; mac: Uint8Array };
function crypto_aead_chacha20poly1305_decrypt_detached(
secret_nonce: Uint8Array | null,
ciphertext: Uint8Array,
mac: Uint8Array,
additional_data: Uint8Array | null,
public_nonce: Uint8Array,
key: Uint8Array
): Uint8Array;const crypto_aead_chacha20poly1305_KEYBYTES: number; // 32
const crypto_aead_chacha20poly1305_NPUBBYTES: number; // 8
const crypto_aead_chacha20poly1305_ABYTES: number; // 16
const crypto_aead_chacha20poly1305_NSECBYTES: number; // 0import _sodium from 'libsodium-wrappers-sumo';
await _sodium.ready;
const sodium = _sodium;
// Generate key
const key = sodium.crypto_aead_xchacha20poly1305_ietf_keygen();
// Prepare data
const message = sodium.from_string('Confidential message');
const additionalData = sodium.from_string('public header');
const nonce = sodium.randombytes_buf(sodium.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
// Encrypt
const ciphertext = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(
message, additionalData, null, nonce, key
);
// Decrypt
const plaintext = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
null, ciphertext, additionalData, nonce, key
);
console.log(sodium.to_string(plaintext)); // "Confidential message"// Encrypt with separate MAC
const { ciphertext, mac } = sodium.crypto_aead_xchacha20poly1305_ietf_encrypt_detached(
message, additionalData, null, nonce, key
);
// Store ciphertext and MAC separately
console.log('Ciphertext:', sodium.to_hex(ciphertext));
console.log('MAC:', sodium.to_hex(mac));
// Decrypt with separate MAC
const plaintext = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
null, ciphertext, mac, additionalData, nonce, key
);// For high-throughput applications
const key = sodium.crypto_aead_aegis256_keygen();
const nonce = sodium.randombytes_buf(sodium.crypto_aead_aegis256_NPUBBYTES);
const ciphertext = sodium.crypto_aead_aegis256_encrypt(
message, additionalData, null, nonce, key
);
const plaintext = sodium.crypto_aead_aegis256_decrypt(
null, ciphertext, additionalData, nonce, key
);All algorithms provide strong security when used correctly with unique nonces per key.