A fine-tuned implementation of the SHA256 hashing algorithm.
Dependents: EntropySource Wallet_v1
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:
-
data the bytes to be added. size the number of bytes to read from data, but see the note.
Definition at line 33 of file SHA256.cpp.
void append | ( | const char * | str ) |
void append | ( | const std::string & | str ) |
void append | ( | char | c ) |
std::string binString | ( | ) |
const char* digest | ( | ) |
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.
Generated on Fri Jul 15 2022 02:57:12 by 1.7.2