What this is about
A hash is a short fingerprint of some data. You give a hash function a file, or a line of text, or anything else made of bytes, and it gives back a small, fixed-size number. Give it the same data and you get the same number. Change one byte, and the number changes completely.
That makes hashes a fast way to ask "are these two files the same?" You compare two short numbers, and you do not need to compare every byte.
Why it matters
Hashes are everywhere. A download page lists a checksum so you can check your copy. A backup tool uses hashes to find duplicates. A build tool uses them to see which files changed. Our own tools use them to tie a result to the exact model file it came from.
If you understand hashes, you can answer: did this file survive the copy? Did anyone change it? Do these two folders hold the same files?
How it works
A good hash has three habits.
- The same input always gives the same fingerprint. The program can be run on another day, on another machine.
- A tiny change flips the result. Add one space to a file, and the fingerprint looks unrelated to the old one.
- You cannot easily go backwards. From the fingerprint, you cannot rebuild the file.
Two different files can in theory share a fingerprint, but with a good function, it is extremely unlikely by accident.
There are two families to know.
- Cryptographic hashes, such as sha256, are designed to resist someone who tries to cheat. Use them when you check a download or sign something. Older ones, md5 and sha1, can be broken on purpose, so do not trust them against an attacker. They are still fine for catching accidental damage.
- Fast hashes, such as xxHash, are designed for speed only. Use them to find duplicates or for hash tables, when no one is trying to fool you.
Let's try it in Mojo
Our mojo-hashlib library gives you sha256, sha1 and md5, with the same results as Python's hashlib. Here is a program that hashes a file. We ran it on Mojo 1.2.0, with a small settings.json in the folder.
from hashlib import Sha256
def main() raises:
var f = open("settings.json", "r")
var h = Sha256()
h.update(f.read_bytes())
print(h.hexdigest())
It printed:
256a73811d8fc08c5907679c3d5dd56ce29b44f03fda7d1adda6c2be6cee40c0
We then ran the standard sha256sum settings.json command, and it printed the same 64 characters. That is a small oracle test: a tool we trust says the same thing as ours.
You can call update many times, so a huge file can be hashed in chunks. Reading the digest does not end the hasher.
For text, the same call works:
var h = Sha256()
h.update("hello world")
print(h.hexdigest())
Inside a def main() raises:, that prints b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9, which also matches Python's hashlib.sha256(b"hello world").hexdigest().
When speed matters: xxHash
Our mojo-xxhash library has four hashes: XXH32, XXH64, XXH3-64 and XXH3-128. One call looks like this:
from xxhash import xxh3_64
def main():
print(xxh3_64("hello world".as_bytes()))
It gave 15296390279056496779 when we ran it on Mojo 1.2.0. The README says its outputs are identical to the reference xxHash for every length from 0 to 300 and a range of larger sizes, with four seeds. We did not run Python's xxhash module ourselves for this article, so that comparison is the README's.
The README speed table, on 64 MiB with a Ryzen 7 7800X3D, compared it with the system library libxxhash 0.8.4 as Arch packages it, which is a generic x86-64 build:
| mojo-xxhash | libxxhash | |
|---|---|---|
| XXH32 | 10.7 GB/s | 8.3 GB/s |
| XXH64 | 21.3 GB/s | 12.1 GB/s |
| XXH3-64 | 51.6 GB/s | 22.8 GB/s |
Part of the gap is that Mojo compiled for the host processor, and the system library was built to run on any x86-64 chip. So the table shows that a tuned build is fast, not that Mojo wins in general.
Where we lose
Our own sha256 is slower than Python's on big inputs. In the mojo-hashlib README, 64 MiB through update took 145 to 150 ms against 27 to 28 ms for Python. Python calls OpenSSL, which uses special processor instructions. Our version is portable Mojo with no processor-specific code. The ratio is 5.3 times for sha256, 2.2 times for sha1 and 1.2 times for md5.
Small jobs go the other way. 200,000 messages of about 30 bytes, each hashed with a new hasher, once with each of the three hashes, took 153 to 172 ms in Mojo against 337 to 343 ms in Python. The library's shell-quoting half, quote, join and split, ran about 28 times faster than Python's shlex. So the answer depends on the shape of the job.
What the libraries do not cover
mojo-hashliboffers only sha256, sha1 and md5. There is no sha512, blake2 or hmac.- One hasher is not safe to share between threads.
mojo-xxhashhas no streaming XXH32 and no custom secret.
Both libraries live in mojo-libs under libs/hashlib and libs/xxhash. The older repositories are read-only.
Words you may not know
- Hash: a short fingerprint computed from data.
- Digest: the output of a hash, often shown as hex digits.
- Checksum: a number used to check that data was not damaged.
- Hex: a way to write numbers with the digits 0 to 9 and letters a to f.
- Seed: a starting number that changes a hash's output on purpose.
Try it
- Hash a small file with the Mojo program above and with
sha256sum. Compare. - Change one letter in the file and hash again. Look at how much of the fingerprint changes.
- Read the mojo-hashlib README and the mojo-xxhash README.
Read next
- Oracle tests: checking new code against code you trust shows how a known hash value becomes a test.

Komentarze
Brak komentarzy.
Zaloguj się aby skomentować.