crypt.hash
Hashes data with the specified algorithm.
function crypt.hash(data: string, algorithm: string): stringSynopsis
How it works
Computes a cryptographic hash digest of the input data using the specified algorithm. The input is processed through the hash function’s compression rounds and the resulting digest is returned as a hexadecimal string.
Supported algorithms
| Algorithm | Digest Size | Hex Chars | Security |
|---|---|---|---|
md5 | 128 bits | 32 | Broken — collision attacks exist. Integrity checks only |
sha1 | 160 bits | 40 | Broken. Avoid for security-critical use |
sha256 | 256 bits | 64 | Strong. SHA-2SHA (hash family)Secure Hash Algorithm family: SHA-1 (160-bit, broken), SHA-256/SHA-384/SHA-512 (SHA-2 family, secure), SHA3-256 (Keccak-based). Used by crypt.hash() to produce fixed-size digests from arbitrary input data. family, Merkle–DamgårdMerkle–DamgårdA construction used by MD5, SHA-1, and SHA-2 family hashes. Processes input in fixed-size blocks through a compression function, chaining intermediate hash values. Vulnerable to length-extension attacks. construction |
sha384 | 384 bits | 96 | SHA-512 truncated. Extra collision resistance |
sha512 | 512 bits | 128 | SHA-2 family. 80 rounds of compression |
sha3-256 | 256 bits | 64 | Keccak spongeKeccak spongeThe permutation-based construction used by SHA-3. "Absorbs" input blocks into a state array, then "squeezes" output. Immune to length-extension attacks unlike Merkle–Damgård hashes. construction (different design than SHA-2) |
sha3-512 | 512 bits | 128 | Keccak sponge, 1600-bit state |
Usage
MD5 hash
print(crypt.hash("Hello, World!", "md5")) "cc">--> 65A8E27D8879283831B664BD8B7F0AD4Parameters
data string The data to hash.
algorithm string Hash algorithm name (e.g., "sha256", "md5").
Returns
string Uppercase hex-encoded hash digest.