📄 module-sha.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>15.2 sha -- SHA message digest algorithm</title>
<META NAME="description" CONTENT="15.2 sha -- SHA message digest algorithm">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-mpz.html" tppabs="http://www.python.org/doc/current/lib/module-mpz.html">
<LINK REL="previous" href="module-md5.html" tppabs="http://www.python.org/doc/current/lib/module-md5.html">
<LINK REL="up" href="crypto.html" tppabs="http://www.python.org/doc/current/lib/crypto.html">
<LINK REL="next" href="module-mpz.html" tppabs="http://www.python.org/doc/current/lib/module-mpz.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-md5.html" tppabs="http://www.python.org/doc/current/lib/module-md5.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="crypto.html" tppabs="http://www.python.org/doc/current/lib/crypto.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-mpz.html" tppabs="http://www.python.org/doc/current/lib/module-mpz.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-md5.html" tppabs="http://www.python.org/doc/current/lib/module-md5.html">15.1 md5 </A>
<b class="navlabel">Up:</b> <a class="sectref" href="crypto.html" tppabs="http://www.python.org/doc/current/lib/crypto.html">15. Cryptographic Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-mpz.html" tppabs="http://www.python.org/doc/current/lib/module-mpz.html">15.3 mpz </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0017200000000000000000">
15.2 <tt class="module">sha</tt> --
SHA message digest algorithm</A>
</H1>
<P>
<P>
This module implements the interface to NIST's secure hash
algorithm, known as SHA. It is used in
the same way as the <tt class='module'><a href="module-md5.html" tppabs="http://www.python.org/doc/current/lib/module-md5.html">md5</a></tt> module: use <tt class="function">new()</tt>
to create an sha object, then feed this object with arbitrary strings
using the <tt class="method">update()</tt> method, and at any point you can ask it
for the <i class="dfn">digest</i> of the concatenation of the strings fed to it
so far. SHA digests are 160 bits instead of
MD5's 128 bits.
<P>
<dl><dt><b><a name='l2h-2952'><tt class='function'>new</tt></a></b> (<big>[</big><var>string</var><big>]</big>)
<dd>
Return a new sha object. If <var>string</var> is present, the method
call <code>update(<var>string</var>)</code> is made.
</dl>
<P>
The following values are provided as constants in the module and as
attributes of the sha objects returned by <tt class="function">new()</tt>:
<P>
<dl><dt><b><a name='l2h-2953'><tt>blocksize</tt></a></b>
<dd>
Size of the blocks fed into the hash function; this is always
<code>1</code>. This size is used to allow an arbitrary string to be
hashed.
</dl>
<P>
<dl><dt><b><a name='l2h-2954'><tt>digestsize</tt></a></b>
<dd>
The size of the resulting digest in bytes. This is always
<code>20</code>.
</dl>
<P>
An sha object has the same methods as md5 objects:
<P>
<dl><dt><b><a name='l2h-2955'><tt class='method'>update</tt></a></b> (<var>arg</var>)
<dd>
Update the sha object with the string <var>arg</var>. Repeated calls are
equivalent to a single call with the concatenation of all the
arguments, i.e. <code>m.update(a); m.update(b)</code> is equivalent to
<code>m.update(a+b)</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-2956'><tt class='method'>digest</tt></a></b> ()
<dd>
Return the digest of the strings passed to the <tt class="method">update()</tt>
method so far. This is a 20-byte string which may contain
non-ASCII characters, including null bytes.
</dl>
<P>
<dl><dt><b><a name='l2h-2957'><tt class='method'>hexdigest</tt></a></b> ()
<dd>
Like <tt class="method">digest()</tt> except the digest is returned as a string of
length 40, containing only hexadecimal digits. This may
be used to exchange the value safely in email or other non-binary
environments.
</dl>
<P>
<dl><dt><b><a name='l2h-2958'><tt class='method'>copy</tt></a></b> ()
<dd>
Return a copy (``clone'') of the sha object. This can be used to
efficiently compute the digests of strings that share a common initial
substring.
</dl>
<P>
<div class='seealso'>
<p class='heading'><b>See Also:</b></p>
<dl compact class="seetitle">
<dt><em class="citetitle"><a href="javascript:if(confirm('http://csrc.nist.gov/fips/fip180-1.txt \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://csrc.nist.gov/fips/fip180-1.txt'" tppabs="http://csrc.nist.gov/fips/fip180-1.txt"
>Secure Hash Standard <img src="offsite.gif" tppabs="http://www.python.org/doc/current/icons/offsite.gif"
border='0' class='offsitelink' height='15' width='17' alt='[off-site link]'
></a></em>
<dd>
The Secure Hash Algorithm is defined by NIST document FIPS
PUB 180-1:
<em class='citetitle'><a
href="javascript:if(confirm('http://csrc.nist.gov/fips/fip180-1.txt \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://csrc.nist.gov/fips/fip180-1.txt'" tppabs="http://csrc.nist.gov/fips/fip180-1.txt"
title='Secure
Hash Standard'
>Secure
Hash Standard <img src="offsite.gif" tppabs="http://www.python.org/doc/current/icons/offsite.gif"
border='0' class='offsitelink' height='15' width='17' alt='[off-site link]'
></a></em>, published in April of 1995. It is
available online as plain text (at least one diagram was
omitted) and as PDF at
<a class="url" href="javascript:if(confirm('http://csrc.nist.gov/fips/fip180-1.pdf \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://csrc.nist.gov/fips/fip180-1.pdf'" tppabs="http://csrc.nist.gov/fips/fip180-1.pdf">http://csrc.nist.gov/fips/fip180-1.pdf <img src="offsite.gif" tppabs="http://www.python.org/doc/current/icons/offsite.gif"
border='0' class='offsitelink' height='15' width='17' alt='[off-site link]'
></a>.
</dl>
</div>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-md5.html" tppabs="http://www.python.org/doc/current/lib/module-md5.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="crypto.html" tppabs="http://www.python.org/doc/current/lib/crypto.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-mpz.html" tppabs="http://www.python.org/doc/current/lib/module-mpz.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-md5.html" tppabs="http://www.python.org/doc/current/lib/module-md5.html">15.1 md5 </A>
<b class="navlabel">Up:</b> <a class="sectref" href="crypto.html" tppabs="http://www.python.org/doc/current/lib/crypto.html">15. Cryptographic Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-mpz.html" tppabs="http://www.python.org/doc/current/lib/module-mpz.html">15.3 mpz </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -