Arduino Cryptography Library
Public Member Functions | Protected Member Functions | List of all members
Hash Class Referenceabstract

Abstract base class for cryptographic hash algorithms. More...

#include <Hash.h>

Inheritance diagram for Hash:
BLAKE2b BLAKE2s SHA1 SHA256 SHA3_256 SHA3_512 SHA512 SHA224 SHA384

Public Member Functions

 Hash ()
 Constructs a new hash object.
 
virtual ~Hash ()
 Destroys this hash object. More...
 
virtual size_t hashSize () const =0
 Size of the hash result from finalize(). More...
 
virtual size_t blockSize () const =0
 Size of the internal block used by the hash algorithm. More...
 
virtual void reset ()=0
 Resets the hash ready for a new hashing process. More...
 
virtual void update (const void *data, size_t len)=0
 Updates the hash with more data. More...
 
virtual void finalize (void *hash, size_t len)=0
 Finalizes the hashing process and returns the hash. More...
 
virtual void clear ()=0
 Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing process. More...
 
virtual void resetHMAC (const void *key, size_t keyLen)=0
 Resets the hash ready for a new HMAC hashing process. More...
 
virtual void finalizeHMAC (const void *key, size_t keyLen, void *hash, size_t hashLen)=0
 Finalizes the HMAC hashing process and returns the hash. More...
 

Protected Member Functions

void formatHMACKey (void *block, const void *key, size_t len, uint8_t pad)
 Formats a HMAC key into a block. More...
 

Detailed Description

Abstract base class for cryptographic hash algorithms.

See also
SHA224, SHA256, SHA384, SHA3_256, BLAKE2s

Definition at line 29 of file Hash.h.

Constructor & Destructor Documentation

◆ ~Hash()

Hash::~Hash ( )
virtual

Destroys this hash object.

Note
Subclasses are responsible for clearing any sensitive data that remains in the hash object when it is destroyed.
See also
clear()

Definition at line 48 of file Hash.cpp.

Member Function Documentation

◆ blockSize()

size_t Hash::blockSize ( ) const
pure virtual

Size of the internal block used by the hash algorithm.

See also
update(), hashSize()

Implemented in SHA1, SHA512, SHA3_512, SHA3_256, SHA256, BLAKE2s, and BLAKE2b.

◆ clear()

void Hash::clear ( )
pure virtual

Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing process.

See also
reset()

Implemented in SHA1, SHA512, SHA3_512, SHA3_256, SHA256, BLAKE2s, and BLAKE2b.

◆ finalize()

void Hash::finalize ( void *  hash,
size_t  len 
)
pure virtual

Finalizes the hashing process and returns the hash.

Parameters
hashThe buffer to return the hash value in.
lenThe length of the hash buffer, normally hashSize().

If len is less than hashSize(), then the hash value will be truncated to the first len bytes. If len is greater than hashSize(), then the remaining bytes will left unchanged.

If finalize() is called again, then the returned hash value is undefined. Call reset() first to start a new hashing process.

See also
reset(), update(), finalizeHMAC()

Implemented in SHA1, SHA512, SHA3_512, SHA3_256, SHA256, BLAKE2s, and BLAKE2b.

◆ finalizeHMAC()

void Hash::finalizeHMAC ( const void *  key,
size_t  keyLen,
void *  hash,
size_t  hashLen 
)
pure virtual

Finalizes the HMAC hashing process and returns the hash.

Parameters
keyPoints to the HMAC key for the hashing process. The contents of this array must be identical to the value passed to resetHMAC().
keyLenSize of the HMAC key in bytes.
hashThe buffer to return the hash value in.
hashLenThe length of the hash buffer, normally hashSize().
See also
resetHMAC(), finalize()

Implemented in SHA1, SHA512, SHA3_512, SHA3_256, SHA256, BLAKE2s, and BLAKE2b.

◆ formatHMACKey()

void Hash::formatHMACKey ( void *  block,
const void *  key,
size_t  len,
uint8_t  pad 
)
protected

Formats a HMAC key into a block.

Parameters
blockThe block to format the key into. Must be at least blockSize() bytes in length.
keyPoints to the HMAC key for the hashing process.
lenLength of the HMAC key in bytes.
padInner (0x36) or outer (0x5C) padding value to XOR with the formatted HMAC key.

This function is intended to help subclasses implement resetHMAC() and finalizeHMAC() by directly formatting the HMAC key into the subclass's internal block buffer and resetting the hash.

Definition at line 162 of file Hash.cpp.

◆ hashSize()

size_t Hash::hashSize ( ) const
pure virtual

Size of the hash result from finalize().

See also
finalize(), blockSize()

Implemented in SHA1, SHA512, SHA384, SHA3_512, SHA3_256, SHA256, SHA224, BLAKE2s, and BLAKE2b.

◆ reset()

void Hash::reset ( )
pure virtual

Resets the hash ready for a new hashing process.

See also
update(), finalize(), resetHMAC()

Implemented in SHA1, SHA512, SHA384, SHA3_512, SHA3_256, SHA256, SHA224, BLAKE2s, and BLAKE2b.

◆ resetHMAC()

void Hash::resetHMAC ( const void *  key,
size_t  keyLen 
)
pure virtual

Resets the hash ready for a new HMAC hashing process.

Parameters
keyPoints to the HMAC key for the hashing process.
keyLenSize of the HMAC key in bytes.

The following example computes a HMAC over a series of data blocks with a specific key:

hash.resetHMAC(key, sizeof(key));
hash.update(data1, sizeof(data1));
hash.update(data2, sizeof(data2));
...
hash.update(dataN, sizeof(dataN));
hash.finalizeHMAC(key, sizeof(key), hmac, sizeof(hmac));

The same key must be passed to both resetHMAC() and finalizeHMAC().

See also
finalizeHMAC(), reset()

Implemented in SHA1, SHA512, SHA3_512, SHA3_256, SHA256, BLAKE2s, and BLAKE2b.

◆ update()

void Hash::update ( const void *  data,
size_t  len 
)
pure virtual

Updates the hash with more data.

Parameters
dataData to be hashed.
lenNumber of bytes of data to be hashed.

If finalize() has already been called, then the behavior of update() will be undefined. Call reset() first to start a new hashing process.

See also
reset(), finalize()

Implemented in SHA1, SHA512, SHA3_512, SHA3_256, SHA256, BLAKE2s, and BLAKE2b.


The documentation for this class was generated from the following files: