/** * Platform-agnostic crypto utilities for browser and Node.js environments. * * This module provides cryptographic functions that work in both browser and Node.js * environments, abstracting away the differences between Web Crypto API and Node.js crypto. * * **Platform Detection Boundary:** * This file uses `isBrowserEnvironment()` and `isNodeEnvironment()` to detect the * runtime platform and conditionally load the appropriate crypto implementation: * - Browser: uses `window.crypto.getRandomValues()` (Web Crypto API) * - Node.js: uses `require('crypto').randomBytes` (loaded lazily via `require()`) * * The Node.js `crypto` module is never imported at the top level; it is loaded * dynamically inside `getRandomBytes()` only when `isBrowserEnvironment()` returns * false. This ensures the module can be bundled for browsers without triggering * missing-module errors. * * SHA-1 hashing uses `@noble/hashes/sha1`, which is a pure-JS implementation * that works identically in both environments — no platform branching needed. * * @platform Node.js (conditional — `getRandomBytes` falls back to Node.js `crypto` when not in a browser) * @module platformCrypto * @see Requirements 11.1, 11.2, 11.3, 11.4, 11.5 * @see Requirement 18.4 — platform-specific code isolated behind detection boundary */ /** * Interface for platform-agnostic crypto operations. */ export interface IPlatformCrypto { /** * Generate cryptographically secure random bytes. * @param length - Number of bytes to generate * @returns Uint8Array of random bytes */ randomBytes(length: number): Uint8Array; /** * Compute SHA-1 hash of input data. * @param data - String or Uint8Array to hash * @returns Uint8Array containing the hash */ sha1(data: string | Uint8Array): Uint8Array; } /** * Detects whether the code is running in a browser environment. * * @returns true if running in a browser with Web Crypto API available * @see Requirement 11.5 */ export declare function isBrowserEnvironment(): boolean; /** * Detects whether the code is running in a Node.js environment. * * @returns true if running in Node.js * @see Requirement 11.5 */ export declare function isNodeEnvironment(): boolean; /** * Generate cryptographically secure random bytes. * * Uses Web Crypto API (crypto.getRandomValues) in browser environments * and Node.js crypto.randomBytes in Node.js environments. * * Handles the Web Crypto API limit of 65,536 bytes per call by chunking * large requests. * * @param length - Number of bytes to generate (must be non-negative) * @returns Uint8Array of cryptographically secure random bytes * @throws Error if length is negative * @see Requirements 11.1, 11.2, 11.3 * * @example * ```typescript * const bytes = getRandomBytes(32); * console.log(bytes.length); // 32 * ``` */ export declare function getRandomBytes(length: number): Uint8Array; /** * Compute SHA-1 hash of input data. * * Uses @noble/hashes for cross-platform SHA-1 implementation that works * in both browser and Node.js environments. * * @param data - String or Uint8Array to hash * @returns Uppercase hexadecimal string representation of the SHA-1 hash * @see Requirement 11.4 * * @example * ```typescript * const hash = sha1Hash('password'); * console.log(hash); // '5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8' * ``` */ export declare function sha1Hash(data: string | Uint8Array): string; /** * Compute SHA-1 hash of input data and return as Uint8Array. * * Uses @noble/hashes for cross-platform SHA-1 implementation that works * in both browser and Node.js environments. * * @param data - String or Uint8Array to hash * @returns Uint8Array containing the SHA-1 hash (20 bytes) * @see Requirement 11.4 * * @example * ```typescript * const hashBytes = sha1HashBytes('password'); * console.log(hashBytes.length); // 20 * ``` */ export declare function sha1HashBytes(data: string | Uint8Array): Uint8Array; /** * Default implementation of IPlatformCrypto interface. * * Provides a class-based wrapper around the standalone functions * for use cases that prefer object-oriented patterns. */ export declare class PlatformCrypto implements IPlatformCrypto { /** * Generate cryptographically secure random bytes. * @param length - Number of bytes to generate * @returns Uint8Array of random bytes */ randomBytes(length: number): Uint8Array; /** * Compute SHA-1 hash of input data. * @param data - String or Uint8Array to hash * @returns Uint8Array containing the hash */ sha1(data: string | Uint8Array): Uint8Array; } //# sourceMappingURL=platformCrypto.d.ts.map