hashalgo.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 binascii import hexlify
  21. class HashAlgo:
  22. """A generic class for an abstract cryptographic hash algorithm.
  23. :undocumented: block_size
  24. """
  25. #: The size of the resulting hash in bytes.
  26. digest_size = None
  27. #: The internal block size of the hash algorithm in bytes.
  28. block_size = None
  29. def __init__(self, hashFactory, data=None):
  30. """Initialize the hash object.
  31. :Parameters:
  32. hashFactory : callable
  33. An object that will generate the actual hash implementation.
  34. *hashFactory* must have a *new()* method, or must be directly
  35. callable.
  36. data : byte string
  37. The very first chunk of the message to hash.
  38. It is equivalent to an early call to `update()`.
  39. """
  40. if hasattr(hashFactory, 'new'):
  41. self._hash = hashFactory.new()
  42. else:
  43. self._hash = hashFactory()
  44. if data:
  45. self.update(data)
  46. def update(self, data):
  47. """Continue hashing of a message by consuming the next chunk of data.
  48. Repeated calls are equivalent to a single call with the concatenation
  49. of all the arguments. In other words:
  50. >>> m.update(a); m.update(b)
  51. is equivalent to:
  52. >>> m.update(a+b)
  53. :Parameters:
  54. data : byte string
  55. The next chunk of the message being hashed.
  56. """
  57. return self._hash.update(data)
  58. def digest(self):
  59. """Return the **binary** (non-printable) digest of the message that has been hashed so far.
  60. This method does not change the state of the hash object.
  61. You can continue updating the object after calling this function.
  62. :Return: A byte string of `digest_size` bytes. It may contain non-ASCII
  63. characters, including null bytes.
  64. """
  65. return self._hash.digest()
  66. def hexdigest(self):
  67. """Return the **printable** digest of the message that has been hashed so far.
  68. This method does not change the state of the hash object.
  69. :Return: A string of 2* `digest_size` characters. It contains only
  70. hexadecimal ASCII digits.
  71. """
  72. return self._hash.hexdigest()
  73. def copy(self):
  74. """Return a copy ("clone") of the hash object.
  75. The copy will have the same internal state as the original hash
  76. object.
  77. This can be used to efficiently compute the digests of strings that
  78. share a common initial substring.
  79. :Return: A hash object of the same type
  80. """
  81. return self._hash.copy()
  82. def new(self, data=None):
  83. """Return a fresh instance of the hash object.
  84. Unlike the `copy` method, the internal state of the object is empty.
  85. :Parameters:
  86. data : byte string
  87. The next chunk of the message being hashed.
  88. :Return: A hash object of the same type
  89. """
  90. pass