md5sum/sha1sum:文件内容比较
- 使用MD5算法(散列函数)根据文件内容生成16字节hash值,比较hash值是否相
同,就可断定两文件内容是否完全相同,(md5sum命令) - 使用SHA-1算法的命令名为sha1sum (20字节hash值)
- 其他散列函数也可以用来完成这一任务: sha512sum
md5算法常用于数据完整性(Data Integrity)验证和判断位于网络不同机器上的两个文件的内容是否相同。例如网上给出的安装程序经常会给出对应的md5值,我们可以对下载的安装程序计算md5值,如果我们计算的md5值与官网给出的md5值相同,则表示该安装程序没有被第三方篡改过,是安全的。
md5sum
测试文件
1 | [root@localhost cmp]# cat -n a.txt |
可看到a.txt与c.txt的内容是相同的。
对着三个文件执行md5sum命令:
[root@localhost cmp]# md5sum a.txt abd3291ec48e1700a70d39ee5db7fb7d a.txt [root@localhost cmp]# md5sum b.txt bf56342c3e159e45ea1e434122410291 b.txt [root@localhost cmp]# md5sum c.txt abd3291ec48e1700a70d39ee5db7fb7d c.txt [root@localhost cmp]#
可以看到a.txt和c.txt的计算结果都是:abd3291ec48e1700a70d39ee5db7fb7d所以,这两个文件是相同的。
md5sum -c md5文件:读取MD5SUM的文件并验证所有文件是否具有匹配的校验和
测试文件:1
2
3
4
5
6
7
8
9
10
11[root@localhost cmp]# cat -n a.txt
1 this is line a
2 this is line b
3 helloworld!
4 this is line d
[root@localhost cmp]# cat -n b.txt
1 this is line a
2 this is line b
3 this is line c
4 this is line d
[root@localhost cmp]#
计算这两个文件的md5值,保存到文件中:1
2
3
4[root@localhost cmp]# md5sum a.txt b.txt
abd3291ec48e1700a70d39ee5db7fb7d a.txt
bf56342c3e159e45ea1e434122410291 b.txt
[root@localhost cmp]# md5sum a.txt b.txt >a_b.md5
修改a.txt文件,在末尾加入一行:1
2
3
4
5
6
7
8[root@localhost cmp]# vim a.txt
[root@localhost cmp]# cat a.txt
this is line a
this is line b
helloworld!
this is line d
this is some new word
[root@localhost cmp]#
执行md5校验:1
2
3
4
5[root@localhost cmp]# md5sum -c a_b.md5
a.txt: 失败
b.txt: 确定
md5sum: 警告:1 个校验和不匹配
[root@localhost cmp]#
可以看到我们修改a.txt后,该文件md5校验失败,这表示a.txt文件被篡改了。
md5sum命令手册
tldr md5sum
1 | [root@localhost cmp]# tldr md5sum |
sha1sum
sha1sum file:计算文件的SHA1校验和
测试文件:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@localhost cmp]# cat -n a.txt
1 this is line a
2 this is line b
3 helloworld!
4 this is line d
[root@localhost cmp]# cat -n b.txt
1 this is line a
2 this is line b
3 this is line c
4 this is line d
[root@localhost cmp]# cat -n c.txt
1 this is line a
2 this is line b
3 helloworld!
4 this is line d
[root@localhost cmp]#
计算sha1校验和:
[root@localhost cmp]# sha1sum a.txt 01c3d325eeca7f806192dddbfa9ce791cb5c6fc7 a.txt [root@localhost cmp]# sha1sum b.txt 1cb5cee11036b493799b8b1b0d6ea9704e84c282 b.txt [root@localhost cmp]# sha1sum c.txt 01c3d325eeca7f806192dddbfa9ce791cb5c6fc7 c.txt [root@localhost cmp]#
可以看到a.txt和c.txt的校验和是一样的,所以这两个文件的内容相同。
sha1sum 多个文件:计算多个文件的SHA1校验和
1 | [root@localhost cmp]# sha1sum a.txt b.txt c.txt |
sha1sum —check file.sha1:检查文件是否被篡改
测试文件:1
2
3
4
5
6
7
8
9
10
11
12
13
14[root@localhost cmp]# cat -n a.txt
1 this is line a
2 this is line b
3 helloworld!
4 this is line d
[root@localhost cmp]# cat -n b.txt
1 this is line a
2 this is line b
3 this is line c
4 this is line d
[root@localhost cmp]# sha1sum a.txt b.txt
01c3d325eeca7f806192dddbfa9ce791cb5c6fc7 a.txt
1cb5cee11036b493799b8b1b0d6ea9704e84c282 b.txt
[root@localhost cmp]#
生成SHA1校验和文件:1
[root@localhost cmp]# sha1sum a.txt b.txt > a_b.sha1
修改文件:1
2
3
4
5
6
7
8
9
10
11
12[root@localhost cmp]# cat -n a.txt
1 this is line a
2 this is line b
3 helloworld!
4 this is line d
5 this is a new line
[root@localhost cmp]# cat -n b.txt
1 this is line a
2 this is line b
3 this is line c
4 this is line d
[root@localhost cmp]#
检查所有文件的SHA1校验和:1
2
3
4
5[root@localhost cmp]# sha1sum --check a_b.sha1
a.txt: 失败
b.txt: 确定
sha1sum: 警告:1 个校验和不匹配
[root@localhost cmp]#
可以看到a.txt校验失败了,这说明a.txt被修改过了。
sha1sum -c filename.sha1
上面的—check参数可以简写为-c:1
2
3
4
5[root@localhost cmp]# sha1sum -c a_b.sha1
a.txt: 失败
b.txt: 确定
sha1sum: 警告:1 个校验和不匹配
[root@localhost cmp]#
sha1sum —check —quiet file.sha1:只显示SHA1校验和失败的文件
1 | Try 'sha1sum --help' for more information. |
sha1sum -c —quiet filename.sha1
--check
可以简写为-c
,不过需要注意的是--quiet
参数不能省略:1
2
3
4
5
6
7
8
9
10[root@localhost cmp]# sha1sum --check --quiet a_b.sha1
a.txt: 失败
sha1sum: 警告:1 个校验和不匹配
[root@localhost cmp]# sha1sum -c --quiet a_b.sha1
a.txt: 失败
sha1sum: 警告:1 个校验和不匹配
[root@localhost cmp]# sha1sum -c -q a_b.sha1
sha1sum:无效选项 -- q
Try 'sha1sum --help' for more information.
[root@localhost cmp]#
sha1sum命令手册
tldr sha1sum
1 | [root@localhost cmp]# tldr sha1sum |
sha1sum —help
1 | [root@localhost cmp]# sha1sum --help |
man sha1sum
1 | SHA1SUM(1) User Commands SHA1SUM(1) |
hash算法失误率
MD5: $2^{-128} =3.4×10^{-38}$
SHA-1: $2^{-160} =4.7×10^{-50}$