std/crypto/sha256

The std/crypto/sha256 module implements the SHA-256 cryptographic hash algorithm as specified in FIPS 180-4.

Overview

Usage

import "std/crypto/sha256.zc"
import "std/io.zc"

fn main() {
    // Simple hashing
    let hash = Sha256::hash("hello world");
    println(f"SHA-256: {hash}");
    
    // Incremental hashing
    let ctx = Sha256::new();
    ctx.update((u8*)"hello", 5);
    ctx.update((u8*)" world", 6);
    
    let digest: u8[32];
    ctx.finalize(&digest[0]);
}

Struct Definition

struct Sha256 {
    state: u32[8];
    buffer: u8[64];
    datalen: u32;
    bitlen: u64;
}

Methods

Sha256 Methods

MethodSignatureDescription
newSha256::new() -> Sha256Initializes a new SHA-256 context.
updateupdate(self, data: u8*, len: usize)Updates the context with a chunk of data.
finalizefinalize(self, hash: u8*)Finalizes the hash and writes the result to the provided 32-byte buffer.
hashSha256::hash(data: char*) -> StringConvenience method to hash a string and return its hexadecimal representation.