A collection of useful crypto utilities for Polkadot ecosystem projects
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Cryptographically secure random number generation in various formats for keys, nonces, and other security-critical randomness.
Generate cryptographically secure random bytes.
/**
* Generate random bytes
* @param bitLength - Length in bits (default: 256)
* @returns Random bytes as Uint8Array
*/
function randomAsU8a(bitLength?: number): Uint8Array;
/**
* Generate random hex string
* @param bitLength - Length in bits (default: 256)
* @returns Random hex string with 0x prefix
*/
function randomAsHex(bitLength?: number): string;
/**
* Generate random number
* @param bitLength - Length in bits (default: 53)
* @returns Random number within JavaScript safe integer range
*/
function randomAsNumber(bitLength?: number): number;Usage Examples:
import { randomAsU8a, randomAsHex, randomAsNumber } from "@polkadot/util-crypto";
// Generate 32 random bytes (256 bits)
const randomBytes = randomAsU8a(256);
console.log(randomBytes.length); // 32
// Generate random hex string
const randomHex = randomAsHex(128); // 16 bytes as hex
console.log(randomHex); // "0x..."
// Generate random number (up to 53 bits for JavaScript safety)
const randomNum = randomAsNumber(32);
console.log(randomNum); // Random 32-bit integerInstall with Tessl CLI
npx tessl i tessl/npm-polkadot--util-crypto