⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08_58.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Digest::MD5 (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_57.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_59.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.58. Digest::MD5</h2><p>The <a name="INDEX-1371" />Digest::MD5 module allows simplercreation of MD5 hashes. MD5 takes as input a message of arbitrarylength and creates a 128-bit fingerprint, or digest, of the input.Like the Digest module, Digest::MD5 implements both a functional andobject-oriented interface, which offer the same benefits. Digest::MD5also outputs binary (16 bytes long), hexadecimal (32 characterslong), and base64 (22 characters long) data.</p><p>To rewrite the example in<a href="ch08_57.htm#perlnut2-CHP-8-SECT-57">Section 8.57, "Digest"</a>:</p><blockquote><pre class="code">#!/usr/local/bin/perl -w# Yes, functional interfaceuse Digest::MD5 qw(md5_hex);my $text = `Be the ball, Danny!';my $hexed = md5_hex($text); print "The sum of \$text is [$hexed].\n";</pre></blockquote><p>You can use the object-oriented interface like so:</p><blockquote><pre class="code">#!/usr/local/bin/perl -w use Digest::MD5; my $text = `Be the ball, Danny!';my $md5 = Digest::MD5-&gt;new();$md5-&gt;add($text); my $sum = $md5-&gt;hexdigest;print "The sum of \$text is [$sum].\n";</pre></blockquote><p>Digest::MD5 implements (and exports) the following functions.</p><a name="INDEX-1372" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>new()</pre><p>Constructor. Returns a new Digest::MD5 object. You can add data to<tt class="literal">$object</tt> and retrieve the digest itself. If you can<tt class="literal">new</tt> more than once, it will reset the existingdigest and create a new Digest::MD5 object.</p></div><a name="INDEX-1373" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>reset</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>reset()</pre><p>An alias for <tt class="literal">new</tt>.</p></div><a name="INDEX-1374" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>add</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>add(<em class="replaceable">data</em>, ...)</pre><p>Appends <em class="replaceable">data</em> to the message for which youcalculate the digest. <tt class="literal">add</tt> returns the Digestobject itself. For example:</p><blockquote><pre class="code">open(FILE, ...) or ...while (&lt;FILE&gt;) {    $md5-&gt;add($_);} close(FILE);</pre></blockquote></div><a name="INDEX-1375" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>addfile</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>addfile(io_handle)</pre><p>Reads <em class="replaceable">io_handle</em> until end-of-file andappends the content to the message for which you calculate thedigest. <tt class="literal">addfile</tt> returns the Digest object itself.For example:</p><blockquote><pre class="code">open(FILE, $file) or ...print Digest::MD5-&gt;new-&gt;addfile(*FILE)-&gt;hexdigest, " $file\n";close(FILE);</pre></blockquote></div><a name="INDEX-1376" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>b64digest</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>b64digest()</pre><p>Same as <tt class="literal">digest</tt>, but returns the digest inbase64-encoded form.</p></div><a name="INDEX-1377" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>digest</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>digest()</pre><p>Returns the binary digest for the message. Note that<tt class="literal">digest</tt> is a destructive operation, in that theDigest object is reset so it can be used to create another value.</p></div><a name="INDEX-1378" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>hexdigest</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>hexdigest()</pre><p>Same as <tt class="literal">digest</tt>, but returns digest in hexadecimalform.</p></div><p>The following functions are implemented by the functional interface.</p><a name="INDEX-1379" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>md5</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>md5(<em class="replaceable">data</em>, ...) </pre><p>Calculates the MD5 digest of <em class="replaceable">data</em> andreturns it in binary form.</p></div><a name="INDEX-1380" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>md5_base64</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>md5_base64(<em class="replaceable">data</em>,...)</pre><p>Same as <tt class="literal">md5</tt>, but returns the digest as abase64-encoded string.</p></div><a name="INDEX-1381" /><a name="INDEX-1382" /><a name="INDEX-1383" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>md5_hex</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>md5_hex(<em class="replaceable">data</em>,...)</pre><p>Same as <tt class="literal">md5</tt>, but returns the digest in hexadecimalform.<a name="INDEX-1382" /><a name="INDEX-1383" /> </p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_57.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_59.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.57. Digest</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.59. DirHandle</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -