@@ -72,22 +72,8 @@ def _prepare_password(self, password: str) -> typing.Tuple[str, str]:
72
72
used by the Pwned Passwords API.
73
73
74
74
"""
75
- # Python's documentation states that the named constructors for particular
76
- # hashes are to be preferred due to better peformance, which here would mean
77
- # calling hashlib.sha1() instead of hashlib.new("sha1").
78
- #
79
- # However, security linters and some restricted runtime environments do not
80
- # allow access to SHA-1 unless a flag is passed to indicate it is not being used
81
- # for cryptographic purposes. This is done by passing usedforsecurity=False to
82
- # the constructor, but that argument was not added in the named constructors
83
- # until Python 3.9, while we currently support all the way back to 3.7. Luckily
84
- # hashlib.new() in Python 3.7 and 3.8 accepts arbitrary arguments without
85
- # complaint. So as a slightly hacky workaround we use new(), passing the
86
- # usedforsecurity=False argument, and rely on it being ignored for Python
87
- # 3.7/3.8, and interpreted as intended on Python 3.9+. Once support for Python
88
- # 3.7 and 3.8 ends, this can be updated to call hashlib.sha1() directly.
89
75
password_hash = (
90
- hashlib .new ( " sha1" , password .encode ("utf-8" ), usedforsecurity = False )
76
+ hashlib .sha1 ( password .encode ("utf-8" ), usedforsecurity = False )
91
77
.hexdigest ()
92
78
.upper () # Pwned Passwords wants all hashes to be uppercase.
93
79
)
0 commit comments