From charlesreid1

import hmac
import hashlib


def hmac_sha256(key, message):
    """Return the HMAC-SHA256 hex digest."""
    return hmac.new(key, message, hashlib.sha256).hexdigest()


def hmac_sha256_verify(key, message, signature):
    """Constant-time comparison of HMAC-SHA256 digests."""
    expected = hmac.new(key, message, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, signature)


if __name__ == '__main__':
    key = b'shared_secret'
    message = b'This is a test'

    sig = hmac_sha256(key, message)
    print("HMAC-SHA256 was '%s'" % sig)
    print("Verified: %s" % hmac_sha256_verify(key, message, sig))

See also

  • RSA – asymmetric encryption, decryption, and signing



Red Links