uTools-Manuals/docs/linux/md5sum.html
2019-04-21 11:50:48 +08:00

44 lines
3.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="md5sum">md5sum</h1>
<p>计算和校验文件报文摘要的工具程序</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>md5sum命令</strong> 采用MD5报文摘要算法128位计算和检查文件的校验和。一般来说安装了Linux后就会有md5sum这个工具直接在命令行终端直接运行。</p>
<p>MD5算法常常被用来验证网络文件传输的完整性防止文件被人篡改。MD5 全称是报文摘要算法Message-Digest Algorithm 5此算法对任意长度的信息逐位进行计算产生一个二进制长度为128位十六进制长度就是32位的“指纹”或称“报文摘要”不同的文件产生相同的报文摘要的可能性是非常非常之小的。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">md5sum(选项)(参数)</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">-b二进制模式读取文件
-t或--text把输入的文件作为文本文件看待
-c从指定文件中读取MD5校验和并进行校验
--status验证成功时不输出任何信息
-w当校验不正确时给出警告信息。</code></pre>
<h3 id="参数">参数</h3>
<p>文件:指定保存着文件名和校验和的文本文件。</p>
<h3 id="实例">实例</h3>
<p><strong>使用 md5sum 生成密码</strong></p>
<p>另一种获取可用作密码的随机字符串的方法是计算 MD5 校验值!校验值看起来确实像是随机字符串组合在一起,我们可以用作密码。确保你的计算源是个变量,这样的话每次运行命令时生成的校验值都不一样。比如 date date 命令 总会生成不同的输出。</p>
<pre><code class="language-bash">[root@localhost ~]# date | md5sum
6a43f2c246cdc3e6a3592652f831d186 -</code></pre>
<p><strong>生成一个文件insert.sql的md5值</strong></p>
<pre><code class="language-bash">[root@localhost ~]# md5sum insert.sql
bcda6cb5c704664f989703ac5a88f112 insert.sql</code></pre>
<p><strong>检查文件testfile是否被修改过</strong></p>
<p>首先生成md5文件</p>
<pre><code class="language-bash">md5sum testfile &gt; testfile.md5</code></pre>
<p>检查:</p>
<pre><code class="language-bash">md5sum testfile -c testfile.md5</code></pre>
<p>如果文件没有变化,输出应该如下:</p>
<pre><code class="language-bash">forsort: OK</code></pre>
<p>此时md5sum命令返回0。</p>
<p>如果文件发生了变化,输出应该如下:</p>
<pre><code class="language-bash">forsort: FAILED
md5sum: WARNING: 1 of 1 computed checksum did NOT match</code></pre>
<p>此时md5sum命令返回非0。</p>
<p>这里,检查用的文件名随意。如果不想有任何输出,则<code>md5sum testfile --status -c testfile.md5</code>,这时候通过返回值来检测结果。</p>
<p>检测的时候如果检测文件非法则输出信息的选项:</p>
<pre><code class="language-bash">md5sum -w -c testfile.md5</code></pre>
<p>输出之后,文件异常输出类似如下:</p>
<pre><code class="language-bash">md5sum: testfile.md5: 1: improperly formatted MD5 checksum line
md5sum: testfile.md5: no properly formatted MD5 checksum lines found</code></pre>
<p>这里testfile.md5只有一行信息但是我认为地给它多加了一个字符导致非法。如果md5文件正常那么<code>-w</code>有没有都一样。</p>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->