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

« back to all changes in this revision

Viewing changes to lib/Crypto/Hash/SHA256.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
 
__revision__ = "$Id$"
22
 
 
23
 
__all__ = ['new', 'digest_size']
24
 
 
25
 
from Crypto.Util.wrapper import Wrapper
 
21
"""SHA-256 cryptographic hash algorithm.
 
22
 
 
23
SHA-256 belongs to the SHA-2_ family of cryptographic hashes.
 
24
It produces the 256 bit digest of a message.
 
25
 
 
26
    >>> from Crypto.Hash import SHA256
 
27
    >>>
 
28
    >>> h = SHA256.new()
 
29
    >>> h.update(b'Hello')
 
30
    >>> print h.hexdigest()
 
31
 
 
32
*SHA* stands for Secure Hash Algorithm.
 
33
 
 
34
.. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
 
35
"""
 
36
 
 
37
_revision__ = "$Id$"
 
38
 
 
39
__all__ = ['new', 'digest_size', 'SHA256Hash' ]
 
40
 
26
41
from Crypto.Util.py3compat import *
27
 
 
28
 
# The OID for SHA-256 is:
29
 
#
30
 
# id-sha256    OBJECT IDENTIFIER ::= {
31
 
#                       joint-iso-itu-t(2) country(16) us(840) organization(1)
32
 
#                       gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1
33
 
#                       }
34
 
#
35
 
oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01')
36
 
 
37
 
def new(data=b("")):
38
 
    obj = Wrapper(hashFactory, data)
39
 
    obj.oid = oid
40
 
    obj.new = globals()['new']
41
 
    if not hasattr(obj, 'digest_size'):
42
 
        obj.digest_size = digest_size
43
 
    return obj
 
42
from Crypto.Hash.hashalgo import HashAlgo
44
43
 
45
44
try:
46
45
    import hashlib
50
49
    from Crypto.Hash import _SHA256
51
50
    hashFactory = _SHA256
52
51
 
53
 
digest_size = 32
54
 
block_size = 64
 
52
class SHA256Hash(HashAlgo):
 
53
    """Class that implements a SHA-256 hash
 
54
    
 
55
    :undocumented: block_size
 
56
    """
 
57
 
 
58
    #: ASN.1 Object identifier (OID)::
 
59
    #:
 
60
    #:  id-sha256    OBJECT IDENTIFIER ::= {
 
61
    #:      joint-iso-itu-t(2) country(16) us(840) organization(1)
 
62
    #:       gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1
 
63
    #:  }
 
64
    #:
 
65
    #: This value uniquely identifies the SHA-256 algorithm.
 
66
    oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01')
 
67
 
 
68
    digest_size = 32
 
69
    block_size = 64
 
70
 
 
71
    def __init__(self, data=None):
 
72
        HashAlgo.__init__(self, hashFactory, data)
 
73
 
 
74
    def new(self, data=None):
 
75
        return SHA256Hash(data)
 
76
 
 
77
def new(data=None):
 
78
    """Return a fresh instance of the hash object.
 
79
 
 
80
    :Parameters:
 
81
       data : byte string
 
82
        The very first chunk of the message to hash.
 
83
        It is equivalent to an early call to `SHA256Hash.update()`.
 
84
        Optional.
 
85
 
 
86
    :Return: A `SHA256Hash` object
 
87
    """
 
88
    return SHA256Hash().new(data)
 
89
 
 
90
#: The size of the resulting hash in bytes.
 
91
digest_size = SHA256Hash.digest_size
 
92
 
 
93
#: The internal block size of the hash algorithm in bytes.
 
94
block_size = SHA256Hash.block_size
55
95