ndn::util::Sha256 Class Reference

Provides stateful SHA-256 digest calculation. More...

#include <ndn-cxx/util/sha256.hpp>

Classes

class  Error
 

Public Member Functions

 Sha256 ()
 Create an empty SHA-256 digest. More...
 
 Sha256 (std::istream &is)
 Calculate SHA-256 digest of the input stream is. More...
 
ConstBufferPtr computeDigest ()
 Finalize and return the digest based on all previously supplied inputs. More...
 
bool empty () const
 Check if digest is empty. More...
 
bool operator!= (Sha256 &digest)
 Check if the supplied digest is not equal to this digest. More...
 
Sha256operator<< (Sha256 &src)
 Add existing digest to the digest calculation. More...
 
Sha256operator<< (span< const uint8_t > bytes)
 Add a contiguous range of arbitrary bytes to the digest calculation. More...
 
Sha256operator<< (std::string_view str)
 Add a string to the digest calculation. More...
 
Sha256operator<< (uint64_t value)
 Add a uint64_t value to the digest calculation. More...
 
bool operator== (Sha256 &digest)
 Check if the supplied digest is equal to this digest. More...
 
void reset ()
 Discard the current state and start a new digest calculation. More...
 
std::string toString ()
 Convert digest to std::string. More...
 
void update (span< const uint8_t > buffer)
 Add a byte buffer to the digest calculation. More...
 

Static Public Member Functions

static ConstBufferPtr computeDigest (span< const uint8_t > buffer)
 Stateless SHA-256 digest calculation. More...
 

Static Public Attributes

static constexpr size_t DIGEST_SIZE = 32
 Length in bytes of a SHA-256 digest. More...
 

Detailed Description

Provides stateful SHA-256 digest calculation.

Example:

Sha256 digest;
digest.update(buf1, size1);
digest.update(buf2, size2);
...
ConstBufferPtr result = digest.computeDigest();
Sha256()
Create an empty SHA-256 digest.
Definition: sha256.cpp:32

Definition at line 42 of file sha256.hpp.

Constructor & Destructor Documentation

◆ Sha256() [1/2]

ndn::util::Sha256::Sha256 ( )

Create an empty SHA-256 digest.

Definition at line 32 of file sha256.cpp.

◆ Sha256() [2/2]

ndn::util::Sha256::Sha256 ( std::istream &  is)
explicit

Calculate SHA-256 digest of the input stream is.

Definition at line 37 of file sha256.cpp.

Member Function Documentation

◆ computeDigest() [1/2]

ConstBufferPtr ndn::util::Sha256::computeDigest ( )

Finalize and return the digest based on all previously supplied inputs.

Definition at line 61 of file sha256.cpp.

◆ computeDigest() [2/2]

ConstBufferPtr ndn::util::Sha256::computeDigest ( span< const uint8_t >  buffer)
static

Stateless SHA-256 digest calculation.

Returns
SHA-256 digest of the input buffer

Definition at line 133 of file sha256.cpp.

◆ empty()

bool ndn::util::Sha256::empty ( ) const
inline

Check if digest is empty.

An empty digest means nothing has been taken into calculation.

Definition at line 73 of file sha256.hpp.

◆ operator!=()

bool ndn::util::Sha256::operator!= ( Sha256 digest)
inline

Check if the supplied digest is not equal to this digest.

Note
This method invokes computeDigest() on both operands, finalizing the digest.

Definition at line 102 of file sha256.hpp.

◆ operator<<() [1/4]

Sha256 & ndn::util::Sha256::operator<< ( Sha256 src)

Add existing digest to the digest calculation.

Parameters
srcdigest to combine with

The result of this combination is sha256(sha256(...))

Note
This method invokes computeDigest() on src, finalizing the digest.
Exceptions
Errorthe digest has already been finalized

Definition at line 86 of file sha256.cpp.

◆ operator<<() [2/4]

Sha256 & ndn::util::Sha256::operator<< ( span< const uint8_t >  bytes)

Add a contiguous range of arbitrary bytes to the digest calculation.

Exceptions
Errorthe digest has already been finalized

Definition at line 107 of file sha256.cpp.

◆ operator<<() [3/4]

Sha256 & ndn::util::Sha256::operator<< ( std::string_view  str)

Add a string to the digest calculation.

Exceptions
Errorthe digest has already been finalized

Definition at line 93 of file sha256.cpp.

◆ operator<<() [4/4]

Sha256 & ndn::util::Sha256::operator<< ( uint64_t  value)

Add a uint64_t value to the digest calculation.

Exceptions
Errorthe digest has already been finalized

Definition at line 100 of file sha256.cpp.

◆ operator==()

bool ndn::util::Sha256::operator== ( Sha256 digest)

Check if the supplied digest is equal to this digest.

Note
This method invokes computeDigest() on both operands, finalizing the digest.

Definition at line 73 of file sha256.cpp.

◆ reset()

void ndn::util::Sha256::reset ( )

Discard the current state and start a new digest calculation.

Definition at line 48 of file sha256.cpp.

◆ toString()

std::string ndn::util::Sha256::toString ( )

Convert digest to std::string.

Note
This method invokes computeDigest(), finalizing the digest.

Definition at line 126 of file sha256.cpp.

◆ update()

void ndn::util::Sha256::update ( span< const uint8_t >  buffer)

Add a byte buffer to the digest calculation.

Exceptions
Errorthe digest has already been finalized

Definition at line 115 of file sha256.cpp.

Member Data Documentation

◆ DIGEST_SIZE

constexpr size_t ndn::util::Sha256::DIGEST_SIZE = 32
staticconstexpr

Length in bytes of a SHA-256 digest.

Definition at line 54 of file sha256.hpp.