UNPKG

@socketsecurity/lib

Version:

Core utilities and infrastructure for Socket.dev security tools

96 lines (95 loc) 3.11 kB
/** * @fileoverview SSRI (Subresource Integrity) hash format utilities. * Provides conversion and validation for SSRI and hex hash formats. */ /** * Convert SSRI format hash to hex format. * * Takes a hash in SSRI format (e.g., "sha256-base64hash") and converts it to * standard hex format (e.g., "hexstring"). * * @param ssri - Hash in SSRI format (algorithm-base64) * @returns Hex string representation of the hash * @throws Error if SSRI format is invalid * * @example * ```typescript * const hex = ssriToHex('sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=') * // Returns: '76682a9fc3bbe62975176e2541f39a8168877d828d5cad8b56461fc36ac2b856' * ``` */ /*@__NO_SIDE_EFFECTS__*/ export declare function ssriToHex(ssri: string): string; /** * Convert hex format hash to SSRI format. * * Takes a hash in hex format and converts it to SSRI format with the specified * algorithm prefix (defaults to sha256). * * @param hex - Hash in hex format * @param algorithm - Hash algorithm (default: 'sha256') * @returns SSRI format hash (algorithm-base64) * @throws Error if hex format is invalid * * @example * ```typescript * const ssri = hexToSsri('76682a9fc3bbe62975176e2541f39a8168877d828d5cad8b56461fc36ac2b856') * // Returns: 'sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=' * ``` */ /*@__NO_SIDE_EFFECTS__*/ export declare function hexToSsri(hex: string, algorithm?: string): string; /** * Check if a string is valid SSRI format. * * Validates that a string matches the SSRI format pattern (algorithm-base64). * Does not verify that the base64 encoding is valid. * * @param value - String to validate * @returns True if string matches SSRI format * * @example * ```typescript * isValidSsri('sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=') // true * isValidSsri('76682a9f...') // false * ``` */ /*@__NO_SIDE_EFFECTS__*/ export declare function isValidSsri(value: string): boolean; /** * Check if a string is valid hex format. * * Validates that a string contains only hexadecimal characters (0-9, a-f). * Does not verify hash length or algorithm. * * @param value - String to validate * @returns True if string is valid hex format * * @example * ```typescript * isValidHex('76682a9fc3bbe62975176e2541f39a8168877d828d5cad8b56461fc36ac2b856') // true * isValidHex('sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=') // false * ``` */ /*@__NO_SIDE_EFFECTS__*/ export declare function isValidHex(value: string): boolean; /** * Parse SSRI format into components. * * Extracts the algorithm and base64 hash from an SSRI string. * * @param ssri - Hash in SSRI format * @returns Object with algorithm and base64Hash properties * @throws Error if SSRI format is invalid * * @example * ```typescript * const { algorithm, base64Hash } = parseSsri('sha256-dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=') * // Returns: { algorithm: 'sha256', base64Hash: 'dmgqn8O75il1F24lQfOagWiHfYKNXK2LVkYfw2rCuFY=' } * ``` */ /*@__NO_SIDE_EFFECTS__*/ export declare function parseSsri(ssri: string): { algorithm: string; base64Hash: string; };