RIPEMD.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. """RIPEMD-160 cryptographic hash algorithm.
  21. RIPEMD-160_ produces the 160 bit digest of a message.
  22. >>> from Crypto.Hash import RIPEMD
  23. >>>
  24. >>> h = RIPEMD.new()
  25. >>> h.update(b'Hello')
  26. >>> print h.hexdigest()
  27. RIPEMD-160 stands for RACE Integrity Primitives Evaluation Message Digest
  28. with a 160 bit digest. It was invented by Dobbertin, Bosselaers, and Preneel.
  29. This algorithm is considered secure, although it has not been scrutinized as
  30. extensively as SHA-1. Moreover, it provides an informal security level of just
  31. 80bits.
  32. .. _RIPEMD-160: http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
  33. """
  34. _revision__ = "$Id$"
  35. __all__ = ['new', 'digest_size', 'RIPEMD160Hash' ]
  36. from Crypto.Util.py3compat import *
  37. from Crypto.Hash.hashalgo import HashAlgo
  38. import Crypto.Hash._RIPEMD160 as _RIPEMD160
  39. hashFactory = _RIPEMD160
  40. class RIPEMD160Hash(HashAlgo):
  41. """Class that implements a RIPMD-160 hash
  42. :undocumented: block_size
  43. """
  44. #: ASN.1 Object identifier (OID)::
  45. #:
  46. #: id-ripemd160 OBJECT IDENTIFIER ::= {
  47. #: iso(1) identified-organization(3) teletrust(36)
  48. #: algorithm(3) hashAlgorithm(2) ripemd160(1)
  49. #: }
  50. #:
  51. #: This value uniquely identifies the RIPMD-160 algorithm.
  52. oid = b("\x06\x05\x2b\x24\x03\x02\x01")
  53. digest_size = 20
  54. block_size = 64
  55. def __init__(self, data=None):
  56. HashAlgo.__init__(self, hashFactory, data)
  57. def new(self, data=None):
  58. return RIPEMD160Hash(data)
  59. def new(data=None):
  60. """Return a fresh instance of the hash object.
  61. :Parameters:
  62. data : byte string
  63. The very first chunk of the message to hash.
  64. It is equivalent to an early call to `RIPEMD160Hash.update()`.
  65. Optional.
  66. :Return: A `RIPEMD160Hash` object
  67. """
  68. return RIPEMD160Hash().new(data)
  69. #: The size of the resulting hash in bytes.
  70. digest_size = RIPEMD160Hash.digest_size
  71. #: The internal block size of the hash algorithm in bytes.
  72. block_size = RIPEMD160Hash.block_size