scclib
Stable Cloud Computing C++ Library
Public Types | Public Member Functions | Static Public Member Functions | List of all members
scc::crypto::Hash Class Reference

General one-way hashing algorithms. More...

#include <hash.h>

Public Types

enum  Algorithm {
  md5_type =1001 , sha1_type =1002 , sha224_type =1003 , sha256_type =1004 ,
  sha384_type =1005 , sha512_type =1006 , sha512_224_type =1007 , sha512_256_type =1008 ,
  sm3_type =1009
}
 Hash type.
 

Public Member Functions

 Hash (Algorithm)
 Initialize with hash type. More...
 
 Hash (const Hash &)=delete
 
Hashoperator= (const Hash &)=delete
 
 Hash (Hash &&other)
 Move constructor. More...
 
Hashoperator= (Hash &&other)
 Move assign. More...
 
void reset ()
 Reset the hash to initial value.
 
void update (const void *, int)
 Update the hash by adding data. More...
 
void update (const std::vector< char > &v)
 Update using a char vector.
 
void update (const std::string v)
 Update using a string.
 
int get_tag (void *, int)
 Get the current hash value without resetting the hash. More...
 
int get_tag (std::vector< char > &v)
 Get the full current hash value using a secure vector.
 
int get_tag (std::vector< char > &v, int sz)
 Get the current hash value using a vector, for length between 1 and size of hash. More...
 
int final (void *, int)
 Write out a final value, and reset the hash. More...
 
int final (std::vector< char > &v)
 Get the final value using a vector. More...
 
int alg () const
 Hash algorithm. More...
 
int size () const
 Return the current hash size.
 

Static Public Member Functions

static int alg_size (Algorithm alg)
 
static bool supported (Algorithm)
 Test if an algorithm type is supported.
 

Detailed Description

General one-way hashing algorithms.

On 64-bit processors, sha512 and sha384 outperforms sha256 for general data, due to the use of 64-bit word size for calculations.

Secure hash standard is here: https://csrc.nist.gov/csrc/media/publications/fips/180/4/final/documents/fips180-4-draft-aug2014.pdf

Security of hash algorithms is here: https://ws680.nist.gov/publication/get_pdf.cfm?pub_id=911479

Collision resistance strength is, in general, 2^(n/2) where n is bit length of hash.

So, for sha512/224, for example, this is 2^112 or S~=10^34

Can show that the chance of collision for k hashes, is approx. k^2 / S.

So for 10^9 hashes, probablility of collision would be approx. 10^-16

For sha512/256, S~=10^38, so 10^9 hashes collision probablity would be approx. 10^-20

MD5 and SHA1 hashes are not considered secure, but are included for applications where better performance is needed.

Examples
scclib/crypto/unittest/hash_digest.cc.

Definition at line 87 of file hash.h.

Constructor & Destructor Documentation

◆ Hash() [1/2]

scc::crypto::Hash::Hash ( Algorithm  )

Initialize with hash type.

Creating an algorithm for an unsupported type results in an exception.

Parameters
algHash algorithm,
See also
Hash::Algorithm

◆ Hash() [2/2]

scc::crypto::Hash::Hash ( Hash &&  other)

Move constructor.

Original hash will be invalid, and throw an exception if operations are attempted.

Member Function Documentation

◆ alg()

int scc::crypto::Hash::alg ( ) const
inline

Hash algorithm.

See also
Hash::Algorithm

Definition at line 206 of file hash.h.

◆ final() [1/2]

int scc::crypto::Hash::final ( std::vector< char > &  v)
inline

Get the final value using a vector.

Return size written to output vector.

Definition at line 199 of file hash.h.

◆ final() [2/2]

int scc::crypto::Hash::final ( void *  ,
int   
)

Write out a final value, and reset the hash.

Parameters
locHash value buffer
lenSize of buffer, must be the full hash size.
Returns
size written

Throws an exception if len is too small to store hash.

◆ get_tag() [1/2]

int scc::crypto::Hash::get_tag ( std::vector< char > &  v,
int  sz 
)
inline

Get the current hash value using a vector, for length between 1 and size of hash.

Return size written to output vector.

Definition at line 184 of file hash.h.

◆ get_tag() [2/2]

int scc::crypto::Hash::get_tag ( void *  ,
int   
)

Get the current hash value without resetting the hash.

Parameters
locHash value buffer
lenSize of buffer, can be between 1 and full hash size.
Returns
size written

Can request a subset of hash.

Throws exception if buffer size is out of range.

◆ operator=()

Hash& scc::crypto::Hash::operator= ( Hash &&  other)

Move assign.

Original hash will be invalid, and throw an exception if operations are attempted.

◆ update()

void scc::crypto::Hash::update ( const void *  ,
int   
)

Update the hash by adding data.

Parameters
locUpdate data buffer
lenSize of buffer

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