Add wider support and validation for valid SHA1/256 fingerprints.
Review Request #14925 — Created March 17, 2026 and submitted
CertificateFingerprints.from_string()now supports plain SHA1 and
SHA256 strings (without the semicolons used in fingerprints). These will
be converted into the correct format.The values are also now validated against regexes. This will key off
the pattern and information needed to optionally normalize the string
and then set it, based on the length of the string provided. If there
isn't a suitable match,Nonewill be returned.
Unit tests pass.
| Summary | ID |
|---|---|
| db7091e37d7b46b63a1a7ba68b3628c7ac5af3a3 |
| Description | From | Last Updated |
|---|---|---|
|
A SHA1 hash is 20 bytes, but that corresponds to 40 hex characters. Your test didn't catch this because the … |
|
|
|
Can we add a type hint here? |
|
|
|
Leftover debug code. |
|
|
|
It might be nice to switch to re.fullmatch() for these, to make it less fragile for future changes. I also … |
|
|
|
Might be nicer to do length == X and cls.SHA... here instead of nested if statements? Same with the elifs … |
|
-
-
A SHA1 hash is 20 bytes, but that corresponds to 40 hex characters. Your test didn't catch this because the invalid character happened to be within the first half of the hash.
-
It might be nice to switch to
re.fullmatch()for these, to make it less fragile for future changes.I also agree with Michelle's comment about combining the conditionals--as long as length is listed first, it won't check the regex match.
It might also be nice to order these to keep sha1 and sha256 together, instead of going in reverse order by length.
- Change Summary:
-
- Reworked
from_string()'s length checks to index into a dictionary of information used to validate/normalize/set the fingerprint. This keeps from having to check conditions for each possible input value, and centralizes the SHA-to-fingerprint logic. - Updated unit tests to inject an invalid character only at the end of the string.
- Updated
SHA1_REto have the right length, and switched tore.fullmatch(). - Updated versions to RB 8.
- Reworked
- Description:
-
CertificateFingerprints.from_string()now supports plain SHA1 andSHA256 strings (without the semicolons used in fingerprints). These will be converted into the correct format. ~ The values are also now validated against regexes. This will first check
~ the line lengths and then follow up with a pattern match. ~ The values are also now validated against regexes. This will key off
~ the pattern and information needed to optionally normalize the string + and then set it, based on the length of the string provided. If there + isn't a suitable match, Nonewill be returned. - Commits:
-
Summary ID 3591bcfc48229e9dc2e6d64c8267028e03d0b358 db7091e37d7b46b63a1a7ba68b3628c7ac5af3a3