summaryrefslogtreecommitdiff
path: root/astroid
diff options
context:
space:
mode:
authorMark Byrne <31762852+mbyrnepr2@users.noreply.github.com>2022-10-15 10:33:02 +0200
committerGitHub <noreply@github.com>2022-10-15 10:33:02 +0200
commit2bdcd08522f5088cc565995af4e083c38e60ae6c (patch)
tree7901dcc527c3d84bf51015dd1e4b036f4863fd0f /astroid
parent9c06593226519a690a22cf3482a5a5c72665a27a (diff)
downloadastroid-git-2bdcd08522f5088cc565995af4e083c38e60ae6c.tar.gz
Update the ``hashlib`` brain ``hash.digest`` & ``hash.hexdigest`` methods. (#1827)
* Add the ``length`` parameter to ``hash.digest`` & ``hash.hexdigest`` in the ``hashlib`` brain. * Move content in Changelog to a different section. Refs PyCQA/pylint#4039
Diffstat (limited to 'astroid')
-rw-r--r--astroid/brain/brain_hashlib.py104
1 files changed, 71 insertions, 33 deletions
diff --git a/astroid/brain/brain_hashlib.py b/astroid/brain/brain_hashlib.py
index b628361d..e321af6d 100644
--- a/astroid/brain/brain_hashlib.py
+++ b/astroid/brain/brain_hashlib.py
@@ -10,48 +10,86 @@ from astroid.manager import AstroidManager
def _hashlib_transform():
maybe_usedforsecurity = ", usedforsecurity=True" if PY39_PLUS else ""
- signature = f"value=''{maybe_usedforsecurity}"
+ init_signature = f"value=''{maybe_usedforsecurity}"
+ digest_signature = "self"
+ shake_digest_signature = "self, length"
+
template = """
- class %(name)s(object):
- def __init__(self, %(signature)s): pass
- def digest(self):
- return %(digest)s
- def copy(self):
- return self
- def update(self, value): pass
- def hexdigest(self):
- return ''
- @property
- def name(self):
- return %(name)r
- @property
- def block_size(self):
- return 1
- @property
- def digest_size(self):
- return 1
+ class %(name)s:
+ def __init__(self, %(init_signature)s): pass
+ def digest(%(digest_signature)s):
+ return %(digest)s
+ def copy(self):
+ return self
+ def update(self, value): pass
+ def hexdigest(%(digest_signature)s):
+ return ''
+ @property
+ def name(self):
+ return %(name)r
+ @property
+ def block_size(self):
+ return 1
+ @property
+ def digest_size(self):
+ return 1
"""
+
algorithms_with_signature = dict.fromkeys(
- ["md5", "sha1", "sha224", "sha256", "sha384", "sha512"], signature
+ [
+ "md5",
+ "sha1",
+ "sha224",
+ "sha256",
+ "sha384",
+ "sha512",
+ "sha3_224",
+ "sha3_256",
+ "sha3_384",
+ "sha3_512",
+ ],
+ (init_signature, digest_signature),
+ )
+
+ blake2b_signature = (
+ "data=b'', *, digest_size=64, key=b'', salt=b'', "
+ "person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, "
+ f"node_depth=0, inner_size=0, last_node=False{maybe_usedforsecurity}"
+ )
+
+ blake2s_signature = (
+ "data=b'', *, digest_size=32, key=b'', salt=b'', "
+ "person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, "
+ f"node_depth=0, inner_size=0, last_node=False{maybe_usedforsecurity}"
)
- blake2b_signature = f"data=b'', *, digest_size=64, key=b'', salt=b'', \
- person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
- node_depth=0, inner_size=0, last_node=False{maybe_usedforsecurity}"
- blake2s_signature = f"data=b'', *, digest_size=32, key=b'', salt=b'', \
- person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
- node_depth=0, inner_size=0, last_node=False{maybe_usedforsecurity}"
- new_algorithms = dict.fromkeys(
- ["sha3_224", "sha3_256", "sha3_384", "sha3_512", "shake_128", "shake_256"],
- signature,
+
+ shake_algorithms = dict.fromkeys(
+ ["shake_128", "shake_256"],
+ (init_signature, shake_digest_signature),
)
- algorithms_with_signature.update(new_algorithms)
+ algorithms_with_signature.update(shake_algorithms)
+
algorithms_with_signature.update(
- {"blake2b": blake2b_signature, "blake2s": blake2s_signature}
+ {
+ "blake2b": (blake2b_signature, digest_signature),
+ "blake2s": (blake2s_signature, digest_signature),
+ }
)
+
classes = "".join(
- template % {"name": hashfunc, "digest": 'b""', "signature": signature}
- for hashfunc, signature in algorithms_with_signature.items()
+ template
+ % {
+ "name": hashfunc,
+ "digest": 'b""',
+ "init_signature": init_signature,
+ "digest_signature": digest_signature,
+ }
+ for hashfunc, (
+ init_signature,
+ digest_signature,
+ ) in algorithms_with_signature.items()
)
+
return parse(classes)