Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 1 | \section{\module{sha} --- |
Raymond Hettinger | 1e4cf67 | 2003-09-15 18:20:52 +0000 | [diff] [blame] | 2 | SHA-1 message digest algorithm} |
Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 3 | |
| 4 | \declaremodule{builtin}{sha} |
| 5 | \modulesynopsis{NIST's secure hash algorithm, SHA.} |
| 6 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
| 7 | |
| 8 | |
| 9 | This module implements the interface to NIST's\index{NIST} secure hash |
Raymond Hettinger | 1e4cf67 | 2003-09-15 18:20:52 +0000 | [diff] [blame] | 10 | algorithm,\index{Secure Hash Algorithm} known as SHA-1. SHA-1 is an |
| 11 | improved version of the original SHA hash algorithm. It is used in |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 12 | the same way as the \refmodule{md5} module:\ use \function{new()} |
Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 13 | to create an sha object, then feed this object with arbitrary strings |
| 14 | using the \method{update()} method, and at any point you can ask it |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 15 | for the \dfn{digest} of the concatenation of the strings fed to it |
Raymond Hettinger | 1e4cf67 | 2003-09-15 18:20:52 +0000 | [diff] [blame] | 16 | so far.\index{checksum!SHA} SHA-1 digests are 160 bits instead of |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 17 | MD5's 128 bits. |
Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | \begin{funcdesc}{new}{\optional{string}} |
| 21 | Return a new sha object. If \var{string} is present, the method |
| 22 | call \code{update(\var{string})} is made. |
| 23 | \end{funcdesc} |
| 24 | |
| 25 | |
| 26 | The following values are provided as constants in the module and as |
| 27 | attributes of the sha objects returned by \function{new()}: |
| 28 | |
| 29 | \begin{datadesc}{blocksize} |
| 30 | Size of the blocks fed into the hash function; this is always |
| 31 | \code{1}. This size is used to allow an arbitrary string to be |
| 32 | hashed. |
| 33 | \end{datadesc} |
| 34 | |
Andrew M. Kuchling | bc4a1c2 | 2001-11-02 21:44:09 +0000 | [diff] [blame] | 35 | \begin{datadesc}{digest_size} |
Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 36 | The size of the resulting digest in bytes. This is always |
| 37 | \code{20}. |
| 38 | \end{datadesc} |
| 39 | |
| 40 | |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 41 | An sha object has the same methods as md5 objects: |
Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 42 | |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 43 | \begin{methoddesc}[sha]{update}{arg} |
| 44 | Update the sha object with the string \var{arg}. Repeated calls are |
| 45 | equivalent to a single call with the concatenation of all the |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 46 | arguments: \code{m.update(a); m.update(b)} is equivalent to |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 47 | \code{m.update(a+b)}. |
Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 48 | \end{methoddesc} |
| 49 | |
Tim Peters | 1de8098 | 2000-09-18 15:34:57 +0000 | [diff] [blame] | 50 | \begin{methoddesc}[sha]{digest}{} |
| 51 | Return the digest of the strings passed to the \method{update()} |
| 52 | method so far. This is a 20-byte string which may contain |
| 53 | non-\ASCII{} characters, including null bytes. |
| 54 | \end{methoddesc} |
| 55 | |
| 56 | \begin{methoddesc}[sha]{hexdigest}{} |
| 57 | Like \method{digest()} except the digest is returned as a string of |
| 58 | length 40, containing only hexadecimal digits. This may |
| 59 | be used to exchange the value safely in email or other non-binary |
| 60 | environments. |
| 61 | \end{methoddesc} |
| 62 | |
| 63 | \begin{methoddesc}[sha]{copy}{} |
| 64 | Return a copy (``clone'') of the sha object. This can be used to |
| 65 | efficiently compute the digests of strings that share a common initial |
| 66 | substring. |
| 67 | \end{methoddesc} |
Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 68 | |
| 69 | \begin{seealso} |
Fred Drake | 3c0fc84 | 2001-08-30 14:42:40 +0000 | [diff] [blame] | 70 | \seetitle[http://csrc.nist.gov/publications/fips/fips180-1/fip180-1.txt] |
| 71 | {Secure Hash Standard} |
| 72 | {The Secure Hash Algorithm is defined by NIST document FIPS |
| 73 | PUB 180-1: |
| 74 | \citetitle[http://csrc.nist.gov/publications/fips/fips180-1/fip180-1.txt] |
| 75 | {Secure Hash Standard}, published in April of 1995. It is |
| 76 | available online as plain text (at least one diagram was |
| 77 | omitted) and as PDF at |
| 78 | \url{http://csrc.nist.gov/publications/fips/fips180-1/fip180-1.pdf}.} |
Fred Drake | a20c265 | 2001-09-06 18:59:43 +0000 | [diff] [blame] | 79 | |
| 80 | \seetitle[http://csrc.nist.gov/encryption/tkhash.html] |
| 81 | {Cryptographic Toolkit (Secure Hashing)} |
| 82 | {Links from NIST to various information on secure hashing.} |
Fred Drake | 957ac3f | 1999-04-23 21:52:18 +0000 | [diff] [blame] | 83 | \end{seealso} |