Nyx ships SHA-256, MD5, and HMAC-SHA256 as built-in functions backed by the runtime's crypto.c module. No external library or import is required — all three are available in every Nyx program.
// Criptografía: SHA-256, MD5 y HMAC-SHA256 // bool -> texto: int_to_string() espera un int y un bool no se convierte solo, // así que la conversión se escribe explícita. fn si_no(b: bool) -> String { if b { return "si" } return "no" } fn main() -> int { // SHA-256 let h1: String = sha256("hello world") print("sha256(\"hello world\"):") print(" " + h1) print(" longitud: " + int_to_string(h1.length()) + " chars") // SHA-256 de string vacío let h_empty: String = sha256("") print("sha256(\"\"):") print(" " + h_empty) // MD5 let m1: String = md5("hello world") print("md5(\"hello world\"):") print(" " + m1) print(" longitud: " + int_to_string(m1.length()) + " chars") // HMAC-SHA256 let key: String = "clave-secreta" let data: String = "mensaje importante" let hmac: String = hmac_sha256(key, data) print("hmac_sha256(key, data):") print(" " + hmac) // Verificar determinismo: mismo input -> mismo hash let a: String = sha256("nyx") let b: String = sha256("nyx") print("sha256 es determinista: " + si_no(a == b)) // Verificar que inputs distintos -> hashes distintos let c: String = sha256("aaa") let d: String = sha256("bbb") print("hashes distintos para inputs distintos: " + si_no(c != d)) return 0 }
sha256("hello world"):
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
longitud: 64 chars
sha256(""):
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
md5("hello world"):
5eb63bbbe01eeed093cb22bb8f5acdc3
longitud: 32 chars
hmac_sha256(key, data):
5f7881b8e97ca2ea9adbf0c50a0ccf6207eb8923b5e91e2810b292f7b20bae18
sha256 es determinista: si
hashes distintos para inputs distintos: siHow it works
sha256(s) returns a 64-character lowercase hexadecimal string — the standard encoding of a 256-bit (32-byte) digest. The empty-string hash e3b0c44... is a well-known constant you can use to verify your toolchain is computing correctly. SHA-256 is suitable for data integrity checks, password hashing (with a proper KDF on top), and digital signatures.
md5(s) returns a 32-character hex string (128-bit digest). MD5 is no longer considered cryptographically secure for collision resistance, but it remains widely used for checksums and cache keys where collision attacks are not a concern.
hmac_sha256(key, data) computes a keyed hash that proves both the content of data and knowledge of key. It is the standard primitive for API request signing and JWT verification. The determinism assertions at the end demonstrate a fundamental property of all hash functions: identical inputs always produce identical outputs.