Linux校验文件SHA1/SHA256/MD5哈希值方法

Kevin 教程 1,174 次浏览 4条评论

要知道一个文件是否被修改,可靠方法是校验文件Hash值。它由不同算法创建得出,以一串数字字母组成。相当于文件的电子指纹,一旦修改数值也随之变更。现在常用的算法有SHA1/SHA256/MD5,在Linux系统下有相应工具可以计算,无需另外安装就能使用,它们被默认包含在大多Linux系统内。

Linux计算文件Hash值

下面以计算文件SHA1演示,其它算法也是一样用法,只是命令名称有所区别。

示例1:计算一个文件的SHA1值。

[root@testhost ~]# sha1sum test.txt
d0b0d61ce9b03d9e07e511ca53b19a4dfde4dc73  test.txt

示例2:计算多个文件SHA1(可以用*通配符批量选择),并将结果写入指定文件。

[root@testhost ~]# sha1sum test.txt test2.txt test3.txt > hash.sha1
[root@testhost ~]# cat hash.sha1
d0b0d61ce9b03d9e07e511ca53b19a4dfde4dc73  test.txt
9b256eb031ec0551a19c0cf62ab6c05a78e118b1  test2.txt
9c4e85371847c38f326c6480cee470283cef62af  test3.txt

示例3:输入指定SHA1值校验与文件是否匹配。

[root@testhost ~]# echo "d7a7aa567c53f617d2d54ea81d14db2d171b7994  test4.txt" | sha1sum -c
test4.txt: OK

示例4:以SHA1结果文件批量校验所记载的文件。

[root@testhost ~]# sha1sum -c hash.sha1
test.txt: OK
test2.txt: OK
test3.txt: OK

补充

其它常用散列算法还有SHA224/SHA256/SHA512/MD5,Linux下使用只需在名称后加上sum就可。

[root@testhost ~]# sha224sum test.txt
[root@testhost ~]# sha256sum test.txt
[root@testhost ~]# sha512sum test.txt
[root@testhost ~]# md5sum test.txt

评论列表

  1. #1

    We are a gaggle of volunteers and opening a new scheme in our community. Your site provided us with helpful info to paintings on. You’ve performed an impressive activity and our whole community might be thankful to you.

    http://www.vreyrolinomit.com/

  2. #2

    so much wonderful info on here, : D.

    http://www.zortilonrel.com/

  3. #3

    Lovely site! I am loving it!! Will come back again. I am taking your feeds also

    http://www.easoar-soon.com

  4. #4

    I have been absent for a while, but now I remember why I used to love this website. Thank you, I¦ll try and check back more often. How frequently you update your web site?

    http://www.vreyrolinomit.com/

发表评论

电邮地址用于 Gravatar 头像显示,不会被公开可见。