SHA3_256.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. from Crypto.Util.py3compat import bord
  21. from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
  22. VoidPointer, SmartPointer,
  23. create_string_buffer,
  24. get_raw_buffer, c_size_t,
  25. expect_byte_string)
  26. from Crypto.Hash.keccak import _raw_keccak_lib
  27. class SHA3_256_Hash(object):
  28. """A SHA3-256 hash object.
  29. Do not instantiate directly.
  30. Use the :func:`new` function.
  31. :ivar oid: ASN.1 Object ID
  32. :vartype oid: string
  33. :ivar digest_size: the size in bytes of the resulting hash
  34. :vartype digest_size: integer
  35. """
  36. # The size of the resulting hash in bytes.
  37. digest_size = 32
  38. # ASN.1 Object ID
  39. oid = "2.16.840.1.101.3.4.2.8"
  40. def __init__(self, data, update_after_digest):
  41. self._update_after_digest = update_after_digest
  42. self._digest_done = False
  43. state = VoidPointer()
  44. result = _raw_keccak_lib.keccak_init(state.address_of(),
  45. c_size_t(self.digest_size * 2),
  46. 0x06)
  47. if result:
  48. raise ValueError("Error %d while instantiating SHA-3/256"
  49. % result)
  50. self._state = SmartPointer(state.get(),
  51. _raw_keccak_lib.keccak_destroy)
  52. if data:
  53. self.update(data)
  54. def update(self, data):
  55. """Continue hashing of a message by consuming the next chunk of data.
  56. Args:
  57. data (byte string): The next chunk of the message being hashed.
  58. """
  59. if self._digest_done and not self._update_after_digest:
  60. raise TypeError("You can only call 'digest' or 'hexdigest' on this object")
  61. expect_byte_string(data)
  62. result = _raw_keccak_lib.keccak_absorb(self._state.get(),
  63. data,
  64. c_size_t(len(data)))
  65. if result:
  66. raise ValueError("Error %d while updating SHA-3/256"
  67. % result)
  68. return self
  69. def digest(self):
  70. """Return the **binary** (non-printable) digest of the message that has been hashed so far.
  71. :return: The hash digest, computed over the data processed so far.
  72. Binary form.
  73. :rtype: byte string
  74. """
  75. self._digest_done = True
  76. bfr = create_string_buffer(self.digest_size)
  77. result = _raw_keccak_lib.keccak_digest(self._state.get(),
  78. bfr,
  79. c_size_t(self.digest_size))
  80. if result:
  81. raise ValueError("Error %d while instantiating SHA-3/256"
  82. % result)
  83. self._digest_value = get_raw_buffer(bfr)
  84. return self._digest_value
  85. def hexdigest(self):
  86. """Return the **printable** digest of the message that has been hashed so far.
  87. :return: The hash digest, computed over the data processed so far.
  88. Hexadecimal encoded.
  89. :rtype: string
  90. """
  91. return "".join(["%02x" % bord(x) for x in self.digest()])
  92. def new(self):
  93. """Create a fresh SHA3-256 hash object."""
  94. return type(self)(None, self._update_after_digest)
  95. def new(*args, **kwargs):
  96. """Create a new hash object.
  97. Args:
  98. data (byte string):
  99. The very first chunk of the message to hash.
  100. It is equivalent to an early call to :meth:`update`.
  101. update_after_digest (boolean):
  102. Whether :meth:`digest` can be followed by another :meth:`update`
  103. (default: ``False``).
  104. :Return: A :class:`SHA3_256_Hash` hash object
  105. """
  106. data = kwargs.pop("data", None)
  107. update_after_digest = kwargs.pop("update_after_digest", False)
  108. if len(args) == 1:
  109. if data:
  110. raise ValueError("Initial data for hash specified twice")
  111. data = args[0]
  112. if kwargs:
  113. raise TypeError("Unknown parameters: " + str(kwargs))
  114. return SHA3_256_Hash(data, update_after_digest)
  115. # The size of the resulting hash in bytes.
  116. digest_size = SHA3_256_Hash.digest_size