kex docs Standard Library 0.4.0-alpha kex.run ↗

Digest

module Digest

Cryptographic content digests.

Digests are returned as lowercase hex strings, so a Kex program never has to handle backend-specific binary values: they compare with ==, print directly, and go into a map key unchanged.

Digest.sha256("hello")
# => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

SHA-256 is a content fingerprint: use it to tell whether two things are the same, to key a cache, or to check that a download arrived intact. It is not a password hash: a purpose-built password KDF is what that needs.

function sha256

Returns the SHA-256 digest of content, as a 64-character lowercase hex string.

sha256(content) : String -> String
sha256(content) : Binary -> Binary
Parameters
content String
the text to hash

Returns: String — the hex digest

Examples
Digest.sha256("hello")
# => "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
Digest.sha256("")
# => "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"

A cache key derived from an input

let key = Digest.sha256(source).take(12)

Detecting that content changed

Digest.sha256(newText) != Digest.sha256(oldText)

function fileSha256

Returns the SHA-256 digest of the file at path, or None when the file cannot be read.

Reads the file for you, so a large file does not have to be pulled into a String first.

fileSha256(path) : String -> String?
Parameters
path String
the file to hash

Returns: String? — the hex digest, or None

Examples
Digest.fileSha256("archive.tar.gz")   # => Just("a3f1...")
Digest.fileSha256("nowhere")          # => None

Verifying a download against a published checksum

Digest.fileSha256(path).map { |actual| actual == expected }.or(false)