日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

md5sum算法 —— linux或Unix上,md5sum是用来计算和校验文件报文摘要的工具程序

發布時間:2025/10/17 linux 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 md5sum算法 —— linux或Unix上,md5sum是用来计算和校验文件报文摘要的工具程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

md5sum幫助命令:

[root@jie openssl]# md5sum --help

Usage: md5sum [OPTION]... [FILE]... Print or check MD5 (128-bit) checksums. With no FILE, or when FILE is -, read standard input.-b, --binary read in binary mode-c, --check read MD5 sums from the FILEs and check them--tag create a BSD-style checksum-t, --text read in text mode (default)Note: There is no difference between binary and text mode option on GNU system.The following four options are useful only when verifying checksums:--quiet don't print OK for each successfully verified file--status don't output anything, status code shows success--strict exit non-zero for improperly formatted checksum lines-w, --warn warn about improperly formatted checksum lines--help display this help and exit--version output version information and exitThe sums are computed as described in RFC 1321. When checking, the input should be a former output of this program. The default mode is to print a line with checksum, a character indicating input mode ('*' for binary, space for text), and name for each FILE.GNU coreutils online help: <http://www.gnu.org/software/coreutils/> For complete documentation, run: info coreutils 'md5sum invocation'

md5sum 加密字符串的方法:

利用md5sum加密字符串的方法
  ?# md5sum //然后回車
  ?123456 //輸入123456.然后按兩次ctrl+d.
  顯示:
  ?123456e10adc3949ba59abbe56e057f20f883e 紅色代表加密后的值
  還可以用管道命令:
  ?#echo -n '123123' | md5sum

1、手動按照順序輸入的過程如下圖所示:

第一步:在終端輸入md5sum后回車:
[root@jie md5sum]# md5sum?
第二步:輸入要加密的字符串admin:
admin
此時的輸入界面顯示如下:

第三步:輸入兩次ctrl+d結束此次加密:

ctrl+d有兩個含義:

一是向程序發送文件輸入結束符EOF。

二是向程序發送exit退出指令。程序收到信號后具體動作是結束輸入、然后等待,還是直接退出,那就要看該程序捕獲信號后是如何操作的了。
md5sum屬于第一個含義。兩次strl+d了,第一次讀取EOF指令,再次捕獲就會當成exit指令。而shell一類的程序,會直接把ctrl+d解析為退出指令。

2、使用管道命令openssl md5加密的過程如下圖所示:

第一步:在終端輸入管道命令:
[root@jie openssl]# echo -n 'admin' | md5sum?
第二步:輸入回車結束此次加密過程
結果顯示如下:

注意為何要加-n這個參數?
-n就表示不輸入回車符,這樣才能得到正確的結果。
如果你不加-n,那么結果如下:

獲取密文的值的方法(以上方式中得到的結果有一個 - 字符):

1》加密后的密文就是第一個空格之前的字符串,取值的時候可以用scanf函數獲取到值。

2》[root@jie md5sum]# echo -n 'admin' | md5sum | cut -d ' ' -f1

命令解釋:
md5sum: 顯示或檢查 MD5(128-bit) 校驗和,若沒有文件選項,或者文件處為"-",則從標準輸入讀取。
echo -n : 不打印換行符。
cut:? cut用來從標準輸入或文本文件中剪切列或域。剪切文本可以將之粘貼到一個文本文件。?
??????? -d 指定與空格和tab鍵不同的域分隔符。-f1 表示第一個域。參考這里。

3、或者寫成md5加密腳本,叫md5.sh,具體過程如下所示:

第一步:查找自己的bash文件的位置
[root@jie md5sum]# whereis bash

第二步:將以下內容復制到腳本里面:
#!/usr/bin/bash
echo -n $1 | md5sum | awk '{print $1}'

第三步:執行腳本

4、或者寫到txt(文本文件)中,叫md5sum.txt,加密的過程如下圖所示:

第一步:創建文件:
[root@jie md5sum]# touch md5sum.txt
第二步:寫入要加密的字符串:
[root@jie md5sum]# echo -n admin > md5sum.txt?
第三步:查看文件內容:
[root@jie md5sum]# cat md5sum.txt?
第四步:md5sum加密:

?

md5sum 加密文件方法:

目錄下的文件如下所示:

1、生成一個文件的md5值,加密的過程如下圖所示:
[root@jie file]# md5sum aaa.txt > aaa.md5? ? //生成校驗文件
[root@jie file]# ls? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//查看校驗文件是否生成
aaa.md5 ?aaa_pkg.tgz ?aaa.txt ?bbb.log??
[root@jie file]# cat aaa.md5? ? ? ? ? ? ? ? ? ? ? ? ? ?//查看生成的校驗文件的內容
5af7100ca078a49eaa5bec9c81394719 ?aaa.txt

2、MD5sum支持多個文件輸入或者通配符:
[root@jie file]# ls? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查看本目錄下的所有的文件
aaa.md5 ?aaa_pkg.tgz ?aaa.txt ?bbb.log? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //以aaa開頭的有三個
[root@jie file]# md5sum aaa.txt bbb.log aaa_pkg.tgz > all.md5? //多個文件一起生成md5校驗文件
[root@jie file]# cat all.md5? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查看生成的校驗文件的內容
5af7100ca078a49eaa5bec9c81394719 ?aaa.txt
a2e8ad541e1240d4205be6c54f871837 ?bbb.log
9a0ad4e3efae9a653d9d7540addeb03e ?aaa_pkg.tgz
[root@jie file]#?
[root@jie file]# md5sum aaa* > aaa_all.md5? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//md5sum支持通配符
[root@jie file]# cat aaa_all.md5? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //查看生成的校驗文件的內容
99782db4a6b88d153a8282715e169239 ?aaa.md5
9a0ad4e3efae9a653d9d7540addeb03e ?aaa_pkg.tgz
5af7100ca078a49eaa5bec9c81394719 ?aaa.txt

3、對文件內容校驗:
1》由前面的操作可知,aaa.md5文件是1中單個文件,生成的校驗文件。
對aaa.md5文件的校驗如下:

2》all.md5文件是2中,所有文件生成的校驗文件。
對all.md5文件的校驗如下:

3》修改aaa.txt 文件中的內容,再次校驗aaa.md5文件。

4、對文件路徑的校驗($PWD即當前路徑/home/hanlzh/test/):
[root@jie file]# pwd
/home/jieer/md5/md5sum/file
[root@jie file]# md5sum $PWD/aaa.txt > aaa.md5?
[root@jie file]# cat aaa.md5?
5ef9e9268a7550d08f2a2766884450c4 ?/home/jieer/md5/md5sum/file/aaa.txt
[root@jie file]# md5sum -c aaa.md5?
/home/jieer/md5/md5sum/file/aaa.txt: OK
[root@jie file]# cd ..
[root@jie md5sum]# md5sum -c ./file/aaa.md5?
/home/jieer/md5/md5sum/file/aaa.txt: OK

5、文件缺失或者不存在的情況
由4知,文件aaa.md5是aaa.txt文件路徑的校驗,
現在我們把aaa.txt文件刪除,然后再校驗aaa.md5文件。
[root@jie file]# ll | grep aaa.txt
-rw-r--r--. 1 root root ?17 Jul 26 20:59 aaa.txt
[root@jie file]# cat aaa.md5?
5ef9e9268a7550d08f2a2766884450c4 ?/home/jieer/md5/md5sum/file/aaa.txt
[root@jie file]# rm -rf aaa.txt?
[root@jie file]# ll | grep aaa.txt
[root@jie file]# cat aaa.md5?
5ef9e9268a7550d08f2a2766884450c4 ?/home/jieer/md5/md5sum/file/aaa.txt
[root@jie file]# md5sum -c aaa.md5?
md5sum: /home/jieer/md5/md5sum/file/aaa.txt: No such file or directory
/home/jieer/md5/md5sum/file/aaa.txt: FAILED open or read
md5sum: WARNING: 1 listed file could not be read

?

總結:

特殊說明:
1)md5sum是校驗文件內容,與文件名是否相同無關;

2)md5sum是逐位校驗,所以文件越大,校驗時間越長。

?

md5校驗,可能極小概率出現不同的文件生成相同的校驗和,比md5更安全的校驗算法還有SHA*系列,如sha1sum/sha224sum/sha256sum/sha384sum/sha512sum等等,基本用法與md5sum命令類似,詳情可通過man sha1sum查詢。

以下僅簡單列舉一例,不再贅述。

[root@HLZ test]#?sha512sum aaa.txt > aaa.sha512

[root@HLZ test]#?cat aaa.sha512

8edb9790359ef641112fa85eea5c8f09b7330564a58cddf39aef66006f32454a8879b1e63d9f667ecf86264d4d5b6a602a4c94c79d962ec755345a3837217f89?aaa.txt

[root@HLZ test]#

[root@HLZ test]#?sha512sum -c ./aaa.sha512

aaa.txt:OK

?

參考鏈接:https://blog.csdn.net/taiyang1987912/article/details/42041329

? ? ? ? ? ? ? ? ??https://blog.csdn.net/hanlizhong85/article/details/77844635#t2

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的md5sum算法 —— linux或Unix上,md5sum是用来计算和校验文件报文摘要的工具程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。