Digest
Standard library namespace · Always available, with no import
A digest is a short fingerprint of some data. The same data always gives the same digest, and the
smallest change to it gives a completely different one, so comparing digests is a quick way to check
that a file or message has not changed. Digest makes two kinds: SHA-256, and HMAC-SHA256, which also
uses a secret key.
const fingerprint = Digest.sha256("abc".to_bytes())print(fingerprint.count)print(fingerprint.to_hex())32ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015adA digest is Bytes, 32 of them. To show or compare one the usual way,
as hexadecimal text, use to_hex().
These digests never change between versions or computers, so they are safe to save and to compare later. That is different from a value’s own hash, used by dictionaries and sets, which is an internal detail and must not be saved.
Neither function is a way to store passwords. Storing passwords needs a scheme made for the job, which is not part of this library.
At a glance
Section titled “At a glance”| Digests | |
|---|---|
Digest.sha256(bytes) |
The SHA-256 digest of some bytes |
Digest.hmac_sha256(bytes, key) |
The HMAC-SHA256 digest of some bytes with a key |
Digests
Section titled “Digests”Digest.sha256(bytes: Bytes): Bytes
The 32-byte SHA-256 digest of bytes. To take the digest of text, turn it into bytes first with
to_bytes().
print(Digest.sha256("".to_bytes()).to_hex())e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855To check a file, read its bytes with File.read_binary and take
their digest.
Digest.hmac_sha256(bytes: Bytes, key: Bytes): Bytes
The 32-byte HMAC-SHA256 digest of bytes, made with a secret key. Only someone who knows the key can
make the digest for a given message, so it shows both that the message is unchanged and that it came
from someone with the key. Web services often use it to sign what they send.
const message = "what do ya want for nothing?".to_bytes()const key = "Jefe".to_bytes()print(Digest.hmac_sha256(message, key).to_hex())5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843