Port of TweetNaCl cryptographic library to JavaScript providing comprehensive cryptographic primitives
npx @tessl/cli install tessl/npm-tweetnacl@1.0.0TweetNaCl.js is a JavaScript port of the TweetNaCl cryptographic library that provides comprehensive cryptographic primitives including public-key authenticated encryption, secret-key authenticated encryption, digital signatures, scalar multiplication, hashing, and secure random byte generation. The library offers two implementations: nacl.js (direct port) and nacl-fast.js (optimized version used by default).
npm install tweetnaclconst nacl = require('tweetnacl');For ES modules:
import nacl from 'tweetnacl';For TypeScript with types:
import * as nacl from 'tweetnacl';
// or
import nacl from 'tweetnacl';const nacl = require('tweetnacl');
// Generate a random key pair
const keyPair = nacl.box.keyPair();
// Create a message and nonce
const message = new Uint8Array([1, 2, 3, 4, 5]);
const nonce = nacl.randomBytes(nacl.box.nonceLength);
// Encrypt the message
const encrypted = nacl.box(message, nonce, keyPair.publicKey, keyPair.secretKey);
// Decrypt the message
const decrypted = nacl.box.open(encrypted, nonce, keyPair.publicKey, keyPair.secretKey);
console.log(decrypted); // [1, 2, 3, 4, 5]TweetNaCl.js is organized around five core cryptographic capabilities:
The library automatically detects the runtime environment (browser or Node.js) and initializes the appropriate cryptographically secure random number generator.
Public-key authenticated encryption using x25519-xsalsa20-poly1305. Provides confidentiality and authenticity between two parties using their key pairs.
nacl.box(message, nonce, theirPublicKey, mySecretKey): Uint8Array
nacl.box.open(box, nonce, theirPublicKey, mySecretKey): Uint8Array | null
nacl.box.keyPair(): {publicKey: Uint8Array, secretKey: Uint8Array}Secret-key authenticated encryption using xsalsa20-poly1305. Provides confidentiality and authenticity using a shared secret key.
nacl.secretbox(message, nonce, key): Uint8Array
nacl.secretbox.open(box, nonce, key): Uint8Array | nullDigital signatures using ed25519. Create and verify cryptographic signatures for message authentication and non-repudiation.
nacl.sign(message, secretKey): Uint8Array
nacl.sign.open(signedMessage, publicKey): Uint8Array | null
nacl.sign.keyPair(): {publicKey: Uint8Array, secretKey: Uint8Array}Low-level scalar multiplication using x25519. Enables custom cryptographic protocols and key derivation.
nacl.scalarMult(n, p): Uint8Array
nacl.scalarMult.base(n): Uint8ArrayCore utility functions including SHA-512 hashing, secure random byte generation, and constant-time comparison.
nacl.hash(message): Uint8Array
nacl.randomBytes(length): Uint8Array
nacl.verify(x, y): boolean
nacl.setPRNG(fn): voidAdvanced low-level cryptographic primitives for custom protocol implementation and performance optimization. These functions provide direct access to underlying operations.
nacl.lowlevel.crypto_box(c, m, mlen, n, pk, sk): number
nacl.lowlevel.crypto_secretbox(c, m, mlen, n, k): number
nacl.lowlevel.crypto_sign(sm, smlen_p, m, mlen, sk): number