Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/Crypto/Random/Fortuna/SHAd256.py: 79%
38 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: ascii -*-
2#
3# Random/Fortuna/SHAd256.py : SHA_d-256 hash function implementation
4#
5# Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
6#
7# ===================================================================
8# The contents of this file are dedicated to the public domain. To
9# the extent that dedication to the public domain is not available,
10# everyone is granted a worldwide, perpetual, royalty-free,
11# non-exclusive license to exercise all rights associated with the
12# contents of this file for any purpose whatsoever.
13# No rights are reserved.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22# SOFTWARE.
23# ===================================================================
25"""\
26SHA_d-256 hash function implementation.
28This module should comply with PEP 247.
29"""
31__revision__ = "$Id$"
32__all__ = ['new', 'digest_size']
34import sys
35if sys.version_info[0] == 2 and sys.version_info[1] == 1:
36 from Crypto.Util.py21compat import *
37from Crypto.Util.py3compat import *
39from binascii import b2a_hex
41from Crypto.Hash import SHA256
43assert SHA256.digest_size == 32
45class _SHAd256(object):
46 """SHA-256, doubled.
48 Returns SHA-256(SHA-256(data)).
49 """
51 digest_size = SHA256.digest_size
53 _internal = object()
55 def __init__(self, internal_api_check, sha256_hash_obj):
56 if internal_api_check is not self._internal:
57 raise AssertionError("Do not instantiate this class directly. Use %s.new()" % (__name__,))
58 self._h = sha256_hash_obj
60 # PEP 247 "copy" method
61 def copy(self):
62 """Return a copy of this hashing object"""
63 return _SHAd256(SHAd256._internal, self._h.copy())
65 # PEP 247 "digest" method
66 def digest(self):
67 """Return the hash value of this object as a binary string"""
68 retval = SHA256.new(self._h.digest()).digest()
69 assert len(retval) == 32
70 return retval
72 # PEP 247 "hexdigest" method
73 def hexdigest(self):
74 """Return the hash value of this object as a (lowercase) hexadecimal string"""
75 retval = b2a_hex(self.digest())
76 assert len(retval) == 64
77 if sys.version_info[0] == 2:
78 return retval
79 else:
80 return retval.decode()
82 # PEP 247 "update" method
83 def update(self, data):
84 self._h.update(data)
86# PEP 247 module-level "digest_size" variable
87digest_size = _SHAd256.digest_size
89# PEP 247 module-level "new" function
90def new(data=None):
91 """Return a new SHAd256 hashing object"""
92 if not data:
93 data=b("")
94 sha = _SHAd256(_SHAd256._internal, SHA256.new(data))
95 sha.new = globals()['new']
96 return sha
98# vim:set ts=4 sw=4 sts=4 expandtab: