Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::Hashing Struct Reference

Compute MD5, SHA1 or SHA256 hash for stream of data. More...

#include <Hashing.h>

Classes

struct  Result
 

Public Types

enum  Type {
  TypeMD5 ,
  TypeSHA1 ,
  TypeSHA256
}
 

Public Member Functions

 Hashing ()
 Initializes an Hashing struct. More...
 
 ~Hashing ()
 Destroys an Hashing struct. More...
 
 Hashing (const Hashing &)=delete
 
 Hashing (Hashing &&)=delete
 
Hashingoperator= (const Hashing &)=delete
 
Hashingoperator= (Hashing &&)=delete
 
bool add (Span< const uint8_t > data)
 Add data to be hashed. More...
 
bool getHash (Result &res)
 Finalize hash computation that has been pushed through Hashing::add. More...
 
bool setType (Type newType)
 Set type of hash to compute. More...
 

Detailed Description

Compute MD5, SHA1 or SHA256 hash for stream of data.


Data can be added until needed with SC::Hashing::add call. SC::Hashing::getHash will generate an actual SC::Hashing::Result holding the computed hash.

Example:

Hashing hash;
SC_TEST_EXPECT(hash.add("test"_a8.toBytesSpan()));
Hashing::Result res;
SC_TEST_EXPECT(hash.getHash(res));
String test;
SC_TEST_EXPECT(StringBuilder(test).appendHex(res.toBytesSpan(), StringBuilder::AppendHexCase::UpperCase));
SC_TEST_EXPECT(test == "098F6BCD4621D373CADE4E832627B4F6"_a8);
report.console.printLine(test.view());
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113
Hashing()
Initializes an Hashing struct.
@ TypeMD5
Compute MD5 hash for the incoming stream of bytes.
Definition: Hashing.h:38

Example with update (for hashing longer streams of data):

Hashing hash;
SC_TEST_EXPECT(hash.add("test"_a8.toBytesSpan()));
SC_TEST_EXPECT(hash.add("test"_a8.toBytesSpan()));
Hashing::Result res;
SC_TEST_EXPECT(hash.getHash(res));
String test;
SC_TEST_EXPECT(StringBuilder(test).appendHex(res.toBytesSpan(), StringBuilder::AppendHexCase::UpperCase));
SC_TEST_EXPECT(test == "05A671C66AEFEA124CC08B76EA6D30BB"_a8);

Member Enumeration Documentation

◆ Type

Enumerator
TypeMD5 

Compute MD5 hash for the incoming stream of bytes.

TypeSHA1 

Compute SHA1 hash for the incoming stream of bytes.

TypeSHA256 

Compute SHA256 hash for the incoming stream of bytes.

Constructor & Destructor Documentation

◆ Hashing()

SC::Hashing::Hashing ( )

Initializes an Hashing struct.

◆ ~Hashing()

SC::Hashing::~Hashing ( )

Destroys an Hashing struct.

Member Function Documentation

◆ add()

bool SC::Hashing::add ( Span< const uint8_t data)

Add data to be hashed.

Can be called multiple times before Hashing::getHash

Parameters
dataData to be hashed
Returns
true if data has been hashed successfully

◆ getHash()

bool SC::Hashing::getHash ( Result res)

Finalize hash computation that has been pushed through Hashing::add.

Parameters
[out]resResult object holding the actual Result::hash
Returns
true if the final hash has been computed successfully

◆ setType()

bool SC::Hashing::setType ( Type  newType)

Set type of hash to compute.

Parameters
newTypeMD5, SHA1, SHA256
Returns
true if the hash type has been changed successfully

The documentation for this struct was generated from the following file: