Saltar al contenido

Hashes (std::hash)

En esta página

std::hash reúne funciones de digest determinísticas: algoritmos SHA y MD5 para la huella digital de contenido, CRC32 para checksums rápidos, y HMAC para autenticación de mensajes con clave secreta. Todas las funciones son cómputos puros y se ejecutan en el sandbox.


SHA-256, SHA-512, SHA-1 y MD5

Cada función devuelve el digest en hexadecimal en minúsculas. SHA-256 es la elección estándar; MD5 y SHA-1 solo para compatibilidad con sistemas heredados. El determinismo garantiza que la misma entrada siempre produce la misma salida.

Digests determinísticos con SHA-256/512/1, MD5 y CRC32.

01-sha-md5.zolo
Playground
// Feature: hash.sha256 / hash.sha1 / hash.md5 — hash functions

// When to use: content identity (caching, dedupe), checksums.

// SHA-256 is the default choice; MD5 and SHA-1 only for legacy compatibility.


use std::hash

// Each function returns the lowercase hexadecimal digest representation.

print(hash.sha256("hello"))  // expected: 64 hex chars (2cf24...e9c8c)

print(hash.sha512("hello").len())  // expected: 128

print(hash.sha1("hello"))  // expected: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

print(hash.md5("hello"))  // expected: 5d41402abc4b2a76b9719d911017c592


// Hashes are deterministic: same input -> same output.

print(hash.sha256("zolo") == hash.sha256("zolo"))  // expected: true

print(hash.sha256("a") == hash.sha256("b"))  // expected: false


// CRC32: fast, non-cryptographic — good for detecting corruption, bad

// for security.

print(hash.crc32("hello"))  // expected: int


HMAC — autenticación con clave

hash.hmac(algo, clave, mensaje) produce un código de autenticación que no puede ser falsificado sin la clave secreta, incluso si el atacante conoce el algoritmo de hash. Usos típicos: validación de webhooks, cookies firmadas y tokens HS256.

HMAC-SHA256 para validar webhooks; comparación de MACs con claves distintas.

02-hmac.zolo
Playground
// Feature: hash.hmac — Hash-based Message Authentication Code

// When to use: authenticate payloads (webhooks, HS* JWTs, signed cookies).

// Tamper-resistant — even knowing the hash, you can't forge it without the key.


use std::hash

let secret = "my-secret-key"
let payload = "user_id=42&action=transfer"

// hmac(algo, key, message) -> hex digest.

let mac = hash.hmac("sha256", secret, payload)
print(mac.len())  // expected: 64 hex chars


// A different key produces a completely different HMAC (avalanche effect).

let mac2 = hash.hmac("sha256", "another-key", payload)
print(mac == mac2)  // expected: false


// Useful to validate webhooks: compare the header against the expected HMAC.

fn validate_webhook(body: str, signature: str, key: str) -> bool {
  return hash.hmac("sha256", key, body) == signature
}

let ok = validate_webhook(payload, mac, secret)
print(ok)  // expected: true

Desafío

Implementa una función sign_jwt_payload(payload, secret) que devuelva base64_url(header) + "." + base64_url(payload) + "." + hmac_sha256(...). Usa std::hash y std::base64 juntos.

Buscar en Zolo

9 resultados

enespt-br