Hash a bytes-like (buffer-compatible) object. This function returns a good quality hash but is not cryptographically secure. The fastest available algorithm is selected. A fixed-length bytes object is returned.
(buf, hasher=None)
| 71 | |
| 72 | |
| 73 | def hash_buffer(buf, hasher=None): |
| 74 | """ |
| 75 | Hash a bytes-like (buffer-compatible) object. This function returns |
| 76 | a good quality hash but is not cryptographically secure. The fastest |
| 77 | available algorithm is selected. A fixed-length bytes object is returned. |
| 78 | """ |
| 79 | if hasher is not None: |
| 80 | try: |
| 81 | return hasher(buf) |
| 82 | except (TypeError, OverflowError): |
| 83 | # Some hash libraries may have overly-strict type checking, |
| 84 | # not accepting all buffers |
| 85 | pass |
| 86 | for hasher in hashers: |
| 87 | try: |
| 88 | return hasher(buf) |
| 89 | except (TypeError, OverflowError): |
| 90 | pass |
| 91 | raise TypeError(f"unsupported type for hashing: {type(buf)}") |
| 92 | |
| 93 | |
| 94 | def hash_buffer_hex(buf, hasher=None): |
no outgoing calls