Python Glossary Русский

Python 3.7+hmacfunction

hmac.digest()

message authentication

Computes the HMAC of a message in one call and returns it as bytes — a fast replacement for hmac.new(...).digest().

Syntax

hmac.digest(key, msg, digest)

It is faster than hmac.new() when the whole message is in memory: with the algorithm given as a name, OpenSSL does the work. The third parameter is called digest and is required. It returns bytes, not a hex string: call .hex() for text. For data that arrives in pieces you need hmac.new() with update().

Examples

import hmac
fast = hmac.digest(b'key', b'message', 'sha256')
slow = hmac.new(b'key', b'message', 'sha256').digest()
print(fast == slow, type(fast).__name__, len(fast))  # → True bytes 32

See also

Open in the full glossarydocs.python.orgReport an inaccuracy