CryptoSys PKI  23.0.0
Public Types | Static Public Member Functions | List of all members
dipki::Hash Class Reference

Message digest hash functions. More...

Public Types

enum class  Alg
 Hash algorithm More...
 
enum class  FileMode
 File mode. More...
 

Static Public Member Functions

static std::string AlgName (Hash::Alg alg)
 Get the algorithm name. More...
 
static bvec_t Bytes (const bvec_t &data, Alg alg=Alg::Sha1)
 Compute hash digest over byte array. More...
 
static bvec_t Double (const bvec_t &data, Alg alg=Alg::Sha1)
 Create double hash (hash of hash). More...
 
static bvec_t File (const std::string &fileName, Alg alg=Alg::Sha1, FileMode mode=FileMode::Binary)
 Compute hash digest over a binary file. More...
 
static Hash::Alg HashAlgFromString (const std::string &hashStr)
 Get hash algorithm from a string. More...
 
static std::string HexFromBytes (const bvec_t &data, Alg alg=Alg::Sha1)
 Compute hash digest in hex format over byte array. More...
 
static std::string HexFromFile (const std::string &fileName, Alg alg=Alg::Sha1, FileMode mode=FileMode::Binary)
 Compute hash digest in hex format over a file. More...
 
static std::string HexFromHex (const std::string &dataHex, Alg alg=Alg::Sha1)
 Compute hash digest in hex format over hex-encoded input. More...
 
static std::string HexFromString (const std::string &s, Alg alg=Alg::Sha1)
 Compute hash digest in hex format over string input. More...
 
static int Length (Alg alg)
 Return length of message digest output in bytes. More...
 

Detailed Description

Message digest hash functions.

Member Enumeration Documentation

◆ Alg

enum dipki::Hash::Alg
strong

Hash algorithm

Enumerator
Sha1 

SHA-1 [default].

Md5 

MD5 [legacy, not recommended].

Md2 

MD2 [legacy, definitely not recommended].

Sha256 

SHA-256 [minimum recommended strength].

Sha384 

SHA-384.

Sha512 

SHA-512.

Sha224 

SHA-224.

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.

Btc160 

Bitcoin160, RIPEMD160(SHA256(m))

◆ FileMode

enum dipki::Hash::FileMode
strong

File mode.

Enumerator
Binary 

Binary mode [default].

Text 

Text mode: treat CR-LF (0x13, 0x10) pairs as a single LF (0x10)

Member Function Documentation

◆ AlgName()

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

Get the algorithm name.

Parameters
algHash algorithm
Returns
Algorithm name as a string, e.g. "sha512".
// sha512
static std::string AlgName(Hash::Alg alg)
Get the algorithm name.
@ Sha512
SHA-512.

◆ Bytes()

static bvec_t dipki::Hash::Bytes ( const bvec_t data,
Alg  alg = Alg::Sha1 
)
static

Compute hash digest over byte array.

Parameters
dataMessage data in byte array
algDigest algorithm to be used (default = SHA-1)
Returns
Message digest in byte array.

◆ Double()

static bvec_t dipki::Hash::Double ( const bvec_t data,
Alg  alg = Alg::Sha1 
)
static

Create double hash (hash of hash).

Parameters
dataMessage data in byte array
algDigest algorithm to be used (default = SHA-1)
Returns
Message digest HASH(HASH(m)) in byte array.

◆ File()

static bvec_t dipki::Hash::File ( const std::string &  fileName,
Alg  alg = Alg::Sha1,
FileMode  mode = FileMode::Binary 
)
static

Compute hash digest over a binary file.

Parameters
fileNameName of file containing message data
algDigest algorithm to be used (default = SHA-1)
modeFile mode (default = binary)
Returns
Message digest in byte array.

◆ HashAlgFromString()

static Hash::Alg dipki::Hash::HashAlgFromString ( const std::string &  hashStr)
static

Get hash algorithm from a string.

Parameters
hashStrString containing name of hash algorithm, e.g. "sha1" or "SHA-1".
Returns
Hash algorithm enumerator.
Remarks
Case-insensitive; spaces, punctuation and other non-alphanumerical characters are ignored.
cout << "AlgName=" << dipki::Hash::AlgName(dipki::Hash::HashAlgFromString("! SHa-256;")) << endl;
// AlgName=sha256
static Hash::Alg HashAlgFromString(const std::string &hashStr)
Get hash algorithm from a string.

◆ HexFromBytes()

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

Compute hash digest in hex format over byte array.

Parameters
dataMessage data in byte array
algDigest algorithm to be used (default = SHA-1)
Returns
Message digest in hex-encoded format.

◆ HexFromFile()

static std::string dipki::Hash::HexFromFile ( const std::string &  fileName,
Alg  alg = Alg::Sha1,
FileMode  mode = FileMode::Binary 
)
static

Compute hash digest in hex format over a file.

Parameters
fileNameName of file containing message data
algDigest algorithm to be used (default = SHA-1)
modeFile mode (default = binary)
Returns
Message digest in hex-encoded format.

◆ HexFromHex()

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

Compute hash digest in hex format over hex-encoded input.

Parameters
dataHexMessage data in hex-encoded format
algDigest algorithm to be used (default = SHA-1)
Returns
Message digest in hex-encoded format.
cout << dipki::Hash::HexFromHex("616263") << endl;
// a9993e364706816aba3e25717850c26c9cd0d89d
static std::string HexFromHex(const std::string &dataHex, Alg alg=Alg::Sha1)
Compute hash digest in hex format over hex-encoded input.

◆ HexFromString()

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

Compute hash digest in hex format over string input.

Parameters
sMessage data string
algDigest algorithm to be used (default = SHA-1)
Returns
Message digest in hex-encoded format.
// ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
static std::string HexFromString(const std::string &s, Alg alg=Alg::Sha1)
Compute hash digest in hex format over string input.
@ Sha256
SHA-256 [minimum recommended strength].

◆ Length()

static int dipki::Hash::Length ( Alg  alg)
static

Return length of message digest output in bytes.

Parameters
algDigest algorithm.
Returns
Length of the hash function output in bytes.
cout << "Hash::Length(Sha256)=" << dipki::Hash::Length(dipki::Hash::Alg::Sha256) << endl;
// 32
static int Length(Alg alg)
Return length of message digest output in bytes.
Copyright © 2004-24 D.I. Management Services Pty Limited t/a CryptoSys ABN 78 083 210 584 Australia. All rights reserved. <www.di-mgt.com.au> <www.cryptosys.net>. Generated on Mon Sep 23 2024 15:37:33 by Doxygen 1.9.1.