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 exit
The 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