Crypto.sha512
func Crypto.sha512(bytes: Bytes): BytesCrypto.sha512 returns the 64-byte SHA-512 digest of bytes.
Details
- It hashes bytes, not text. Turn a string into bytes with
String.toBytes, and the digest into text withEncoding.toHexorEncoding.toBase64.
Basic examples
Hash the bytes of a string and show the digest in hex:
let digest = Crypto.sha512(String.toBytes("abc"))
println(Encoding.toHex(digest))ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49fThe digest itself is Bytes, 64 of them:
let digest = Crypto.sha512(String.toBytes("abc"))
digest.length() // 64Scope
Empty input has a digest too:
let digest = Crypto.sha512(String.toBytes(""))
println(digest.length())
println(Encoding.toHex(digest))64
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3eAny bytes work, not only text:
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:
let base64 = String.toBytes("abc") | Crypto.sha512() | Encoding.toBase64()
println(base64)
println(Crypto.sha512(bytes: String.toBytes("abc")).length())3a81oZNherrMQXNJriBBMRLm+k6JqX6iCp7u5ktV05ohkpkqJ0/BqDa6PCOj/uu9RU1EI2Q86A4qmslPpUyknw==
64Applications
Print the checksum of a file, as sha512sum does:
let path = "release.txt"
let data = File.readBytes(path) | Result.unwrap()
let sum = Crypto.sha512(data) | Encoding.toHex()
println("${sum} ${path}")7fe28a07afc6196ee6c9c88b496f2d1135f3af581cdc2ff30a4b207e9f5209fc3d8f482b2f5a6def9b4034b3d5732bedb10e102eceb90603ff4088895c877514 release.txtCheck downloaded bytes against a published hex digest. Decode the hex with Encoding.fromHex, and return early with try when it is malformed:
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())Ok{ value: true }
Ok{ value: false }
falseStore blobs under the hex digest of their content, so the same content is stored once:
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())true
2Properties and relations
The digest is 64 bytes and 128 hex digits for any input:
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:
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:
let x = String.toBytes("abc")
Crypto.sha256(x).length() // 32
Crypto.sha512(x).slice(0, 32) == Crypto.sha256(x) // falsePossible issues
It takes Bytes, not a string:
let digest = Crypto.sha512("abc") // error: cannot use string where Bytes is expectedA digest is not text. String.fromBytes gives null when its bytes are not valid UTF-8, as here; encode it with Encoding.toHex instead:
String.fromBytes(Crypto.sha512(String.toBytes("abc"))) // nullA 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:
let a = Crypto.sha512(String.toBytes("Ant v0.1.0"))
let b = Crypto.sha512(String.toBytes("Ant v0.1.0\n"))
a == b // falseString.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:
let composed = Crypto.sha512(String.toBytes("é"))
let decomposed = Crypto.sha512(String.toBytes("é"))
composed == decomposed // falseNormalize first with String.normalize when such strings must hash alike:
let a = Crypto.sha512(String.toBytes("é".normalize("NFC")))
let b = Crypto.sha512(String.toBytes("é".normalize("NFC")))
a == b // trueSee also
Crypto.sha256 · Crypto.sha1 · Crypto.md5 · Crypto.hmacSha512 · Crypto.equal · Encoding.toHex · Encoding.toBase64 · String.toBytes · File.readBytes
