SHAd256.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: ascii -*-
  2. #
  3. # Random/Fortuna/SHAd256.py : SHA_d-256 hash function implementation
  4. #
  5. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """\
  25. SHA_d-256 hash function implementation.
  26. This module should comply with PEP 247.
  27. """
  28. __revision__ = "$Id$"
  29. __all__ = ['new', 'digest_size']
  30. import sys
  31. if sys.version_info[0] == 2 and sys.version_info[1] == 1:
  32. from Crypto.Util.py21compat import *
  33. from Crypto.Util.py3compat import *
  34. from binascii import b2a_hex
  35. from Crypto.Hash import SHA256
  36. assert SHA256.digest_size == 32
  37. class _SHAd256(object):
  38. """SHA-256, doubled.
  39. Returns SHA-256(SHA-256(data)).
  40. """
  41. digest_size = SHA256.digest_size
  42. _internal = object()
  43. def __init__(self, internal_api_check, sha256_hash_obj):
  44. if internal_api_check is not self._internal:
  45. raise AssertionError("Do not instantiate this class directly. Use %s.new()" % (__name__,))
  46. self._h = sha256_hash_obj
  47. # PEP 247 "copy" method
  48. def copy(self):
  49. """Return a copy of this hashing object"""
  50. return _SHAd256(SHAd256._internal, self._h.copy())
  51. # PEP 247 "digest" method
  52. def digest(self):
  53. """Return the hash value of this object as a binary string"""
  54. retval = SHA256.new(self._h.digest()).digest()
  55. assert len(retval) == 32
  56. return retval
  57. # PEP 247 "hexdigest" method
  58. def hexdigest(self):
  59. """Return the hash value of this object as a (lowercase) hexadecimal string"""
  60. retval = b2a_hex(self.digest())
  61. assert len(retval) == 64
  62. if sys.version_info[0] == 2:
  63. return retval
  64. else:
  65. return retval.decode()
  66. # PEP 247 "update" method
  67. def update(self, data):
  68. self._h.update(data)
  69. # PEP 247 module-level "digest_size" variable
  70. digest_size = _SHAd256.digest_size
  71. # PEP 247 module-level "new" function
  72. def new(data=None):
  73. """Return a new SHAd256 hashing object"""
  74. if not data:
  75. data=b("")
  76. sha = _SHAd256(_SHAd256._internal, SHA256.new(data))
  77. sha.new = globals()['new']
  78. return sha
  79. # vim:set ts=4 sw=4 sts=4 expandtab: