SHA256.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. #
  3. # ===================================================================
  4. # The contents of this file are dedicated to the public domain. To
  5. # the extent that dedication to the public domain is not available,
  6. # everyone is granted a worldwide, perpetual, royalty-free,
  7. # non-exclusive license to exercise all rights associated with the
  8. # contents of this file for any purpose whatsoever.
  9. # No rights are reserved.
  10. #
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  12. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  13. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  14. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  15. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. # SOFTWARE.
  19. # ===================================================================
  20. """SHA-256 cryptographic hash algorithm.
  21. SHA-256 belongs to the SHA-2_ family of cryptographic hashes.
  22. It produces the 256 bit digest of a message.
  23. >>> from Crypto.Hash import SHA256
  24. >>>
  25. >>> h = SHA256.new()
  26. >>> h.update(b'Hello')
  27. >>> print h.hexdigest()
  28. *SHA* stands for Secure Hash Algorithm.
  29. .. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  30. """
  31. _revision__ = "$Id$"
  32. __all__ = ['new', 'digest_size', 'SHA256Hash' ]
  33. from Crypto.Util.py3compat import *
  34. from Crypto.Hash.hashalgo import HashAlgo
  35. try:
  36. import hashlib
  37. hashFactory = hashlib.sha256
  38. except ImportError:
  39. from Crypto.Hash import _SHA256
  40. hashFactory = _SHA256
  41. class SHA256Hash(HashAlgo):
  42. """Class that implements a SHA-256 hash
  43. :undocumented: block_size
  44. """
  45. #: ASN.1 Object identifier (OID)::
  46. #:
  47. #: id-sha256 OBJECT IDENTIFIER ::= {
  48. #: joint-iso-itu-t(2) country(16) us(840) organization(1)
  49. #: gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1
  50. #: }
  51. #:
  52. #: This value uniquely identifies the SHA-256 algorithm.
  53. oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01')
  54. digest_size = 32
  55. block_size = 64
  56. def __init__(self, data=None):
  57. HashAlgo.__init__(self, hashFactory, data)
  58. def new(self, data=None):
  59. return SHA256Hash(data)
  60. def new(data=None):
  61. """Return a fresh instance of the hash object.
  62. :Parameters:
  63. data : byte string
  64. The very first chunk of the message to hash.
  65. It is equivalent to an early call to `SHA256Hash.update()`.
  66. Optional.
  67. :Return: A `SHA256Hash` object
  68. """
  69. return SHA256Hash().new(data)
  70. #: The size of the resulting hash in bytes.
  71. digest_size = SHA256Hash.digest_size
  72. #: The internal block size of the hash algorithm in bytes.
  73. block_size = SHA256Hash.block_size