>>> from tomcrypt import mac
This module contains a MAC class, and a convenience function for every MAC provided:
>>> mymac = mac.MAC('hmac', 'sha256', b'secret')
>>> # OR
>>> mymac = mac.hmac('sha256', b'secret')
The module also contains a list of the names of all MACS provided, and lists of those which use ciphers or hashes:
>>> sorted(mac.names)
['hmac', 'omac', 'pmac', 'xcbc']
>>> sorted(mac.hash_macs)
['hmac']
>>> sorted(mac.cipher_macs)
['omac', 'pmac', 'xcbc']
The MAC will accept either a name or a Descriptor to specify which hash/cipher algorithm to use:
>>> mac.hmac('md5', b'secret', b'content').hexdigest()
'97e5f3684213a40aaaa9ef31f9f4b1a7'
>>> mac.hmac(hash.md5, b'secret', b'content').hexdigest()
'97e5f3684213a40aaaa9ef31f9f4b1a7'
>>> mac.pmac('aes', b'0123456789abcdef', b'content').hexdigest()
'530566cd1c33e874f503b3c3272d0fd4'
>>> mac.pmac(cipher.aes, b'0123456789abcdef', b'content').hexdigest()
'530566cd1c33e874f503b3c3272d0fd4'
The rest of the API is similar to the stdlib hmac (or hashlib or tomcrypt.hash, etc.), offering update, digest, hexdigest, and copy methods.
Calculator of keyed hashes.
>>> mac = MAC('hmac', 'md5', b'secret') # or hmac('md5', b'secret')
>>> mac = MAC('omac', 'aes', b'0123456789abcdef') # or omac(...)
>>> mac = MAC('pmac', 'aes', b'0123456789abcdef') # or pmac(...)
>>> mac = MAC('xcbc', 'aes', b'0123456789abcdef') # or xcbc(...)
Get a copy of the mac state.
>>> a = hmac('md5', b'secret', b'message')
>>> b = a.copy()
>>> b.update(b'some more')
>>> b.hexdigest()
'e0cdc5e1d7af04f800b0e0f0ceee588a'
>>> a.hexdigest()
'7e0d0767775312154ba16fd3af9771a2'
Return binary digest.
>>> mac = hmac('md5', b'secret', b'message')
>>> mac.digest()
b'~\r\x07gwS\x12\x15K\xa1o\xd3\xaf\x97q\xa2'
Return hex-encoded string of digest.
>>> mac = hmac('md5', b'secret', b'message')
>>> mac.hexdigest()
'7e0d0767775312154ba16fd3af9771a2'
Add more data to the mac.
>>> mac = hmac('md5', b'secret')
>>> mac.update(b'message')
>>> mac.hexdigest()
'7e0d0767775312154ba16fd3af9771a2'
Run internal libtomcrypt mac tests.
>>> test_library()
True