lazyxchacha
v1.0.8
Published
Lazy XChaCha20-Poly1305 in JavaScript base on RustCrypto: ChaCha20Poly1305.
Readme
lazyxchacha-js
Lazy XChaCha20-Poly1305 in JavaScript (WebAssembly) based on lazyxchacha and RustCrypto: ChaCha20Poly1305.
- Key exchange: X25519
- Encryption: XChaCha20-Poly1305
- Ciphertext layout:
nonce (24 bytes) + ciphertext
Installation
npm install lazyxchachaUsage
Generate a key pair
const lazyxchacha = require("lazyxchacha");
const keypair = lazyxchacha.new_keypair();
console.log("pk:", keypair.pk); // hex string
console.log("sk:", keypair.sk); // hex stringKey exchange (X25519)
const clientKp = lazyxchacha.new_keypair();
const serverKp = lazyxchacha.new_keypair();
// Client side
const clientSharedKey = lazyxchacha.shared_key(serverKp.pk, clientKp.sk);
// Server side
const serverSharedKey = lazyxchacha.shared_key(clientKp.pk, serverKp.sk);
// clientSharedKey === serverSharedKeyEncrypt / Decrypt (hex string)
const sharedKey = lazyxchacha.shared_key(serverKp.pk, clientKp.sk);
const ciphertext = lazyxchacha.encrypt("Hello lazyxchacha-js", sharedKey); // hex string
const plaintext = lazyxchacha.decrypt(ciphertext, sharedKey);
console.log("ciphertext:", ciphertext);
console.log("plaintext:", plaintext);Encrypt / Decrypt (bytes)
const ciphertext = lazyxchacha.encrypt_bytes("Hello lazyxchacha-js", sharedKey); // Uint8Array
const plaintext = lazyxchacha.decrypt_bytes(ciphertext, sharedKey); // stringEncrypt / Decrypt (raw binary)
const data = new Uint8Array([1, 2, 3, 4, 5]);
const ciphertext = lazyxchacha.encrypt_raw(data, sharedKey); // Uint8Array
const plaintext = lazyxchacha.decrypt_raw(ciphertext, sharedKey); // Uint8ArrayHex helpers
const hex = lazyxchacha.to_hex(new TextEncoder().encode("Hello")); // "48656c6c6f"
const bytes = lazyxchacha.from_hex(hex); // Uint8ArrayAPI
| Function | Parameters | Returns |
|---|---|---|
| new_keypair() | - | KeyPair { pk, sk } (hex strings) |
| shared_key(pk, sk) | hex strings | shared key as hex string |
| encrypt(plaintext, key) | string, hex key | hex string |
| decrypt(ciphertext, key) | hex string, hex key | string |
| encrypt_bytes(plaintext, key) | string, hex key | Uint8Array |
| decrypt_bytes(ciphertext, key) | Uint8Array, hex key | string |
| encrypt_raw(plaintext, key) | Uint8Array, hex key | Uint8Array |
| decrypt_raw(ciphertext, key) | Uint8Array, hex key | Uint8Array |
| to_hex(bytes) | Uint8Array | hex string |
| from_hex(hex) | hex string | Uint8Array |
Supported types across the JS/wasm boundary: https://rustwasm.github.io/docs/wasm-bindgen/reference/types.html
Development
# Build (Node.js target)
make build
# Test in headless browsers
make test
# Publish to npm
make publishLicense
See LICENSE.
