Chinaunix首页 | 论坛 | 博客
  • 博客访问: 476667
  • 博文数量: 99
  • 博客积分: 3621
  • 博客等级: 中校
  • 技术积分: 1089
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-22 16:29
文章存档

2012年(21)

2011年(28)

2010年(50)

分类: Python/Ruby

2011-10-09 16:26:33

  1. 用sha1sum进行文件校验试验:
  2. [root@localhost bruce]# touch sha.txt
  3. [root@localhost bruce]# echo "this is the test file">sha.txt #创建文件,并写入初始值
  4. [root@localhost bruce]# sha1sum sha.txt >sha_sum.txt #生成校验文件
  5. [root@localhost bruce]# mkdir test
  6. [root@localhost bruce]# cp sha* test #这里没有做网络传输,测试直接CP了
  7. [root@localhost bruce]# ll test
  8. total 8
  9. -rw-r--r-- 1 root root 50 Oct 9 01:17 sha_sum.txt
  10. -rw-r--r-- 1 root root 22 Oct 9 01:17 sha.txt
  11. [root@localhost bruce]# cd test
  12. [root@localhost test]# sha1sum sha.txt >sha_sum_bak.txt #复制过来的文件,再做一次校验,生成新的校验文件
  13. [root@localhost test]# ll
  14. total 12
  15. -rw-r--r-- 1 root root 50 Oct 9 01:18 sha_sum_bak.txt
  16. -rw-r--r-- 1 root root 50 Oct 9 01:17 sha_sum.txt
  17. -rw-r--r-- 1 root root 22 Oct 9 01:17 sha.txt
  18. [root@localhost test]# diff sha_sum.txt sha_sum_bak.txt #比较这2个校验文件,是否是一致的,如果是,说明复制过程(或者网络传输中)没有发生文件错误
  19. [root@localhost test]# cat sha_sum.txt
  20. 4a0f171a35948dd42ec39bccbfa9c500da7a2725 sha.txt
  21. [root@localhost test]# cat sha_sum_bak.txt
  22. 4a0f171a35948dd42ec39bccbfa9c500da7a2725 sha.txt
  23. [root@localhost test]#

  24. #在测试传输后文件出错的情况。
  25. [root@localhost test]# echo "something worng">>sha.txt #在文件后面加入一些信息
  26. [root@localhost test]# sha1sum sha.txt >sha_sum_bak_2.txt #生成新的校验文件
  27. [root@localhost test]# diff sha_sum.txt sha_sum_bak_2.txt #比较原始文件和新的校验文件
  28. 1c1
  29. < 4a0f171a35948dd42ec39bccbfa9c500da7a2725 sha.txt
  30. ---
  31. > 2f39d1c95b3c9c1e9102bd288f1300cc0360745e sha.txt

  32. #很明显校验值不一样。
  33. #在传输的时候如果把源文件和校验文件一起发送,那么接收后,只需要进行CHECK操作
  34. [root@localhost test]# sha1sum -c sha_sum_bak_2.txt
  35. sha.txt: OK
  36. [root@localhost test]# sha1sum -c sha_sum_bak.txt #这里提示错误,是因为sha.txt文件内容已经改变,校验文件不是sha_sum_bak.txt了。
  37. sha.txt: FAILED
  38. sha1sum: WARNING: 1 of 1 computed checksum did NOT match

  39. #man sha1sum
  40.        -c, --check
  41.               read SHA1 sums from the FILEs and check them
阅读(4108) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

9094133352011-10-09 16:43:32

扩展:
[root@localhost test]# md5sum sha.txt sha1.txt
8f606db17f0b6569382d8c7a0bd1efac  sha.txt
8f606db17f0b6569382d8c7a0bd1efac  sha1.txt