digest
The digest namespace provides one-shot and incremental hashing.
tyrus
import { CryptoError, digest } from "tyrus:crypto";
import { utf8 } from "tyrus:encoding";
const data: u8[] = utf8.encode("hello world");
const hash: Result<u8[], CryptoError> = digest.sha256(data);
match (hash) { Ok(bytes) => { print(bytes.length); } Err(error) => { printError(error.code); } }Functions
| Function | Signature |
|---|---|
digest.sha256 | (string | u8[]) => Result<u8[], CryptoError> |
digest.sha384 | (string | u8[]) => Result<u8[], CryptoError> |
digest.sha512 | (string | u8[]) => Result<u8[], CryptoError> |
digest.md5 | (string | u8[]) => Result<u8[], CryptoError> |
digest.create | (string) => Result<DigestStream, CryptoError> |
ATTENTION
digest.md5 exists only for compatibility checksums; MD5 is collision-broken and must not protect passwords, signatures, certificates, or security-sensitive integrity.
DigestStream
digest.create("sha256" | "sha384" | "sha512") returns a Result<DigestStream, CryptoError> for incremental hashing:
| Method | Signature | Description |
|---|---|---|
update | (string | u8[]) => Result<DigestStream, CryptoError> | Feed data, returns self |
digest | () => Result<u8[], CryptoError> | Finalize and return hash |
reset | () => Result<DigestStream, CryptoError> | Reset state, returns self |
dispose | () => void | Deterministic early release |
The native handle is released automatically at the end of its lexical scope; dispose() permits deterministic early release and must be called at most once. Unknown algorithms are rejected with ERR_CRYPTO_DIGEST_ALGORITHM.
tyrus
import { digest } from "tyrus:crypto";
import { utf8 } from "tyrus:encoding";
const stream: DigestStream = digest.create("sha256");
stream.update(utf8.encode("hello "));
stream.update(utf8.encode("world"));
const hash: u8[] = stream.digest();