NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
ndn::util::Sha256 Class Reference

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

#include <sha256.hpp>

Collaboration diagram for ndn::util::Sha256:

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...
 
bool empty () const
 Check if digest is empty. More...
 
void reset ()
 Discard the current state and start a new digest calculation. More...
 
ConstBufferPtr computeDigest ()
 Finalize and return the digest based on all previously supplied inputs. More...
 
bool operator== (Sha256 &digest)
 Check if the supplied digest is equal to this digest. 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<< (const std::string &str)
 Add a string to the digest calculation. More...
 
Sha256operator<< (const Block &block)
 Add a block to the digest calculation. More...
 
Sha256operator<< (uint64_t value)
 Add a uint64_t value to the digest calculation. More...
 
void update (const uint8_t *buffer, size_t size)
 Add a raw buffer to the digest calculation. More...
 
std::string toString ()
 Convert digest to std::string. More...
 

Static Public Member Functions

static ConstBufferPtr computeDigest (const uint8_t *buffer, size_t size)
 Stateless SHA-256 digest calculation. More...
 

Static Public Attributes

static const 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();

Definition at line 44 of file sha256.hpp.

Constructor & Destructor Documentation

◆ Sha256() [1/2]

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

Create an empty SHA-256 digest.

Definition at line 34 of file sha256.cpp.

References reset().

◆ Sha256() [2/2]

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

Calculate SHA-256 digest of the input stream is.

Definition at line 39 of file sha256.cpp.

References ndn::security::transform::digestFilter(), ndn::SHA256, and ndn::security::transform::streamSink().

Member Function Documentation

◆ 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 75 of file sha256.hpp.

◆ reset()

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

Discard the current state and start a new digest calculation.

Definition at line 50 of file sha256.cpp.

References ndn::security::transform::digestFilter(), ndn::SHA256, and ndn::security::transform::streamSink().

Referenced by Sha256().

◆ computeDigest() [1/2]

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

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

Definition at line 63 of file sha256.cpp.

Referenced by computeDigest(), ndn::Data::getFullName(), operator<<(), ndn::util::operator<<(), operator==(), and toString().

◆ 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 75 of file sha256.cpp.

References computeDigest().

◆ 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 104 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 89 of file sha256.cpp.

References computeDigest(), and update().

◆ operator<<() [2/4]

Sha256 & ndn::util::Sha256::operator<< ( const std::string &  str)

Add a string to the digest calculation.

Exceptions
Errorthe digest has already been finalized

Definition at line 97 of file sha256.cpp.

References update().

◆ operator<<() [3/4]

Sha256 & ndn::util::Sha256::operator<< ( const Block block)

Add a block to the digest calculation.

Exceptions
Errorthe digest has already been finalized

Definition at line 104 of file sha256.cpp.

References ndn::Block::size(), update(), and ndn::Block::wire().

◆ 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 111 of file sha256.cpp.

References update().

◆ update()

void ndn::util::Sha256::update ( const uint8_t *  buffer,
size_t  size 
)

Add a raw buffer to the digest calculation.

Parameters
bufferthe input buffer
sizethe size of the input buffer
Exceptions
Errorthe digest has already been finalized

Definition at line 118 of file sha256.cpp.

Referenced by computeDigest(), and operator<<().

◆ toString()

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

Convert digest to std::string.

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

Definition at line 129 of file sha256.cpp.

References computeDigest(), and ndn::toHex().

◆ computeDigest() [2/2]

ConstBufferPtr ndn::util::Sha256::computeDigest ( const uint8_t *  buffer,
size_t  size 
)
static

Stateless SHA-256 digest calculation.

Parameters
bufferthe input buffer
sizethe size of the input buffer
Returns
SHA-256 digest of the input buffer

Definition at line 136 of file sha256.cpp.

References computeDigest(), and update().

Member Data Documentation

◆ DIGEST_SIZE

const size_t ndn::util::Sha256::DIGEST_SIZE = 32
static

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