CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-sodium-native

Low level bindings for libsodium cryptographic library

Pending
Overview
Eval results
Files

random.mddocs/

Random Number Generation

Cryptographically secure random number generation for keys, nonces, salts, and other random values in sodium-native.

Capabilities

Random Buffer Generation

Fills a buffer with cryptographically secure random bytes.

/**
 * Fill buffer with cryptographically secure random bytes
 * @param buffer - Buffer to fill with random data
 */
function randombytes_buf(buffer: Buffer): void;

Usage Example:

const sodium = require('sodium-native');

// Generate random key
const key = Buffer.alloc(32);
sodium.randombytes_buf(key);

// Generate random nonce
const nonce = Buffer.alloc(24);
sodium.randombytes_buf(nonce);

// Generate random salt
const salt = Buffer.alloc(16);
sodium.randombytes_buf(salt);

Deterministic Random Generation

Generates deterministic random bytes from a seed for reproducible randomness.

/**
 * Fill buffer with deterministic random bytes derived from seed
 * @param buffer - Buffer to fill with deterministic random data
 * @param seed - Seed buffer for deterministic generation
 */
function randombytes_buf_deterministic(buffer: Buffer, seed: Buffer): void;

Usage Example:

const sodium = require('sodium-native');

// Create a seed
const seed = Buffer.alloc(sodium.randombytes_SEEDBYTES);
sodium.randombytes_buf(seed);

// Generate deterministic random data
const deterministicData1 = Buffer.alloc(32);
const deterministicData2 = Buffer.alloc(32);

sodium.randombytes_buf_deterministic(deterministicData1, seed);
sodium.randombytes_buf_deterministic(deterministicData2, seed);

// deterministicData1 and deterministicData2 will be identical
console.log(deterministicData1.equals(deterministicData2)); // true

Constants

// Size of seed for deterministic random generation
const randombytes_SEEDBYTES: number;

Common Patterns

Key Generation

const sodium = require('sodium-native');

// Generate different types of keys
const secretboxKey = Buffer.alloc(sodium.crypto_secretbox_KEYBYTES);
const signSecretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES);
const hashKey = Buffer.alloc(sodium.crypto_generichash_KEYBYTES);

sodium.randombytes_buf(secretboxKey);
sodium.randombytes_buf(signSecretKey);
sodium.randombytes_buf(hashKey);

Nonce Generation

const sodium = require('sodium-native');

// Generate nonces for different operations
const secretboxNonce = Buffer.alloc(sodium.crypto_secretbox_NONCEBYTES);
const boxNonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES);
const streamNonce = Buffer.alloc(sodium.crypto_stream_NONCEBYTES);

sodium.randombytes_buf(secretboxNonce);
sodium.randombytes_buf(boxNonce);
sodium.randombytes_buf(streamNonce);

Salt Generation

const sodium = require('sodium-native');

// Generate salts for password hashing
const pwhashSalt = Buffer.alloc(sodium.crypto_pwhash_SALTBYTES);
const scryptSalt = Buffer.alloc(sodium.crypto_pwhash_scryptsalsa208sha256_SALTBYTES);

sodium.randombytes_buf(pwhashSalt);
sodium.randombytes_buf(scryptSalt);

Install with Tessl CLI

npx tessl i tessl/npm-sodium-native

docs

aead.md

auth.md

box.md

ed25519.md

hash.md

index.md

kdf.md

kx.md

memory.md

pwhash.md

random.md

secretbox.md

secretstream.md

shorthash.md

sign.md

stream.md

tile.json