/** * @fileoverview Shared Cryptographic Utilities * @module @/lib/utils/crypto-utils * * Centralized cryptographic utilities for the Harbor React Framework. * This module consolidates previously duplicated crypto functions from: * - security/csrf-protection.ts * - security/csp-manager.ts * - security/secure-storage.ts * * All cryptographic operations use the Web Crypto API for maximum security. * * @see https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API * @see https://owasp.org/www-project-web-security-testing-guide/ */ /** * Generate cryptographically secure random bytes * * Uses crypto.getRandomValues() which provides cryptographically strong * random values suitable for key generation and token creation. * * @param length - Number of random bytes to generate * @returns Uint8Array of random bytes * * @example * const bytes = getRandomBytes(32); // 256 bits of entropy */ export declare function getRandomBytes(length: number): Uint8Array; /** * Generate a random hex string of specified byte length * * @param byteLength - Number of random bytes (output will be 2x characters) * @returns Hex-encoded random string * * @example * const hex = generateRandomHex(16); // 32-character hex string */ export declare function generateRandomHex(byteLength: number): string; /** * Generate a random base64url-encoded string of specified byte length * * @param byteLength - Number of random bytes * @returns Base64url-encoded random string * * @example * const token = generateRandomBase64Url(32); // 43-character base64url string */ export declare function generateRandomBase64Url(byteLength: number): string; /** * Convert bytes to hexadecimal string * * @param bytes - Uint8Array to convert * @returns Lowercase hex-encoded string * * @example * bytesToHex(new Uint8Array([255, 0, 128])) // "ff0080" */ export declare function bytesToHex(bytes: Uint8Array): string; /** * Convert hexadecimal string to bytes * * @param hex - Hex-encoded string * @returns Uint8Array of bytes * @throws Error if hex string is invalid * * @example * hexToBytes("ff0080") // Uint8Array([255, 0, 128]) */ export declare function hexToBytes(hex: string): Uint8Array; /** * Convert bytes to standard base64 string * * @param bytes - Uint8Array to convert * @returns Base64-encoded string * * @example * bytesToBase64(new Uint8Array([72, 101, 108, 108, 111])) // "SGVsbG8=" */ export declare function bytesToBase64(bytes: Uint8Array): string; /** * Convert base64 string to bytes * * @param base64 - Base64-encoded string * @returns Uint8Array of bytes * @throws Error if base64 string is invalid * * @example * base64ToBytes("SGVsbG8=") // Uint8Array([72, 101, 108, 108, 111]) */ export declare function base64ToBytes(base64: string): Uint8Array; /** * Convert bytes to URL-safe base64 string (base64url) * * URL-safe base64 replaces + with -, / with _, and removes padding. * Safe for use in URLs, cookies, and HTML attributes. * * @param bytes - Uint8Array to convert * @returns Base64url-encoded string (no padding) * * @example * bytesToBase64Url(new Uint8Array([255, 255])) // "__8" */ export declare function bytesToBase64Url(bytes: Uint8Array): string; /** * Convert URL-safe base64 string to bytes * * @param base64url - Base64url-encoded string * @returns Uint8Array of bytes * * @example * base64UrlToBytes("__8") // Uint8Array([255, 255]) */ export declare function base64UrlToBytes(base64url: string): Uint8Array; /** * Constant-time string comparison to prevent timing attacks * * Timing attacks can reveal secret values by measuring comparison time. * This function always takes the same amount of time regardless of * where strings differ. * * @param a - First string to compare * @param b - Second string to compare * @returns true if strings are equal, false otherwise * * @example * constantTimeCompare(userToken, storedToken) */ export declare function constantTimeCompare(a: string, b: string): boolean; /** * Constant-time byte array comparison * * @param a - First byte array to compare * @param b - Second byte array to compare * @returns true if arrays are equal, false otherwise */ export declare function constantTimeCompareBytes(a: Uint8Array, b: Uint8Array): boolean; /** * Compute SHA-256 hash of data * * @param data - String or bytes to hash * @returns Promise resolving to hex-encoded hash * * @example * const hash = await sha256("hello world"); */ export declare function sha256(data: string | Uint8Array): Promise; /** * Compute SHA-512 hash of data * * @param data - String or bytes to hash * @returns Promise resolving to hex-encoded hash */ export declare function sha512(data: string | Uint8Array): Promise; /** * Generate a cryptographically secure token * * Default generates 32 bytes (256 bits) of entropy, base64url encoded. * * @param byteLength - Number of random bytes (default: 32) * @returns Secure random token string * * @example * const csrfToken = generateSecureToken(); * const sessionId = generateSecureToken(64); */ export declare function generateSecureToken(byteLength?: number): string; /** * Generate a nonce for Content Security Policy * * Generates a 16-byte (128-bit) base64-encoded nonce suitable for CSP. * * @returns Base64-encoded nonce string * * @example * const nonce = generateCSPNonce(); * // Use in CSP header: script-src 'nonce-{nonce}' */ export declare function generateCSPNonce(): string; /** * Derive a key from a password using PBKDF2 * * Uses PBKDF2-SHA256 with configurable iterations for password-based * key derivation. Suitable for encrypting user data with user passwords. * * @param password - User password * @param salt - Salt bytes (should be random per-user) * @param iterations - Number of PBKDF2 iterations (default: 100000) * @param keyLength - Output key length in bytes (default: 32) * @returns Promise resolving to derived key bytes * * @example * const salt = getRandomBytes(16); * const key = await deriveKey(password, salt); */ export declare function deriveKey(password: string, salt: Uint8Array, iterations?: number, keyLength?: number): Promise; export type {};