Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/Crypto/Hash/MD5.py: 75%

40 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 07:03 +0000

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 

21"""MD5 cryptographic hash algorithm. 

22 

23MD5 is specified in RFC1321_ and produces the 128 bit digest of a message. 

24 

25 >>> from Crypto.Hash import MD5 

26 >>> 

27 >>> h = MD5.new() 

28 >>> h.update(b'Hello') 

29 >>> print h.hexdigest() 

30 

31MD5 stand for Message Digest version 5, and it was invented by Rivest in 1991. 

32 

33This algorithm is insecure. Do not use it for new designs. 

34 

35.. _RFC1321: http://tools.ietf.org/html/rfc1321  

36""" 

37 

38 

39 

40_revision__ = "$Id$" 

41 

42__all__ = ['new', 'block_size', 'digest_size'] 

43 

44from Crypto.Util.py3compat import * 

45if sys.version_info[0] == 2 and sys.version_info[1] == 1: 

46 from Crypto.Util.py21compat import * 

47 

48def __make_constructor(): 

49 try: 

50 # The md5 module is deprecated in Python 2.6, so use hashlib when possible. 

51 from hashlib import md5 as _hash_new 

52 except ImportError: 

53 from md5 import new as _hash_new 

54 

55 h = _hash_new() 

56 if hasattr(h, 'new') and hasattr(h, 'name') and hasattr(h, 'digest_size') and hasattr(h, 'block_size'): 

57 # The module from stdlib has the API that we need. Just use it. 

58 return _hash_new 

59 else: 

60 # Wrap the hash object in something that gives us the expected API. 

61 _copy_sentinel = object() 

62 class _MD5(object): 

63 digest_size = 16 

64 block_size = 64 

65 name = "md5" 

66 def __init__(self, *args): 

67 if args and args[0] is _copy_sentinel: 

68 self._h = args[1] 

69 else: 

70 self._h = _hash_new(*args) 

71 def copy(self): 

72 return _MD5(_copy_sentinel, self._h.copy()) 

73 def update(self, *args): 

74 f = self.update = self._h.update 

75 f(*args) 

76 def digest(self): 

77 f = self.digest = self._h.digest 

78 return f() 

79 def hexdigest(self): 

80 f = self.hexdigest = self._h.hexdigest 

81 return f() 

82 _MD5.new = _MD5 

83 return _MD5 

84 

85new = __make_constructor() 

86del __make_constructor 

87 

88#: The size of the resulting hash in bytes. 

89digest_size = new().digest_size 

90 

91#: The internal block size of the hash algorithm in bytes. 

92block_size = new().block_size