Module std::crypto

Cryptographic hashing and utilities.

Provides SHA-2 hash functions (SHA-256, SHA-384, SHA-512), HMAC-SHA-256 message authentication, secure random byte generation, and constant-time comparison.

Examples

import std::crypto::crypto;
import std::encoding::hex;

fn main() {
    let data: bytes = bytes::new();
    data.push(65);  // 'A'
    let hash = crypto.sha256(data);
    println(hex.encode(hash));
}

Contents

Functions

Function sha256

pub fn sha256(data: bytes) -> bytes

Compute the SHA-256 hash of data, returning a 32-byte bytes value.

Function sha384

pub fn sha384(data: bytes) -> bytes

Compute the SHA-384 hash of data, returning a 48-byte bytes value.

Function sha512

pub fn sha512(data: bytes) -> bytes

Compute the SHA-512 hash of data, returning a 64-byte bytes value.

Function hmac_sha256

pub fn hmac_sha256(key: bytes, data: bytes) -> bytes

Compute HMAC-SHA-256 with the given key and data.

Returns a 32-byte bytes value.

Function random_bytes

pub fn random_bytes(len: i64) -> bytes

Generate len cryptographically random bytes.

Function constant_time_eq

pub fn constant_time_eq(a: bytes, b: bytes) -> bool

Compare two bytes values in constant time.

Returns true if they are equal (same length and same content). Always runs in time proportional to the length to avoid timing leaks.