blob: 6d1da68da8bac5022040236ce4a427138ca9ff3a [file] [log] [blame]
Fred Drake957ac3f1999-04-23 21:52:18 +00001\section{\module{sha} ---
Raymond Hettinger1e4cf672003-09-15 18:20:52 +00002 SHA-1 message digest algorithm}
Fred Drake957ac3f1999-04-23 21:52:18 +00003
4\declaremodule{builtin}{sha}
5\modulesynopsis{NIST's secure hash algorithm, SHA.}
6\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
7
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00008\deprecated{2.5}{Use the \refmodule{hashlib} module instead.}
9
Fred Drake957ac3f1999-04-23 21:52:18 +000010
11This module implements the interface to NIST's\index{NIST} secure hash
Raymond Hettinger1e4cf672003-09-15 18:20:52 +000012algorithm,\index{Secure Hash Algorithm} known as SHA-1. SHA-1 is an
13improved version of the original SHA hash algorithm. It is used in
Tim Peters1de80982000-09-18 15:34:57 +000014the same way as the \refmodule{md5} module:\ use \function{new()}
Fred Drake957ac3f1999-04-23 21:52:18 +000015to create an sha object, then feed this object with arbitrary strings
16using the \method{update()} method, and at any point you can ask it
Thomas Woutersf8316632000-07-16 19:01:10 +000017for the \dfn{digest} of the concatenation of the strings fed to it
Raymond Hettinger1e4cf672003-09-15 18:20:52 +000018so far.\index{checksum!SHA} SHA-1 digests are 160 bits instead of
Tim Peters1de80982000-09-18 15:34:57 +000019MD5's 128 bits.
Fred Drake957ac3f1999-04-23 21:52:18 +000020
21
22\begin{funcdesc}{new}{\optional{string}}
23 Return a new sha object. If \var{string} is present, the method
24 call \code{update(\var{string})} is made.
25\end{funcdesc}
26
27
28The following values are provided as constants in the module and as
29attributes of the sha objects returned by \function{new()}:
30
31\begin{datadesc}{blocksize}
32 Size of the blocks fed into the hash function; this is always
33 \code{1}. This size is used to allow an arbitrary string to be
34 hashed.
35\end{datadesc}
36
Andrew M. Kuchlingbc4a1c22001-11-02 21:44:09 +000037\begin{datadesc}{digest_size}
Fred Drake957ac3f1999-04-23 21:52:18 +000038 The size of the resulting digest in bytes. This is always
39 \code{20}.
40\end{datadesc}
41
42
Tim Peters1de80982000-09-18 15:34:57 +000043An sha object has the same methods as md5 objects:
Fred Drake957ac3f1999-04-23 21:52:18 +000044
Tim Peters1de80982000-09-18 15:34:57 +000045\begin{methoddesc}[sha]{update}{arg}
46Update the sha object with the string \var{arg}. Repeated calls are
47equivalent to a single call with the concatenation of all the
Fred Drake907e76b2001-07-06 20:30:11 +000048arguments: \code{m.update(a); m.update(b)} is equivalent to
Tim Peters1de80982000-09-18 15:34:57 +000049\code{m.update(a+b)}.
Fred Drake957ac3f1999-04-23 21:52:18 +000050\end{methoddesc}
51
Tim Peters1de80982000-09-18 15:34:57 +000052\begin{methoddesc}[sha]{digest}{}
53Return the digest of the strings passed to the \method{update()}
54method so far. This is a 20-byte string which may contain
55non-\ASCII{} characters, including null bytes.
56\end{methoddesc}
57
58\begin{methoddesc}[sha]{hexdigest}{}
59Like \method{digest()} except the digest is returned as a string of
60length 40, containing only hexadecimal digits. This may
61be used to exchange the value safely in email or other non-binary
62environments.
63\end{methoddesc}
64
65\begin{methoddesc}[sha]{copy}{}
66Return a copy (``clone'') of the sha object. This can be used to
67efficiently compute the digests of strings that share a common initial
68substring.
69\end{methoddesc}
Fred Drake957ac3f1999-04-23 21:52:18 +000070
71\begin{seealso}
Andrew M. Kuchling7dd8fda2004-06-29 13:35:01 +000072 \seetitle[http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf]
Fred Drake3c0fc842001-08-30 14:42:40 +000073 {Secure Hash Standard}
74 {The Secure Hash Algorithm is defined by NIST document FIPS
Andrew M. Kuchling7dd8fda2004-06-29 13:35:01 +000075 PUB 180-2:
76 \citetitle[http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf]
77 {Secure Hash Standard}, published in August 2002.}
Fred Drakea20c2652001-09-06 18:59:43 +000078
79 \seetitle[http://csrc.nist.gov/encryption/tkhash.html]
80 {Cryptographic Toolkit (Secure Hashing)}
81 {Links from NIST to various information on secure hashing.}
Fred Drake957ac3f1999-04-23 21:52:18 +000082\end{seealso}
Andrew M. Kuchling7dd8fda2004-06-29 13:35:01 +000083