Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/Crypto/Hash/__init__.py: 42%
83 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 07:03 +0000
« 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# ===================================================================
21"""Hashing algorithms
23Hash functions take arbitrary binary strings as input, and produce a random-like output
24of fixed size that is dependent on the input; it should be practically infeasible
25to derive the original input data given only the hash function's
26output. In other words, the hash function is *one-way*.
28It should also not be practically feasible to find a second piece of data
29(a *second pre-image*) whose hash is the same as the original message
30(*weak collision resistance*).
32Finally, it should not be feasible to find two arbitrary messages with the
33same hash (*strong collision resistance*).
35The output of the hash function is called the *digest* of the input message.
36In general, the security of a hash function is related to the length of the
37digest. If the digest is *n* bits long, its security level is roughly comparable
38to the the one offered by an *n/2* bit encryption algorithm.
40Hash functions can be used simply as a integrity check, or, in
41association with a public-key algorithm, can be used to implement
42digital signatures.
44The hashing modules here all support the interface described in `PEP
45247`_ , "API for Cryptographic Hash Functions".
47.. _`PEP 247` : http://www.python.org/dev/peps/pep-0247/
49:undocumented: _MD2, _MD4, _RIPEMD160, _SHA224, _SHA256, _SHA384, _SHA512
50"""
52__all__ = ['HMAC', 'MD2', 'MD4', 'MD5', 'RIPEMD160', 'SHA1',
53 'SHA224', 'SHA256', 'SHA384', 'SHA512', 'CMAC']
55__revision__ = "$Id$"
57import sys
58if sys.version_info[0] == 2 and sys.version_info[1] == 1:
59 from Crypto.Util.py21compat import *
60from Crypto.Util.py3compat import *
62def new(algo, *args):
63 """Initialize a new hash object.
65 The first argument to this function may be an algorithm name or another
66 hash object.
68 This function has significant overhead. It's recommended that you instead
69 import and use the individual hash modules directly.
70 """
72 # Try just invoking algo.new()
73 # We do this first so that this is the fastest.
74 try:
75 new_func = algo.new
76 except AttributeError:
77 pass
78 else:
79 return new_func(*args)
81 # Try getting the algorithm name.
82 if isinstance(algo, str):
83 name = algo
84 else:
85 try:
86 name = algo.name
87 except AttributeError:
88 raise ValueError("unsupported hash type %r" % (algo,))
90 # Got the name. Let's see if we have a PyCrypto implementation.
91 try:
92 new_func = _new_funcs[name]
93 except KeyError:
94 # No PyCrypto implementation. Try hashlib.
95 try:
96 import hashlib
97 except ImportError:
98 # There is no hashlib.
99 raise ValueError("unsupported hash type %s" % (name,))
100 return hashlib.new(name, *args)
101 else:
102 # We have a PyCrypto implementation. Instantiate it.
103 return new_func(*args)
105# This dict originally gets the following _*_new methods, but its members get
106# replaced with the real new() methods of the various hash modules as they are
107# used. We do it without locks to improve performance, which is safe in
108# CPython because dict access is atomic in CPython. This might break PyPI.
109_new_funcs = {}
111def _md2_new(*args):
112 from Crypto.Hash import MD2
113 _new_funcs['MD2'] = _new_funcs['md2'] = MD2.new
114 return MD2.new(*args)
115_new_funcs['MD2'] = _new_funcs['md2'] = _md2_new
116del _md2_new
118def _md4_new(*args):
119 from Crypto.Hash import MD4
120 _new_funcs['MD4'] = _new_funcs['md4'] = MD4.new
121 return MD4.new(*args)
122_new_funcs['MD4'] = _new_funcs['md4'] = _md4_new
123del _md4_new
125def _md5_new(*args):
126 from Crypto.Hash import MD5
127 _new_funcs['MD5'] = _new_funcs['md5'] = MD5.new
128 return MD5.new(*args)
129_new_funcs['MD5'] = _new_funcs['md5'] = _md5_new
130del _md5_new
132def _ripemd160_new(*args):
133 from Crypto.Hash import RIPEMD160
134 _new_funcs['RIPEMD160'] = _new_funcs['ripemd160'] = \
135 _new_funcs['RIPEMD'] = _new_funcs['ripemd'] = RIPEMD160.new
136 return RIPEMD160.new(*args)
137_new_funcs['RIPEMD160'] = _new_funcs['ripemd160'] = \
138 _new_funcs['RIPEMD'] = _new_funcs['ripemd'] = _ripemd160_new
139del _ripemd160_new
141def _sha1_new(*args):
142 from Crypto.Hash import SHA1
143 _new_funcs['SHA1'] = _new_funcs['sha1'] = \
144 _new_funcs['SHA'] = _new_funcs['sha'] = SHA1.new
145 return SHA1.new(*args)
146_new_funcs['SHA1'] = _new_funcs['sha1'] = \
147 _new_funcs['SHA'] = _new_funcs['sha'] = _sha1_new
148del _sha1_new
150def _sha224_new(*args):
151 from Crypto.Hash import SHA224
152 _new_funcs['SHA224'] = _new_funcs['sha224'] = SHA224.new
153 return SHA224.new(*args)
154_new_funcs['SHA224'] = _new_funcs['sha224'] = _sha224_new
155del _sha224_new
157def _sha256_new(*args):
158 from Crypto.Hash import SHA256
159 _new_funcs['SHA256'] = _new_funcs['sha256'] = SHA256.new
160 return SHA256.new(*args)
161_new_funcs['SHA256'] = _new_funcs['sha256'] = _sha256_new
162del _sha256_new
164def _sha384_new(*args):
165 from Crypto.Hash import SHA384
166 _new_funcs['SHA384'] = _new_funcs['sha384'] = SHA384.new
167 return SHA384.new(*args)
168_new_funcs['SHA384'] = _new_funcs['sha384'] = _sha384_new
169del _sha384_new
171def _sha512_new(*args):
172 from Crypto.Hash import SHA512
173 _new_funcs['SHA512'] = _new_funcs['sha512'] = SHA512.new
174 return SHA512.new(*args)
175_new_funcs['SHA512'] = _new_funcs['sha512'] = _sha512_new
176del _sha512_new