Message Digests

A message digest is the output of a cryptographic hash function containing a string of bytes created by a one-way formula using the original message as input.

Message digests are designed to protect the integrity of a piece of data or media to detect changes and alterations to any part of a message.

Hashing Classes

Interface

All Hashing Functions available in this module implements the following interface:

class wolfcrypt.hashes._Hash(string=None)[source]

A PEP 247: Cryptographic Hash Functions compliant Hash Function Interface.

classmethod new(string=None)[source]

Creates a new hashing object and returns it. The optional string parameter, if supplied, will be immediately hashed into the object’s starting state, as if obj.update(string) was called.

copy()[source]

Returns a separate copy of this hashing object. An update to this copy won’t affect the original object.

update(string)[source]

Hashes string into the current state of the hashing object. update() can be called any number of times during a hashing object’s lifetime.

digest()[source]

Returns the hash value of this hashing object as a string containing 8-bit data. The object is not altered in any way by this function; you can continue updating the object after calling this function.

hexdigest()[source]

Returns the hash value of this hashing object as a string containing hexadecimal digits. Lowercase letters are used for the digits ‘a’ through ‘f’. Like the .digest() method, this method doesn’t alter the object.

SHA-1

Attention

NIST has deprecated SHA-1 in favor of the SHA-2 variants. New applications are strongly suggested to use SHA-2 over SHA-1.

class wolfcrypt.hashes.Sha(string=None)[source]

SHA-1 is a cryptographic hash function standardized by NIST.

It produces an [ 160-bit | 20 bytes ] message digest.

SHA-2 family

class wolfcrypt.hashes.Sha256(string=None)[source]

SHA-256 is a cryptographic hash function from the SHA-2 family and is standardized by NIST.

It produces a [ 256-bit | 32 bytes ] message digest.

class wolfcrypt.hashes.Sha384(string=None)[source]

SHA-384 is a cryptographic hash function from the SHA-2 family and is standardized by NIST.

It produces a [ 384-bit | 48 bytes ] message digest.

class wolfcrypt.hashes.Sha512(string=None)[source]

SHA-512 is a cryptographic hash function from the SHA-2 family and is standardized by NIST.

It produces a [ 512-bit | 64 bytes ] message digest.

Example

>>> from wolfcrypt.hashes import Sha256
>>>
>>> s = Sha256()
>>> s.update(b'wolf')
>>> s.update(b'crypt')
>>> s.digest()
b'\x96\xe0.{\x1c\xbc\xd6\xf1\x04\xfe\x1f\xdbFR\x02zU\x05\xb6\x86R\xb7\x00\x95\xc61\x8f\x9d\xce\r\x18D'
>>> s.hexdigest()
b'96e02e7b1cbcd6f104fe1fdb4652027a5505b68652b70095c6318f9dce0d1844'
>>>
>>> s.update(b'rocks')
>>> s.hexdigest()
b'e1a50df419d65715c48316bdc6a6f7f0485f4b26c1b107228faf17988b61c83f'
>>>
>>> Sha256(b'wolfcryptrocks').hexdigest()
b'e1a50df419d65715c48316bdc6a6f7f0485f4b26c1b107228faf17988b61c83f'
>>>
>>> Sha256.new(b'wolfcryptrocks').hexdigest()
b'e1a50df419d65715c48316bdc6a6f7f0485f4b26c1b107228faf17988b61c83f'