std/crypto/sha256
Il modulo std/crypto/sha256 implementa l'algoritmo di hash crittografico SHA-256 come specificato in FIPS 180-4.
Panoramica
- Hashing: Calcola hash a 256 bit di stringhe arbitrarie o dati in byte.
- Hashing Incrementale: Elabora i dati in blocchi utilizzando
updateefinalize. - Output Esadecimale: Pratico helper
hashche restituisce una stringa esadecimale.
Utilizzo
import "std/crypto/sha256.zc"
import "std/io.zc"
fn main() {
// Hashing semplice
let hash = Sha256::hash("hello world");
println(f"SHA-256: ");
// Hashing incrementale
let ctx = Sha256::new();
ctx.update((u8*)"hello", 5);
ctx.update((u8*)" world", 6);
let digest: u8[32];
ctx.finalize(&digest[0]);
}
Definizione Struct
struct Sha256 {
state: u32[8];
buffer: u8[64];
datalen: u32;
bitlen: u64;
}
Metodi
Metodi di Sha256
| Metodo | Firma | Descrizione |
|---|---|---|
| new | Sha256::new() -> Sha256 | Inizializza un nuovo contesto SHA-256. |
| update | update(self, data: u8*, len: usize) | Aggiorna il contesto con un blocco di dati. |
| finalize | finalize(self, hash: u8*) | Finalizza l'hash e scrive il risultato nel buffer da 32 byte fornito. |
| hash | Sha256::hash(data: char*) -> String | Metodo pratico per eseguire l'hash di una stringa e restituire la sua rappresentazione esadecimale. |