Hashing and digital signatures
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
哈希保护完整性
- 我们用哈希来保护密码的保密性。哈希也能保护完整性 —— 证明数据未被改动。
- 哪怕只改输入中的一个字符,哈希也会完全不同。
import hashlib
print(hashlib.sha256(b"hello").hexdigest()[:16])
print(hashlib.sha256(b"hellp").hexdigest()[:16]) # totally different
校验下载
- 网站会公布文件的哈希值。下载之后,你对自己的副本做哈希再比对。
- 如果两个哈希相同,文件就是完好送达的。如果不同,它要么损坏了,要么被篡改了。
数字签名
- 数字签名既能证明是谁发送了某物,也能证明它没有被改动。
- 发送方对消息做哈希,再用自己的私钥加密这个哈希。
- 任何人都能用发送方的公钥来验证它 —— 只有真正的发送方才可能做出它。
轮到你了
- 比较原始消息和收到消息的哈希。
True表示消息是完好的。
涵盖:A-Level 17.1(数字认证)、6.2(完整性)。
常见错误
- 哈希是单向的——无法从中还原原文。
- 数字签名证明谁发送了消息,以及消息未被篡改。
探索
Hashing & signatures
A hash is one-way and avalanches — perfect for fingerprints.
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
Check whether a received message is unchanged. Hash both original and received with SHA-256 and print whether the two digests are equal (True 或 False).
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
This time an attacker edited the message in transit. Hash both versions and print TAMPERED if the digests differ, else OK — one changed character is enough to catch.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
A website publishes the SHA-256 of its download. Write download_ok(data, published), which hashes the data bytes and returns True only if the hex digest equals published. Ignore upper or lower case in published.
点击“运行”查看此处输出。