Rill v0.13 Reference

Standard library · Cryptography

crypto/sha2

Imported as import "crypto/sha2" as sha2, its names are then sha2.…. Every signature below is the one the checker infers.

Functions

fn sha256(msg: Str) -> Str

The digest, as 32 raw bytes.

sha2.hex(sha2.sha256(""))   # => e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
#sha2.sha256("x")           # => 32

fn sha512(msg: Str) -> Str

The digest, as 64 raw bytes.

str_sub(sha2.hex(sha2.sha512("")), 0, 32)   # => cf83e1357eefb8bdf1542850d66d8007
#sha2.sha512("x")                          # => 64

fn hmac_sha256(key: Str, msg: Str) -> Str

sha2.hex(sha2.hmac_sha256("key", "The quick brown fox jumps over the lazy dog"))   # => f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8

fn hmac_sha512(key: Str, msg: Str) -> Str

str_sub(sha2.hex(sha2.hmac_sha512("key", "msg")), 0, 32)   # => 1e4b55b925ccc28ed90d9d18fc2393fc

fn hex(digest: Str) -> Str

A digest as lowercase hex.

sha2.hex("AZ")   # => 415a

fn same(a: Str, b: Str) -> Bool

Whether two tags are the same, taking the same time whether they differ in the first byte or the last — a comparison that stops early tells a caller how much of a guess was right.

sha2.same("abc", "abc")    # => true
sha2.same("abc", "abd")    # => false
sha2.same("abc", "abcd")   # => false

Tests

  • test_sha256_vectors
  • test_sha256_block_edges
  • test_sha512_vectors
  • test_hmac_rfc4231 — RFC 4231's cases 1, 2, 3 and 6 — a short key, a text key, a key of one byte repeated, and one longer than the block.
  • test_same_is_equality