Module: crypto.sha256

Source: ./crypto/sha256.reef


Overview

crypto/sha256 - SHA-256 message-digest algorithm

Implements SHA-256 (FIPS 180-4) hash function. Produces 256-bit (32-byte) digest.

Simple Usage: import crypto.sha256 let hash = sha256("Hello, World!") // Returns 64-char hex string let hash = sha256_bytes(data) // Hash byte array

File Hashing (memory efficient, streams in chunks): let hash = sha256_file("/path/to/file") // Hash file without loading into memory

Streaming API (for incremental hashing): let ctx = sha256_init() // Initialize context sha256_update(ctx, chunk1) // Add data sha256_update(ctx, chunk2) // Add more data let hash = sha256_finalize(ctx) // Get final hash


Types

Sha256Context

Context for streaming SHA-256 computation

Fields:

Name Type
state [int]
buffer [int]
buffer_len int
total_len int
work [int]

Functions

fn sha256_k(t: int): int

K constants - first 32 bits of fractional parts of cube roots of first 64 primes

fn sha256_ch(x: int, y: int, z: int): int

Ch(x, y, z) = (x AND y) XOR (NOT x AND z)

fn sha256_maj(x: int, y: int, z: int): int

Maj(x, y, z) = (x AND y) XOR (x AND z) XOR (y AND z)

fn sha256_sigma0(x: int): int

Sigma0(x) = ROTR2(x) XOR ROTR13(x) XOR ROTR22(x)

fn sha256_sigma1(x: int): int

Sigma1(x) = ROTR6(x) XOR ROTR11(x) XOR ROTR25(x)

fn sha256_lsigma0(x: int): int

sigma0(x) = ROTR7(x) XOR ROTR18(x) XOR SHR3(x)

fn sha256_lsigma1(x: int): int

sigma1(x) = ROTR17(x) XOR ROTR19(x) XOR SHR10(x)

fn sha256(data: string): string

fn sha256_hex(data: string): string

fn sha256_hex_lower(data: string): string

fn sha256_bytes(data: [uint8]): string

SHA-256 for binary data (byte arrays) - handles null bytes correctly

fn sha256_hex_digits(): string

fn sha256_state_to_hex(state: [int]): string

Convert SHA-256 state to 64-char hex string (big-endian byte order)

fn sha256_init(): Sha256Context

Initialize a new SHA-256 context

fn sha256_finalize(ctx: Sha256Context): string

Finalize and return the hash

fn sha256_file(path: string): string

Hash a file using streaming (memory efficient for large files) Returns empty string on error


Procedures

proc sha256_process_block(state: [int], block: [int], w: [int])

proc sha256_update(ctx: Sha256Context, data: [uint8])

Update context with more data


Generated by reefc doc