SHA-256 cryptographic hash function implementation for TypeScript and JavaScript
91
Build a hash state manager that processes data in stages and properly handles errors when invalid operations are attempted on finalized hashes.
Implement a HashStateManager class that manages the lifecycle of hash operations:
Multi-stage processing: Process data through multiple stages, where each stage adds data to the hash before finalization.
State lifecycle enforcement: Once a hash is finalized (digest computed), detect and handle attempts to update it with more data.
Error detection: Catch errors when trying to perform invalid operations (like updating a finalized hash) and track them for debugging.
Safe retry mechanism: Provide a way to safely attempt operations that might fail due to invalid state, returning success/failure status instead of throwing.
/**
* Manages hash state lifecycle with proper error handling
*/
export class HashStateManager {
/**
* Creates a new hash state manager
*/
constructor();
/**
* Adds data to the current hash state
* @param data - Data to add to hash
* @throws Error if hash is already finalized
*/
addData(data: Uint8Array): void;
/**
* Finalizes the hash and returns the digest
* @returns The computed hash digest
*/
finalize(): Uint8Array;
/**
* Safely attempts to add data, catching any errors
* @param data - Data to add
* @returns true if successful, false if operation failed
*/
tryAddData(data: Uint8Array): boolean;
/**
* Gets the last error message encountered
* @returns Error message string or null if no error
*/
getLastError(): string | null;
/**
* Resets to a fresh hash state
*/
reset(): void;
}Provides cryptographic hashing functionality with state management.
Install with Tessl CLI
npx tessl i tessl/npm-stablelib--sha256docs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10