MD4.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. """MD4 cryptographic hash algorithm.
  21. MD4 is specified in RFC1320_ and produces the 128 bit digest of a message.
  22. >>> from Crypto.Hash import MD4
  23. >>>
  24. >>> h = MD4.new()
  25. >>> h.update(b'Hello')
  26. >>> print h.hexdigest()
  27. MD4 stand for Message Digest version 4, and it was invented by Rivest in 1990.
  28. This algorithm is insecure. Do not use it for new designs.
  29. .. _RFC1320: http://tools.ietf.org/html/rfc1320
  30. """
  31. _revision__ = "$Id$"
  32. __all__ = ['new', 'digest_size', 'MD4Hash' ]
  33. from Crypto.Util.py3compat import *
  34. from Crypto.Hash.hashalgo import HashAlgo
  35. import Crypto.Hash._MD4 as _MD4
  36. hashFactory = _MD4
  37. class MD4Hash(HashAlgo):
  38. """Class that implements an MD4 hash
  39. :undocumented: block_size
  40. """
  41. #: ASN.1 Object identifier (OID)::
  42. #:
  43. #: id-md2 OBJECT IDENTIFIER ::= {
  44. #: iso(1) member-body(2) us(840) rsadsi(113549)
  45. #: digestAlgorithm(2) 4
  46. #: }
  47. #:
  48. #: This value uniquely identifies the MD4 algorithm.
  49. oid = b('\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x04')
  50. digest_size = 16
  51. block_size = 64
  52. def __init__(self, data=None):
  53. HashAlgo.__init__(self, hashFactory, data)
  54. def new(self, data=None):
  55. return MD4Hash(data)
  56. def new(data=None):
  57. """Return a fresh instance of the hash object.
  58. :Parameters:
  59. data : byte string
  60. The very first chunk of the message to hash.
  61. It is equivalent to an early call to `MD4Hash.update()`.
  62. Optional.
  63. :Return: A `MD4Hash` object
  64. """
  65. return MD4Hash().new(data)
  66. #: The size of the resulting hash in bytes.
  67. digest_size = MD4Hash.digest_size
  68. #: The internal block size of the hash algorithm in bytes.
  69. block_size = MD4Hash.block_size