>>> from tomcrypt import hash
This module contains a Descriptor class which describes a hash, and a Hash class for using a hash. As a convenience there is a pre-made Descriptor for every hash provided:
>>> hasher = hash.Hash('sha256')
>>> # OR:
>>> hasher = hash.sha256()
The module also contains a list of the names of all hashes provided:
>>> sorted(hash.names)
['chc', 'md2', 'md4', 'md5', 'rmd128', 'rmd160', 'rmd256', 'rmd320', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'tiger', 'whirlpool']
The module has been designed to be a drop-in replacement for hashlib (new() is simply another name for Hash):
>>> hasher = hash.new('sha256')
You can also provide initial content:
>>> hasher = hash.Hash('sha256', b'intial')
>>> # OR:
>>> hasher = hash.new('sha256', b'intial')
>>> # OR:
>>> hasher = hash.sha256(b'intial')
The rest of the API is the same as hashlib as well:
>>> # Digests!
>>> hasher = hash.sha256(b'initial')
>>> hasher.hexdigest()
'ac1b5c0961a7269b6a053ee64276ed0e20a7f48aefb9f67519539d23aaf10149'
>>> # Copies!
>>> copy = hasher.copy()
>>> hasher.update(b'something')
>>> copy.hexdigest()
'ac1b5c0961a7269b6a053ee64276ed0e20a7f48aefb9f67519539d23aaf10149'
>>> # Binary output!
>>> hasher.hexdigest()
'2e917b6429310675c7b8020885cbce99f64482a36dba5ee323e9891b8afe1545'
>>> hasher.digest()
b'.\x91{d)1\x06u\xc7\xb8\x02\x08\x85\xcb\xce\x99\xf6D\x82\xa3m\xba^\xe3#\xe9\x89\x1b\x8a\xfe\x15E'
LibTomCrypt descriptor of a hash function.
Can be called as convenience to calling Hash, passing the hash name via kwargs.
>>> md5 = Descriptor('md5') # Same as tomcrypt.hash.md5.
Internal block size of this hash, in bytes.
>>> md5.block_size
64
Return digest for a single string.
This is a convenience for constructing a Hash object and calling hexdigest on it.
>>> md5.digest(b'message')
b'x\xe71\x02}\x8f\xd5\x0e\xd6B4\x0b|\x9ac\xb3'
Size of final digest, in bytes.
>>> md5.digest_size
16
Return hexdigest for a single string.
This is a convenience for constructing a Hash object and calling hexdigest on it.
>>> md5.hexdigest(b'message')
'78e731027d8fd50ed642340b7c9a63b3'
Name of this hash.
>>> md5.name
'md5'
All state required to digest messages with a given hash function.
The API of this class has been designed to be a drop-in replacement for the standard library’s hashlib.
For CHC hashes see CHC class.
>>> hash = Hash('md5', b'message')
Get a copy of the hash state.
>>> a = md5(b'message')
>>> b = a.copy()
>>> b.update(b'some more')
>>> b.hexdigest()
'd62b5837649450564c7fc65d1ab2ef85'
>>> a.hexdigest()
'78e731027d8fd50ed642340b7c9a63b3'
Return binary digest.
>>> hash = md5(b'message')
>>> hash.digest()
b'x\xe71\x02}\x8f\xd5\x0e\xd6B4\x0b|\x9ac\xb3'
Return hex-encoded string of digest.
>>> hash = md5(b'message')
>>> hash.hexdigest()
'78e731027d8fd50ed642340b7c9a63b3'
Add more data to the digest.
>>> hash = md5()
>>> hash.update(b'message')
>>> hash.hexdigest()
'78e731027d8fd50ed642340b7c9a63b3'