CryptoSys API  6.22.1
Public Types | Public Member Functions | Static Public Member Functions | List of all members
crsysapi::Hash Class Reference

Message digest hash functions. More...

Public Types

enum class  Alg
 Hash algorithm. More...
 

Public Member Functions

int AddData (bvec_t data)
 Adds a chunk of data in a byte array to be digested by the Hash object. More...
 
int AddData (std::string s)
 Adds a chunk of data in a string to be digested by the Hash object. More...
 
bvec_t Final ()
 Computes final message digest for the Hash object. More...
 
bool Init (Alg alg)
 Initialises the Hash object ready for repeated incremental operations. More...
 

Static Public Member Functions

static std::string AlgName (Alg alg)
 Get the algorithm name as a string. More...
 
static bvec_t BytesFromBytes (const bvec_t &data, Alg alg=Alg::Sha1)
 Compute hash value in byte format of byte input. More...
 
static bvec_t BytesFromFile (const std::string &fileName, Alg alg=Alg::Sha1)
 Compute hash value in byte format of a binary file. More...
 
static std::string HexFromBits (const bvec_t &data, int dataBitLen, Alg alg=Alg::Sha1)
 Compute hash value in hex format from bit-oriented input. More...
 
static std::string HexFromBytes (const bvec_t &data, Alg alg=Alg::Sha1)
 Computes hash value in hex format of byte input. More...
 
static std::string HexFromFile (const std::string &fileName, Alg alg=Alg::Sha1)
 Compute hash value in hex format of a binary file. More...
 
static std::string HexFromHex (const std::string &dataHex, Alg alg=Alg::Sha1)
 Compute hash value in hex format of hex-encoded input. More...
 
static std::string HexFromString (const std::string &s, Alg alg=Alg::Sha1)
 Compute hash value in hex format of string input. More...
 
static std::string HexFromTextFile (const std::string &fileName, Alg alg=Alg::Sha1)
 Compute hash value in hex format of a text file, treating CR-LF pairs as a single LF. More...
 
static int LengthInBytes (Alg alg)
 Get the number of bytes in the message digest for the given hash algorithm. More...
 

Detailed Description

Message digest hash functions.

Member Enumeration Documentation

◆ Alg

enum crsysapi::Hash::Alg
strong

Hash algorithm.

Enumerator
Sha1 

SHA-1 (as per FIPS PUB 180-2)

Sha224 

SHA-224 (as per FIPS PUB 180-4)

Sha256 

SHA-256 (as per FIPS PUB 180-4)

Sha384 

SHA-384 (as per FIPS PUB 180-4)

Sha512 

SHA-512 (as per FIPS PUB 180-4)

Sha3_224 

SHA-3-224 (as per FIPS PUB 202)

Sha3_256 

SHA-3-256 (as per FIPS PUB 202)

Sha3_384 

SHA-3-384 (as per FIPS PUB 202)

Sha3_512 

SHA-3-512 (as per FIPS PUB 202)

Rmd160 

RIPEMD-160

Md5 

MD5 (as per RFC 1321)

Md2 

MD2 (as per RFC 1319) [legacy].

Ascon_Hash 

ASCON-HASH.

Ascon_HashA 

ASCON-HASHA.

Member Function Documentation

◆ AddData() [1/2]

int crsysapi::Hash::AddData ( bvec_t  data)

Adds a chunk of data in a byte array to be digested by the Hash object.

Parameters
dataData to add
Returns
0 on success or nonzero, see Gen::ErrorLookup
Exceptions
std::runtime_errorif object is not valid.

◆ AddData() [2/2]

int crsysapi::Hash::AddData ( std::string  s)

Adds a chunk of data in a string to be digested by the Hash object.

Parameters
sData to add
Returns
0 on success or nonzero, see Gen::ErrorLookup
Exceptions
std::runtime_errorif object is not valid.

◆ AlgName()

static std::string crsysapi::Hash::AlgName ( Alg  alg)
static

Get the algorithm name as a string.

Parameters
algHash algorithm
Returns
Name of algorithm.
Remarks
For example
// Length(SHA-256)=32
static std::string AlgName(Alg alg)
Get the algorithm name as a string.
@ Sha256
SHA-256 (as per FIPS PUB 180-4)
static int LengthInBytes(Alg alg)
Get the number of bytes in the message digest for the given hash algorithm.

◆ BytesFromBytes()

static bvec_t crsysapi::Hash::BytesFromBytes ( const bvec_t data,
Alg  alg = Alg::Sha1 
)
static

Compute hash value in byte format of byte input.

Parameters
dataMessage data in byte format
algHash algorithm to be used
Returns
Message digest in byte format

◆ BytesFromFile()

static bvec_t crsysapi::Hash::BytesFromFile ( const std::string &  fileName,
Alg  alg = Alg::Sha1 
)
static

Compute hash value in byte format of a binary file.

Parameters
fileNameName of file containing message data
algHash algorithm to be used (ASCON is not supported in file mode)
Returns
Message digest in byte format
Exceptions
std::runtime_errorif file does not exist or cannot be opened.

◆ Final()

bvec_t crsysapi::Hash::Final ( )

Computes final message digest for the Hash object.

Returns
Digest value in a byte array.
Remarks
This operation delivers the final result of Add operations since the Hash::Init operation. Once called, the Hash object is no longer valid. Use Hash::Init to start again.
Exceptions
std::runtime_errorif object is not valid.

◆ HexFromBits()

static std::string crsysapi::Hash::HexFromBits ( const bvec_t data,
int  dataBitLen,
Alg  alg = Alg::Sha1 
)
static

Compute hash value in hex format from bit-oriented input.

Parameters
dataBit-oriented message data in byte array
dataBitLenNumber of bits in data to digest
algHash algorithm to be used
Returns
Message digest in hex-encoded format
Remarks
Pass a bitstring as an array of bytes in big-endian order with the most-significant bit first. The bitstring will be truncated to the number of bits specified in dataBitLen and extraneous bits on the right will be ignored. Only the SHA family of hash functions (SHA-1, SHA-2, SHA-3) is supported in bit-oriented mode.
Exceptions
std::runtime_errorif algorithm is not supported or if data is too short for the required number of bits.

◆ HexFromBytes()

static std::string crsysapi::Hash::HexFromBytes ( const bvec_t data,
Alg  alg = Alg::Sha1 
)
static

Computes hash value in hex format of byte input.

Parameters
dataMessage data in byte format
algHash algorithm to be used
Returns
Message digest in hex-encoded format

◆ HexFromFile()

static std::string crsysapi::Hash::HexFromFile ( const std::string &  fileName,
Alg  alg = Alg::Sha1 
)
static

Compute hash value in hex format of a binary file.

Parameters
fileNameName of file containing message data
algHash algorithm to be used
Returns
Message digest in hex-encoded format
Exceptions
std::runtime_errorif file does not exist or cannot be opened.

◆ HexFromHex()

static std::string crsysapi::Hash::HexFromHex ( const std::string &  dataHex,
Alg  alg = Alg::Sha1 
)
static

Compute hash value in hex format of hex-encoded input.

Parameters
dataHexMessage data in hex-encoded format
algHash algorithm to be used (ASCON is not supported in file mode)
Returns
Message digest in hex-encoded format

◆ HexFromString()

static std::string crsysapi::Hash::HexFromString ( const std::string &  s,
Alg  alg = Alg::Sha1 
)
static

Compute hash value in hex format of string input.

Parameters
sMessage data string
algHash algorithm to be used
Returns
Message digest in hex-encoded format

◆ HexFromTextFile()

static std::string crsysapi::Hash::HexFromTextFile ( const std::string &  fileName,
Alg  alg = Alg::Sha1 
)
static

Compute hash value in hex format of a text file, treating CR-LF pairs as a single LF.

Parameters
fileNameName of file containing message data
algHash algorithm to be used
Returns
Message digest in hex format
Remarks
Use for passing message digest of file between Unix and Windows systems.
Exceptions
std::runtime_errorif file does not exist or cannot be opened.

◆ Init()

bool crsysapi::Hash::Init ( Alg  alg)

Initialises the Hash object ready for repeated incremental operations.

Parameters
algHash algorithm to be used
Returns
True if successful.
Remarks
Only the SHA families of hash functions (SHA-1, SHA-2, SHA-3) are supported in object mode.
Exceptions
std::runtime_errorif algorithm is not supported.

◆ LengthInBytes()

static int crsysapi::Hash::LengthInBytes ( Alg  alg)
static

Get the number of bytes in the message digest for the given hash algorithm.

Parameters
algHash algorithm
Returns
The length in bytes of the message digest output.
Copyright © 2001-24 D.I. Management Services Pty Limited ABN 78 083 210 584 Australia. All rights reserved. <www.di-mgt.com.au> <www.cryptosys.net>. Generated on Sat Jan 6 2024 18:00:07 by Doxygen 1.9.1.