Data integrity · 数据完整性
Keeping data correct
- Integrity means data stays accurate. Two techniques protect it: validation and verification.
- They sound similar but do different jobs — a classic exam distinction.
让数据保持正确
- 完整性意味着数据保持准确。有两种技术来保护它:验证(validation)和核对(verification)。
- 它们听起来相似,做的却是不同的工作 —— 这是一个经典的考试区分点。
Validation vs verification
- Validation checks data is reasonable as it is entered. Examples:
- Range check (age 0–120), length check (a phone number), format check (an email has an
@), presence check (not left blank).
- Range check (age 0–120), length check (a phone number), format check (an email has an
- Verification checks data was copied correctly. Examples:
- Double entry (type a password twice), or a visual check (read it back).
validation 与 verification 的区别
- validation(验证)在数据录入时检查它是否合理。例如:
- 范围检查(年龄 0–120)、长度检查(电话号码位数)、格式检查(邮箱含
@)、存在性检查(不能留空)。
- 范围检查(年龄 0–120)、长度检查(电话号码位数)、格式检查(邮箱含
- verification(核对)检查数据是否被正确复制。例如:
- 双重录入(把密码输两遍),或目视核对(读回来确认一遍)。
Checking data in transit
- When data travels, bits can flip. A checksum or check digit catches that.
- The sender calculates a small number from the data; the receiver recalculates it. If they differ, the data was corrupted.
检查传输中的数据
- 数据传输时,比特可能翻转。**校验和(checksum)或校验位(check digit)**能发现这一点。
- 发送方根据数据算出一个小数字;接收方再重新计算。如果两者不同,数据就是损坏了。
number = "1389"
print(sum(int(d) for d in number) % 10) # a one-digit checksum
Your turn
- Compute a simple check digit by summing the digits modulo 10. Real systems (like barcodes and ISBNs) use cleverer versions of this idea.
Covers: A-Level 6.2 (validation/verification), IGCSE 2.2 (error detection).
轮到你了
- 把各位数字相加再对 10 取模,算出一个简单的校验位。真实系统(如条形码和 ISBN)使用的是这个思路更巧妙的版本。
涵盖:A-Level 6.2(验证 / 核对)、IGCSE 2.2(错误检测)。
Common mistakes
- A checksum or check digit spots accidental changes to data.
- Validation checks data is sensible; verification checks it was entered correctly.
常见错误
- 校验和或校验位能发现数据的意外改动。
- 验证检查数据是否合理;核对检查是否输入正确。
Hashes detect tampering · 哈希能检测篡改
Re-hash the data and compare: any change flips the digest. · 重新计算哈希再比较:任何改动都会改变摘要。
A simple checksum helps catch typing errors. Add up the digits of number and take the result modulo 10. Print that single check digit. · 一个简单的**校验和(checksum)**能帮助发现输入错误。把 number 的各位数字相加,再对结果取模 10。打印出这个校验位。
Click Run to see the output here. · 点击“运行”查看此处输出。
Now be the receiver. The sender transmitted the digits with check digit 8, but one digit got corrupted on the way. Recompute the checksum of received and print ERROR if it does not equal sent_check, else OK. · 现在扮演接收方。发送方发出的数字带着校验位 8,但途中有一位被损坏了。重新计算 received 的校验和:与 sent_check 不相等就打印 ERROR,否则打印 OK。
Click Run to see the output here. · 点击“运行”查看此处输出。