A fine-tuned implementation of the SHA256 hashing algorithm.

Dependents:   EntropySource Wallet_v1

Embed: (wiki syntax)

« Back to documentation index

SHA256 Class Reference

SHA256 Class Reference

Class to quickly compute SHA-256 hashes. More...

#include <SHA256.h>

Public Member Functions

void reset ()
 Reset this instance so you can calculate a new hash.
void append (const char *data, int size)
 Append data to the hash.
void append (char c)
 Append a single byte.
void append (const char *str)
 Append a zero terminated string.
void append (const std::string &str)
 Append a std::string.
void finalize ()
 Append the required padding and compute the final digest.
const char * digest ()
 Returns a pointer to the 32 bytes of the digest.
std::string binString ()
 Return the digest as a binary std::string.
std::string hexString ()
 Return the digest as a hexadecimal std::string.

Detailed Description

Class to quickly compute SHA-256 hashes.

This class has been heavily optimized for speed at a slight expense of code size.

Example usage:

   SHA256 hash;
   hash.append("The quick brown fox jumps over the lazy dog.");
   hash.finalize();
   std::cout << "Digest: " << hash.hexString() << std::endl;

Definition at line 23 of file SHA256.h.


Member Function Documentation

void append ( const char *  data,
int  size 
)

Append data to the hash.

Note: due to word-allignment optimizations the function may read up to three bytes beyond the end of data.

Parameters:
datathe bytes to be added.
sizethe number of bytes to read from data, but see the note.

Definition at line 33 of file SHA256.cpp.

void append ( const char *  str )

Append a zero terminated string.

The terminating zero itself is not appended.

Parameters:
strthe zero terminated string to be appended.

Definition at line 54 of file SHA256.h.

void append ( const std::string &  str )

Append a std::string.

Parameters:
strThe std::string to be appended.

Definition at line 59 of file SHA256.h.

void append ( char  c )

Append a single byte.

Avoid this function if performance is important.

Parameters:
cthe character to be appended.

Definition at line 47 of file SHA256.h.

std::string binString (  )

Return the digest as a binary std::string.

Avoid this function if performance is important, the std::string constructor will make a copy of the digest internally, doing a malloc and memcpy.

Returns:
a std::string containing the digest as raw binary bits.

Definition at line 93 of file SHA256.h.

const char* digest (  )

Returns a pointer to the 32 bytes of the digest.

Returns:
a pointer to a non-zero-terminated block of 32 bytes containting the digest.

Definition at line 84 of file SHA256.h.

void finalize (  )

Append the required padding and compute the final digest.

Always call this function first before requesting the digest.

After finalization you must call reset() before you can append again.

However, you can do this:

   SHA256 A, AB;
   A.append("First part");
   AB = A;
   A.finalize();
   do_something(A.digest());
   AB.append("Second part");
   AB.finalize();
   do_something(AB.digest());

Definition at line 80 of file SHA256.cpp.

std::string hexString (  )

Return the digest as a hexadecimal std::string.

In addition to the note for binaryString, this function also does conversion.

Returns:
a std::string containing the digest in hexadecimal ascii.

Definition at line 212 of file SHA256.cpp.

void reset (  )

Reset this instance so you can calculate a new hash.

Definition at line 20 of file SHA256.cpp.