Skip to content

Crypto.sha512 ​

ant
func Crypto.sha512(bytes: Bytes): Bytes

Crypto.sha512 returns the 64-byte SHA-512 digest of bytes.

Details ​

Basic examples ​

Hash the bytes of a string and show the digest in hex:

ant
let digest = Crypto.sha512(String.toBytes("abc"))
println(Encoding.toHex(digest))
text
ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f

The digest itself is Bytes, 64 of them:

ant
let digest = Crypto.sha512(String.toBytes("abc"))
digest.length() // 64

Scope ​

Empty input has a digest too:

ant
let digest = Crypto.sha512(String.toBytes(""))
println(digest.length())
println(Encoding.toHex(digest))
text
64
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e

Any bytes work, not only text:

ant
Crypto.sha512(Bytes.fromArray([0, 1, 2])).slice(0, 4) // Bytes(80 81 da 5f)

Use it in a pipe, where the bytes are the first argument, or with a named argument:

ant
let base64 = String.toBytes("abc") | Crypto.sha512() | Encoding.toBase64()
println(base64)
println(Crypto.sha512(bytes: String.toBytes("abc")).length())
text
3a81oZNherrMQXNJriBBMRLm+k6JqX6iCp7u5ktV05ohkpkqJ0/BqDa6PCOj/uu9RU1EI2Q86A4qmslPpUyknw==
64

Applications ​

Print the checksum of a file, as sha512sum does:

ant
let path = "release.txt"
let data = File.readBytes(path) | Result.unwrap()
let sum = Crypto.sha512(data) | Encoding.toHex()
println("${sum}  ${path}")
text
7fe28a07afc6196ee6c9c88b496f2d1135f3af581cdc2ff30a4b207e9f5209fc3d8f482b2f5a6def9b4034b3d5732bedb10e102eceb90603ff4088895c877514  release.txt

Check downloaded bytes against a published hex digest. Decode the hex with Encoding.fromHex, and return early with try when it is malformed:

ant
func verify(data: Bytes, expected: string): Result[bool, Error] {
  let want = try (Encoding.fromHex(expected) | Result.wrapErr("expected digest"))
  Result.ok(Crypto.sha512(data) == want)
}

let data = String.toBytes("abc")
let published = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
println(verify(data, published))
println(verify(String.toBytes("abd"), published))
println(verify(data, "not hex").isOk())
text
Ok{ value: true }
Ok{ value: false }
false

Store blobs under the hex digest of their content, so the same content is stored once:

ant
let store: Map[string, Bytes] = Map{}

func put(blob: Bytes): string {
  let key = Crypto.sha512(blob) | Encoding.toHex()
  store.set(key, blob)
  key
}

let first = put(String.toBytes("hello"))
let second = put(String.toBytes("hello"))
put(String.toBytes("world"))
println(first == second)
println(store.size())
text
true
2

Properties and relations ​

The digest is 64 bytes and 128 hex digits for any input:

ant
for s in ["", "a", "ant".repeat(1000), "ant".repeat(100000)] {
  let digest = Crypto.sha512(String.toBytes(s))
  assert(digest.length() == 64)
  assert(Encoding.toHex(digest).length() == 128)
}

The same bytes always give the same digest, and a one-byte change gives a different one:

ant
let x = String.toBytes("transfer 100")
assert(Crypto.sha512(x) == Crypto.sha512(String.toBytes("transfer 100")))
assert(Crypto.sha512(x) != Crypto.sha512(String.toBytes("transfer 900")))

It is twice as long as Crypto.sha256, and a different function, not a longer version of it:

ant
let x = String.toBytes("abc")
Crypto.sha256(x).length()                         // 32
Crypto.sha512(x).slice(0, 32) == Crypto.sha256(x) // false

Possible issues ​

It takes Bytes, not a string:

ant
let digest = Crypto.sha512("abc") // error: cannot use string where Bytes is expected

A digest is not text. String.fromBytes gives null when its bytes are not valid UTF-8, as here; encode it with Encoding.toHex instead:

ant
String.fromBytes(Crypto.sha512(String.toBytes("abc"))) // null

A digest alone does not prove who sent a message: anyone can compute it. To authenticate a message with a shared key, use Crypto.hmacSha512, and compare the received tag with Crypto.equal, whose time does not depend on the contents.

A trailing line break is part of the bytes. A file that ends in \n and the same text without it have different digests:

ant
let a = Crypto.sha512(String.toBytes("Ant v0.1.0"))
let b = Crypto.sha512(String.toBytes("Ant v0.1.0\n"))
a == b // false

String.toBytes encodes text as UTF-8, so two strings that look the same but are spelled with different code points have different digests. "é" is one code point; "é" is e and a combining accent:

ant
let composed = Crypto.sha512(String.toBytes("é"))
let decomposed = Crypto.sha512(String.toBytes("é"))
composed == decomposed // false

Normalize first with String.normalize when such strings must hash alike:

ant
let a = Crypto.sha512(String.toBytes("é".normalize("NFC")))
let b = Crypto.sha512(String.toBytes("é".normalize("NFC")))
a == b // true

See also ​

Crypto.sha256 · Crypto.sha1 · Crypto.md5 · Crypto.hmacSha512 · Crypto.equal · Encoding.toHex · Encoding.toBase64 · String.toBytes · File.readBytes