Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/Crypto/Hash/SHA1.py: 70%
40 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"""SHA-1 cryptographic hash algorithm.
23SHA-1_ produces the 160 bit digest of a message.
25 >>> from Crypto.Hash import SHA1
26 >>>
27 >>> h = SHA1.new()
28 >>> h.update(b'Hello')
29 >>> print h.hexdigest()
31*SHA* stands for Secure Hash Algorithm.
33This algorithm is not considered secure. Do not use it for new designs.
35.. _SHA-1: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
36"""
40_revision__ = "$Id$"
42__all__ = ['new', 'block_size', 'digest_size']
44from Crypto.Util.py3compat import *
45if sys.version_info[0] == 2 and sys.version_info[1] == 1:
46 from Crypto.Util.py21compat import *
48def __make_constructor():
49 try:
50 # The sha module is deprecated in Python 2.6, so use hashlib when possible.
51 from hashlib import sha1 as _hash_new
52 except ImportError:
53 from sha import new as _hash_new
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 _SHA1(object):
63 digest_size = 20
64 block_size = 64
65 name = "sha1"
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 _SHA1(_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 _SHA1.new = _SHA1
83 return _SHA1
85new = __make_constructor()
86del __make_constructor
88#: The size of the resulting hash in bytes.
89digest_size = new().digest_size
91#: The internal block size of the hash algorithm in bytes.
92block_size = new().block_size