Skip to content

crypto

The crypto package computes message digests and HMACs. Each function takes a string, which it hashes as UTF-8, or bytes, which it hashes as they are, and returns a lowercase hexadecimal string.

println crypto::sha256_hash("kiwi")
# 1a5afeda973d776e31d1d7266f184468f84d99bed311d88d3dcb67015934f9f9
println crypto::md5_hash("kiwi")
# de5949721e6352f01dfef317c3e898a8
println crypto::hmac_sha256("secret-key", "message to sign")
FunctionAlgorithm
md5_hashMD5
sha1_hashSHA-1
sha224_hash, sha256_hash, sha384_hash, sha512_hashSHA-2
hmac_md5, hmac_sha256, hmac_sha512HMAC with that hash
hmac(algorithm, key, input)HMAC with a named algorithm
file_hash(path, algorithm)A file’s contents, with MD5, SHA-1, SHA-256 (the default), SHA-384 or SHA-512

file_hash reads the file in chunks, so it can hash files larger than memory, such as a download you want to check against a published checksum:

println crypto::file_hash("kiwi-linux-x64.tar.gz") # SHA-256
println crypto::file_hash("kiwi-linux-x64.tar.gz", "SHA512")

file_hash is unavailable in safe mode.

MD5 and SHA-1 are fine for checksums and cache keys but are no longer safe against deliberate tampering; use SHA-256 or better for anything security-related. These functions hash data; they are not suitable for storing passwords, which need a slow, salted algorithm.

random_bytes(n) returns n bytes from the operating system’s cryptographically secure generator, and uuid() returns a random (version 4) UUID built from the same source. Use them for tokens, keys and session identifiers; math::random is not suitable for those.

token = crypto::random_bytes(16).to_hex()
println token.size() # 32
println crypto::uuid().size() # 36

For Base64 and hex encoding, see encode.

crypto::md5_hash(input = "")

Computes the MD5 hex digest of a string or bytes. A string is hashed as UTF-8.

ParameterTypeDefaultDescription
inputstring|bytes""The input to hash (default "").

Returns string — A 32-character lowercase hexadecimal MD5 digest.

crypto::sha224_hash(input = "")

Computes the SHA-224 hex digest of a string or bytes. A string is hashed as UTF-8.

ParameterTypeDefaultDescription
inputstring|bytes""The input to hash (default "").

Returns string — A 56-character lowercase hexadecimal SHA-224 digest.

crypto::sha256_hash(input = "")

Computes the SHA-256 hex digest of a string or bytes. A string is hashed as UTF-8.

ParameterTypeDefaultDescription
inputstring|bytes""The input to hash (default "").

Returns string — A 64-character lowercase hexadecimal SHA-256 digest.

crypto::sha1_hash(input = "")

Computes the SHA-1 hex digest of a string or bytes. A string is hashed as UTF-8.

ParameterTypeDefaultDescription
inputstring|bytes""The input to hash (default "").

Returns string — A 40-character lowercase hexadecimal SHA-1 digest.

crypto::sha384_hash(input = "")

Computes the SHA-384 hex digest of a string or bytes. A string is hashed as UTF-8.

ParameterTypeDefaultDescription
inputstring|bytes""The input to hash (default "").

Returns string — A 96-character lowercase hexadecimal SHA-384 digest.

crypto::sha512_hash(input = "")

Computes the SHA-512 hex digest of a string or bytes. A string is hashed as UTF-8.

ParameterTypeDefaultDescription
inputstring|bytes""The input to hash (default "").

Returns string — A 128-character lowercase hexadecimal SHA-512 digest.

crypto::random_bytes(count: integer): bytes

Generates cryptographically secure random bytes, suitable for tokens, keys and session identifiers.

ParameterTypeDefaultDescription
countintegerHow many bytes to generate (0 to 1048576).

Returns bytes — The random bytes.

crypto::uuid(): string

Generates a random (version 4) UUID from a cryptographically secure source.

Returns string — A UUID in lowercase 36-character form, such as "3f2b8c1e-9a4d-4e7b-b1c2-5d6e7f8a9b0c".

crypto::file_hash(path, algorithm = "SHA256")

Computes the hex digest of a file's contents. The file is read in chunks, so it can be larger than memory. Unavailable in safe mode.

ParameterTypeDefaultDescription
pathstringThe file to hash.
algorithmstring"SHA256"One of "MD5", "SHA1", "SHA256", "SHA384" or "SHA512" (default "SHA256").

Returns string — Lowercase hexadecimal digest.

Throws FileReadError — If the file can't be read.

crypto::hmac(algorithm, key, input)

Computes an HMAC hex digest. Strings are hashed as UTF-8.

ParameterTypeDefaultDescription
algorithmstringOne of "MD5", "SHA256", "SHA512".
keystring|bytesThe HMAC key.
inputstring|bytesThe input.

Returns string — Lowercase hexadecimal HMAC digest.

crypto::hmac_sha256(key, input)

Computes HMAC-SHA256. Strings are hashed as UTF-8.

ParameterTypeDefaultDescription
keystring|bytesThe HMAC key.
inputstring|bytesThe input.

Returns string — Lowercase hexadecimal digest.

crypto::hmac_sha512(key, input)

Computes HMAC-SHA512. Strings are hashed as UTF-8.

ParameterTypeDefaultDescription
keystring|bytesThe HMAC key.
inputstring|bytesThe input.

Returns string — Lowercase hexadecimal digest.

crypto::hmac_md5(key, input)

Computes HMAC-MD5. Strings are hashed as UTF-8.

ParameterTypeDefaultDescription
keystring|bytesThe HMAC key.
inputstring|bytesThe input.

Returns string — Lowercase hexadecimal digest.

A pure-Kiwi implementation of the MD5 message-digest algorithm.

static MD5.hexdigest(input)

Computes the MD5 hex digest of a string.

ParameterTypeDefaultDescription
inputstringThe input string to hash.

Returns string — A 32-character lowercase hexadecimal MD5 digest.

static MD5.left_rotate(value, count)
ParameterTypeDefaultDescription
valueany
countany
static MD5.int_to_bytes(num)
ParameterTypeDefaultDescription
numany

A SHA-2 implementation based on the implementation in PyPy https://foss.heptapod.net/pypy/pypy/-/blob/branch/default/lib_pypy/_sha256.py

static SHA.BLOCKSIZE()
static SHA.DIGESTSIZE()
static SHA.new_shaobject()
static SHA.Ch(x, y, z)
ParameterTypeDefaultDescription
xany
yany
zany
static SHA.Maj(x, y, z)
ParameterTypeDefaultDescription
xany
yany
zany
static SHA.S(x, n)
ParameterTypeDefaultDescription
xany
nany
static SHA.R(x, n)
ParameterTypeDefaultDescription
xany
nany
static SHA.Sigma0(x)
ParameterTypeDefaultDescription
xany
static SHA.Sigma1(x)
ParameterTypeDefaultDescription
xany
static SHA.Gamma0(x)
ParameterTypeDefaultDescription
xany
static SHA.Gamma1(x)
ParameterTypeDefaultDescription
xany
static SHA.RND(a, b, c, d, e, f, g, h, i, ki, W)
ParameterTypeDefaultDescription
aany
bany
cany
dany
eany
fany
gany
hany
iany
kiany
Wany
static SHA.transform(sha_info)
ParameterTypeDefaultDescription
sha_infoany
static SHA.sha_init()
static SHA.sha224_init()
static SHA.getbuf(s)
ParameterTypeDefaultDescription
sany
static SHA.sha_update(sha_info, buffer)
ParameterTypeDefaultDescription
sha_infoany
bufferany
static SHA.sha_final(sha_info)
ParameterTypeDefaultDescription
sha_infoany

A pure-Kiwi SHA-256 hasher supporting incremental updates.

SHA256.new(s = null)

Creates a new SHA-256 hasher, optionally hashing an initial string.

ParameterTypeDefaultDescription
sstring|nonenullAn optional initial input string (default null).
update(s)

Feeds additional data into the hasher.

ParameterTypeDefaultDescription
sstringThe data to hash.
digest()

Returns the raw digest as a list of bytes.

Returns list — A list of integers representing the digest bytes.

hexdigest()

Returns the hex-encoded digest string.

Returns string — A 64-character lowercase hexadecimal SHA-256 digest.

copy()

Returns a copy of this hasher with the same internal state.

Returns SHA256 — A new independent SHA256 instance.

Inherits from SHA256.

A pure-Kiwi SHA-224 hasher. Inherits from SHA256 with a 224-bit digest.

SHA224.new(s = null)

Creates a new SHA-224 hasher, optionally hashing an initial string.

ParameterTypeDefaultDescription
sstring|nonenullAn optional initial input string (default null).
update(s)

Feeds additional data into the hasher.

ParameterTypeDefaultDescription
sstringThe data to hash.
digest()

Returns the raw digest as a list of bytes.

Returns list — A list of integers representing the digest bytes.

hexdigest()

Returns the hex-encoded digest string.

Returns string — A 56-character lowercase hexadecimal SHA-224 digest.

copy()

Returns a copy of this hasher with the same internal state.

Returns SHA224 — A new independent SHA224 instance.