SHA256 API Reference
Complete reference for all SHA256 functions and methods.Hash Functions
SHA256.hash(data: Uint8Array): Uint8Array
Compute SHA256 hash of input data.
One-shot hashing function for complete data available in memory.
Parameters:
data: Input data to hash (Uint8Array)
- Uses hardware acceleration (SHA-NI) when available
- Constant-time implementation resists timing attacks
- Optimal for data < 100MB; use streaming API for larger data
SHA256.hashString(str: string): Uint8Array
Hash UTF-8 encoded string with SHA256.
Convenience function that encodes string to UTF-8 bytes before hashing.
Parameters:
str: String to hash (UTF-8 encoded)
- UTF-8 encoding handles all Unicode codepoints correctly
- Multi-byte characters encoded properly
- Emoji and non-Latin scripts supported
SHA256.hashHex(hex: string): Uint8Array
Hash hex-encoded string with SHA256.
Decodes hex string to bytes before hashing. Accepts both “0x”-prefixed and unprefixed hex strings.
Parameters:
hex: Hex string to hash (with or without “0x” prefix)
- Error if hex string contains invalid characters
- Error if hex string has odd length (incomplete byte)
Streaming API
SHA256.create(): Hasher
Create incremental hasher for streaming data.
Returns stateful hasher instance for processing data in chunks. Useful for:
- Large files that don’t fit in memory
- Streaming network data
- Progressive hashing as data arrives
- Memory-efficient processing
update() and digest() methods
Example:
hasher.update(data: Uint8Array): void
Update hasher state with new data chunk.
Can be called multiple times to process data incrementally. Order matters - chunks are processed sequentially.
Parameters:
data: Data chunk to add to hash computation (Uint8Array)
- Can call
update()any number of times - Cannot call
update()afterdigest() - Chunk size doesn’t affect final hash (only performance)
hasher.digest(): Uint8Array
Finalize hash computation and return result.
Completes the hash computation and returns the final 32-byte digest. After calling digest(), the hasher cannot be reused.
Returns: 32-byte SHA256 hash (Uint8Array)
Example:
- Call
digest()only once per hasher instance - Creates a new hasher for subsequent hashing operations
Utility Functions
SHA256.toHex(hash: Uint8Array): string
Convert hash bytes to hex string representation.
Converts 32-byte hash to lowercase hex string with “0x” prefix.
Parameters:
hash: Hash bytes to convert (Uint8Array, typically 32 bytes)
- Always lowercase hex characters (a-f, not A-F)
- Always includes “0x” prefix
- Fixed-width: 32 bytes = 64 hex chars + “0x” = 66 total chars
Constants
SHA256.OUTPUT_SIZE
SHA256 output size in bytes.
Type: number
Value: 32
Example:
SHA256.BLOCK_SIZE
SHA256 internal block size in bytes.
Used internally for message padding and compression. Useful for understanding performance characteristics.
Type: number
Value: 64 (512 bits)
Example:
Type Definitions
Hasher
Incremental hasher interface returned by SHA256.create().
Advanced Usage
Double SHA256 (Bitcoin)
Bitcoin uses double SHA256 for block and transaction hashing:SHA256d (Double-SHA256)
HMAC-SHA256
Hash-based Message Authentication Code (not built-in, requires separate implementation):Progressive File Hashing
See Also
- SHA256 Overview - Introduction and quick start
- Test Vectors - NIST test vectors
- Security - Security properties and considerations
- Performance - Benchmarks and optimization
- Usage Patterns - Common use cases
- Comparison - vs Keccak256, Blake2, RIPEMD160

