Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/astroid/brain/brain_hashlib.py: 32%
19 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:53 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:53 +0000
1# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
2# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
3# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
5from astroid.brain.helpers import register_module_extender
6from astroid.builder import parse
7from astroid.const import PY39_PLUS
8from astroid.manager import AstroidManager
11def _hashlib_transform():
12 maybe_usedforsecurity = ", usedforsecurity=True" if PY39_PLUS else ""
13 init_signature = f"value=''{maybe_usedforsecurity}"
14 digest_signature = "self"
15 shake_digest_signature = "self, length"
17 template = """
18 class %(name)s:
19 def __init__(self, %(init_signature)s): pass
20 def digest(%(digest_signature)s):
21 return %(digest)s
22 def copy(self):
23 return self
24 def update(self, value): pass
25 def hexdigest(%(digest_signature)s):
26 return ''
27 @property
28 def name(self):
29 return %(name)r
30 @property
31 def block_size(self):
32 return 1
33 @property
34 def digest_size(self):
35 return 1
36 """
38 algorithms_with_signature = dict.fromkeys(
39 [
40 "md5",
41 "sha1",
42 "sha224",
43 "sha256",
44 "sha384",
45 "sha512",
46 "sha3_224",
47 "sha3_256",
48 "sha3_384",
49 "sha3_512",
50 ],
51 (init_signature, digest_signature),
52 )
54 blake2b_signature = (
55 "data=b'', *, digest_size=64, key=b'', salt=b'', "
56 "person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, "
57 f"node_depth=0, inner_size=0, last_node=False{maybe_usedforsecurity}"
58 )
60 blake2s_signature = (
61 "data=b'', *, digest_size=32, key=b'', salt=b'', "
62 "person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, "
63 f"node_depth=0, inner_size=0, last_node=False{maybe_usedforsecurity}"
64 )
66 shake_algorithms = dict.fromkeys(
67 ["shake_128", "shake_256"],
68 (init_signature, shake_digest_signature),
69 )
70 algorithms_with_signature.update(shake_algorithms)
72 algorithms_with_signature.update(
73 {
74 "blake2b": (blake2b_signature, digest_signature),
75 "blake2s": (blake2s_signature, digest_signature),
76 }
77 )
79 classes = "".join(
80 template
81 % {
82 "name": hashfunc,
83 "digest": 'b""',
84 "init_signature": init_signature,
85 "digest_signature": digest_signature,
86 }
87 for hashfunc, (
88 init_signature,
89 digest_signature,
90 ) in algorithms_with_signature.items()
91 )
93 return parse(classes)
96register_module_extender(AstroidManager(), "hashlib", _hashlib_transform)