Returns a digest name and the payload for a response hash. If a legacy protocol is detected based on the message length or contents the digest name returned will be empty to indicate legacy mode where MD5 and no digest prefix should be sent.
(message)
| 885 | |
| 886 | |
| 887 | def _get_digest_name_and_payload(message): # type: (bytes) -> tuple[str, bytes] |
| 888 | """Returns a digest name and the payload for a response hash. |
| 889 | |
| 890 | If a legacy protocol is detected based on the message length |
| 891 | or contents the digest name returned will be empty to indicate |
| 892 | legacy mode where MD5 and no digest prefix should be sent. |
| 893 | """ |
| 894 | # modern message format: b"{digest}payload" longer than 20 bytes |
| 895 | # legacy message format: 16 or 20 byte b"payload" |
| 896 | if len(message) in _LEGACY_LENGTHS: |
| 897 | # Either this was a legacy server challenge, or we're processing |
| 898 | # a reply from a legacy client that sent an unprefixed 16-byte |
| 899 | # HMAC-MD5 response. All messages using the modern protocol will |
| 900 | # be longer than either of these lengths. |
| 901 | return '', message |
| 902 | if (message.startswith(b'{') and |
| 903 | (curly := message.find(b'}', 1, _MAX_DIGEST_LEN+2)) > 0): |
| 904 | digest = message[1:curly] |
| 905 | if digest in _ALLOWED_DIGESTS: |
| 906 | payload = message[curly+1:] |
| 907 | return digest.decode('ascii'), payload |
| 908 | raise AuthenticationError( |
| 909 | 'unsupported message length, missing digest prefix, ' |
| 910 | f'or unsupported digest: {message=}') |
| 911 | |
| 912 | |
| 913 | def _create_response(authkey, message): |
no test coverage detected
searching dependent graphs…