std.crypto.keccak
Keccak-f[1600]-Permutation (NIST FIPS 202), 24 Runden. Implementiert SHA-3/256, SHA-3/512, SHAKE-128 und SHAKE-256. Intern genutzt von ML-KEM, ML-DSA und SLH-DSA — direkt nutzbar wenn SHA-3 oder XOF-Output benötigt wird.
import std.crypto.keccak;
→ std.crypto · Standard Library
Konstanten
| Konstante | Wert | Bedeutung |
|---|---|---|
KECCAK_STATE_SIZE | 200 | Zustandsgröße in Bytes (1600 Bit = 25 × uint64) |
Interne Rates (nicht als Konstanten, aber relevant für Low-Level-API):
| Funktion | Rate | Kapazität | Output |
|---|---|---|---|
| SHA3_256 | 136 Bytes | 512 Bit | 32 Bytes |
| SHA3_512 | 72 Bytes | 1024 Bit | 64 Bytes |
| SHAKE128 | 168 Bytes | 256 Bit | variabel |
| SHAKE256 | 136 Bytes | 512 Bit | variabel |
Funktionen
High-Level (empfohlen):
| Funktion | Beschreibung |
|---|---|
SHA3_256(data, len, out) | SHA-3/256-Hash: 32 Bytes Output. FIPS 202, Pad 0x06 |
SHA3_512(data, len, out) | SHA-3/512-Hash: 64 Bytes Output. FIPS 202, Pad 0x06 |
SHAKE128(data, len, out, outLen) | SHAKE-128 XOF: beliebig viele Output-Bytes. Pad 0x1F |
SHAKE256(data, len, out, outLen) | SHAKE-256 XOF: beliebig viele Output-Bytes. Pad 0x1F |
Low-Level (für Streaming oder eigene Konstruktionen):
| Funktion | Beschreibung |
|---|---|
KeccakInit(state) | Setzt 200-Byte-State auf 0 |
KeccakAbsorb(state, data, len, rate, pad) | Absorb-Phase mit Multi-Rate-Padding |
KeccakSqueeze(state, out, outLen, rate) | Squeeze-Phase: extrahiert outLen Bytes |
Verwendungsbeispiele
SHA-3/256 (feste Ausgabelänge):
import std.crypto.keccak;
import std.alloc;
fn main(): int64 {
var msg: pchar := "Hello, SHA-3!"c;
var hash: int64 := alloc(32);
SHA3_256(msg as int64, 13, hash);
// hash[0..31] = SHA-3/256-Digest
free(hash, 32);
return 0;
}
SHAKE-256 als XOF (beliebige Ausgabelänge, z. B. 64 Bytes):
import std.crypto.keccak;
import std.alloc;
fn main(): int64 {
var seed: pchar := "my-seed-data"c;
var out: int64 := alloc(64);
SHAKE256(seed as int64, 12, out, 64);
// out[0..63] = 64 Bytes SHAKE-256-Output
free(out, 64);
return 0;
}
Low-Level Streaming (große Nachrichten, blockweise):
import std.crypto.keccak;
import std.alloc;
fn hashLarge(data: int64, len: int64, out: int64) {
var state: int64 := alloc(KECCAK_STATE_SIZE);
KeccakInit(state);
KeccakAbsorb(state, data, len, 136, 0x06); // rate=136, pad=SHA-3
KeccakSqueeze(state, out, 32, 136);
free(state, KECCAK_STATE_SIZE);
}
SHA-3 vs. SHA-256 Vergleich
| Eigenschaft | SHA-256 (SHA-2) | SHA-3/256 (Keccak) |
|---|---|---|
| Standard | FIPS 180-4 | FIPS 202 |
| Konstruktion | Merkle-Damgård | Sponge (Keccak) |
| Output-Länge | 32 Bytes (fest) | 32 Bytes (fest) |
| Strukturell unabhängig von SHA-2 | Nein | Ja |
| XOF-Modus | Nein | Ja (SHAKE) |
| Typischer Einsatz | TLS, Zertifikate | PQC, ZKP, SHAKE-basierte PRFs |
→ Für klassisches Hashing: std.crypto.sha256 (schneller auf x86_64 mit SHA-NI).
Letzte Aktualisierung: 2026-06-08
