std/crypto/sha256
The std/crypto/sha256 module implements the SHA-256 cryptographic hash algorithm as specified in FIPS 180-4.
Overview
- Hashing: Compute 256-bit hashes of arbitrary string or byte data.
- Incremental Hashing: Process data in chunks using
updateandfinalize. - Hex Output: Convenient
hashhelper that returns a hexadecimal string.
Usage
import "std/crypto/sha256.zc"
import "std/io.zc"
fn main() {
// Simple hashing
let hash = Sha256::hash("hello world");
println(f"SHA-256: ");
// 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
| Method | Signature | Description |
|---|---|---|
| new | Sha256::new() -> Sha256 | Initializes a new SHA-256 context. |
| update | update(self, data: u8*, len: usize) | Updates the context with a chunk of data. |
| finalize | finalize(self, hash: u8*) | Finalizes the hash and writes the result to the provided 32-byte buffer. |
| hash | Sha256::hash(data: char*) -> String | Convenience method to hash a string and return its hexadecimal representation. |