Cryptography
Post-quantum cryptographic primitives, key management, and secure storage for building security-first applications.
Supported Algorithms
| Category | Algorithms | Use Case |
|---|---|---|
| Signatures | Ed25519, secp256k1, Dilithium | Digital signatures |
| Key Exchange | X25519, Kyber | Secure key agreement |
| Encryption | AES-256-GCM, ChaCha20-Poly1305 | Data encryption |
| Hashing | Blake3, SHA-256, SHA-3 | Data integrity |
| BLS | BLS12-381 | Aggregate signatures |
Key Generation
import { Tenzro } from 'tenzro';const tenzro = new Tenzro();// Generate Ed25519 keypairconst ed25519 = await tenzro.crypto.generateKeyPair({algorithm: 'ed25519',});// Generate post-quantum keypair (Dilithium)const dilithium = await tenzro.crypto.generateKeyPair({algorithm: 'dilithium3',});// Generate from mnemonicconst mnemonic = await tenzro.crypto.generateMnemonic(24);const hdKey = await tenzro.crypto.fromMnemonic(mnemonic, {algorithm: 'ed25519',path: "m/44'/0'/0'",});
Digital Signatures
// Sign dataconst signature = await tenzro.crypto.sign({data: messageBytes,privateKey: keyPair.privateKey,algorithm: 'ed25519',});// Verify signatureconst isValid = await tenzro.crypto.verify({data: messageBytes,signature,publicKey: keyPair.publicKey,algorithm: 'ed25519',});console.log('Valid:', isValid); // true
Encryption
// Symmetric encryption (AES-256-GCM)const encrypted = await tenzro.crypto.encrypt({plaintext: sensitiveData,key: encryptionKey,algorithm: 'aes-256-gcm',});// Decryptconst decrypted = await tenzro.crypto.decrypt({ciphertext: encrypted.ciphertext,key: encryptionKey,nonce: encrypted.nonce,algorithm: 'aes-256-gcm',});// Asymmetric encryption (hybrid)const sealed = await tenzro.crypto.seal({plaintext: sensitiveData,publicKey: recipientPublicKey,});const opened = await tenzro.crypto.open({ciphertext: sealed,privateKey: recipientPrivateKey,});
Hashing
// Blake3 (recommended)const hash = await tenzro.crypto.hash({data: input,algorithm: 'blake3',});// SHA-256const sha256 = await tenzro.crypto.hash({data: input,algorithm: 'sha256',});// Keyed hash (MAC)const mac = await tenzro.crypto.mac({data: input,key: macKey,algorithm: 'blake3',});
Key Derivation
// Derive key from passwordconst derived = await tenzro.crypto.deriveKey({password: userPassword,salt: randomSalt,algorithm: 'argon2id',keyLength: 32,});// Derive multiple keys (HKDF)const keys = await tenzro.crypto.hkdf({secret: masterKey,salt: salt,info: 'encryption-key',keyLength: 32,});
BLS Signatures
Aggregate signatures for consensus and multi-party signing:
// Generate BLS keypairconst blsKey = await tenzro.crypto.generateKeyPair({algorithm: 'bls12-381',});// Sign with BLSconst sig1 = await tenzro.crypto.sign({data: message,privateKey: key1.privateKey,algorithm: 'bls12-381',});const sig2 = await tenzro.crypto.sign({data: message,privateKey: key2.privateKey,algorithm: 'bls12-381',});// Aggregate signaturesconst aggregated = await tenzro.crypto.aggregateSignatures([sig1, sig2]);// Verify aggregate (single verification for multiple signers)const valid = await tenzro.crypto.verifyAggregate({data: message,signature: aggregated,publicKeys: [key1.publicKey, key2.publicKey],});
Post-Quantum Cryptography
Future-proof your security with quantum-resistant algorithms:
// Dilithium signatures (NIST PQC standard)const dilithium = await tenzro.crypto.generateKeyPair({algorithm: 'dilithium3', // or 'dilithium5'});const signature = await tenzro.crypto.sign({data: message,privateKey: dilithium.privateKey,algorithm: 'dilithium3',});// Kyber key encapsulationconst kyber = await tenzro.crypto.generateKeyPair({algorithm: 'kyber768', // or 'kyber1024'});const { ciphertext, sharedSecret } = await tenzro.crypto.encapsulate({publicKey: kyber.publicKey,});const decapsulated = await tenzro.crypto.decapsulate({ciphertext,privateKey: kyber.privateKey,});
Secure Random
// Generate random bytesconst random = await tenzro.crypto.randomBytes(32);// Generate random number in rangeconst number = await tenzro.crypto.randomInt(1, 100);// Generate UUIDconst uuid = await tenzro.crypto.uuid();
Key Storage
// Store key securelyawait tenzro.crypto.storeKey({id: 'my-signing-key',key: privateKey,metadata: { purpose: 'signing' },});// Retrieve keyconst stored = await tenzro.crypto.getKey('my-signing-key');// List keysconst keys = await tenzro.crypto.listKeys();// Delete keyawait tenzro.crypto.deleteKey('my-signing-key');
Best Practices
- Use Ed25519 for general-purpose signatures
- Use Dilithium for post-quantum security
- Use Blake3 for hashing (faster than SHA-256)
- Use Argon2id for password hashing
- Never reuse nonces for encryption
- Store keys in secure enclaves for sensitive applications