~ubuntu-branches/ubuntu/saucy/python-crypto/saucy-proposed

« back to all changes in this revision

Viewing changes to lib/Crypto/Hash/SHA.py

  • Committer: Package Import Robot
  • Author(s): Sebastian Ramacher
  • Date: 2012-05-24 20:16:34 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20120524201634-de6vxznjsdgekuwp
Tags: 2.6-1
* New upstream release.
  - Fixes CVE-2012-2417: insecure ElGamal key generation.
* Set urgency to high since this fixes a security issue.
* debian/copyright:
  - Fix formatting.
  - Update Format URL.
* debian/control:
  - Bump Standards-Version to 3.9.3 (no changes required).
  - Drop qNEW from Description since qNEW has been removed.
* debian/patches: Remove posixread.patch (not needed anymore).

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# SOFTWARE.
19
19
# ===================================================================
20
20
 
21
 
# Just use the SHA module from the Python standard library
22
 
 
23
 
__revision__ = "$Id$"
24
 
 
25
 
__all__ = ['new', 'digest_size']
 
21
"""SHA-1 cryptographic hash algorithm.
 
22
 
 
23
SHA-1_ produces the 160 bit digest of a message.
 
24
 
 
25
    >>> from Crypto.Hash import SHA
 
26
    >>>
 
27
    >>> h = SHA.new()
 
28
    >>> h.update(b'Hello')
 
29
    >>> print h.hexdigest()
 
30
 
 
31
*SHA* stands for Secure Hash Algorithm.
 
32
 
 
33
This algorithm is not considered secure. Do not use it for new designs.
 
34
 
 
35
.. _SHA-1: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
 
36
"""
 
37
 
 
38
_revision__ = "$Id$"
 
39
 
 
40
__all__ = ['new', 'digest_size', 'SHA1Hash' ]
26
41
 
27
42
from Crypto.Util.py3compat import *
28
 
from Crypto.Util.wrapper import Wrapper
29
 
 
30
 
# The OID for SHA-1 is:
31
 
#
32
 
# id-sha1    OBJECT IDENTIFIER ::= {
33
 
#          iso(1) identified-organization(3) oiw(14) secsig(3)
34
 
#          algorithms(2) 26
35
 
#      }
36
 
oid = b('\x06\x05\x2b\x0e\x03\x02\x1a')
37
 
 
38
 
def new(data=b("")):
39
 
    obj = Wrapper(hashFactory, data)
40
 
    obj.oid = oid
41
 
    obj.new = globals()['new']
42
 
    if not hasattr(obj, 'digest_size'):
43
 
        obj.digest_size = digest_size
44
 
    return obj
 
43
from Crypto.Hash.hashalgo import HashAlgo
45
44
 
46
45
try:
47
46
    # The sha module is deprecated in Python 2.6, so use hashlib when possible.
52
51
    import sha
53
52
    hashFactory = sha
54
53
 
55
 
digest_size = 20
56
 
block_size = 64
 
54
class SHA1Hash(HashAlgo):
 
55
    """Class that implements a SHA-1 hash
 
56
    
 
57
    :undocumented: block_size
 
58
    """
 
59
 
 
60
    #: ASN.1 Object identifier (OID)::
 
61
    #:
 
62
    #:  id-sha1    OBJECT IDENTIFIER ::= {
 
63
    #:      iso(1) identified-organization(3) oiw(14) secsig(3)
 
64
    #:       algorithms(2) 26
 
65
    #:  }
 
66
    #:
 
67
    #: This value uniquely identifies the SHA-1 algorithm.
 
68
    oid = b('\x06\x05\x2b\x0e\x03\x02\x1a')
 
69
 
 
70
    digest_size = 20
 
71
    block_size = 64
 
72
 
 
73
    def __init__(self, data=None):
 
74
        HashAlgo.__init__(self, hashFactory, data)
 
75
 
 
76
    def new(self, data=None):
 
77
        return SHA1Hash(data)
 
78
 
 
79
def new(data=None):
 
80
    """Return a fresh instance of the hash object.
 
81
 
 
82
    :Parameters:
 
83
       data : byte string
 
84
        The very first chunk of the message to hash.
 
85
        It is equivalent to an early call to `SHA1Hash.update()`.
 
86
        Optional.
 
87
 
 
88
    :Return: A `SHA1Hash` object
 
89
    """
 
90
    return SHA1Hash().new(data)
 
91
 
 
92
#: The size of the resulting hash in bytes.
 
93
digest_size = SHA1Hash.digest_size
 
94
 
 
95
#: The internal block size of the hash algorithm in bytes.
 
96
block_size = SHA1Hash.block_size
 
97
 
 
98