|
scclib
Stable Cloud Computing C++ Library
|
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 | |
| Hash & | operator= (const Hash &)=delete |
| Hash (Hash &&other) | |
| Move constructor. More... | |
| Hash & | operator= (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. | |
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.
| scc::crypto::Hash::Hash | ( | Algorithm | ) |
Initialize with hash type.
Creating an algorithm for an unsupported type results in an exception.
| alg | Hash algorithm, |
| scc::crypto::Hash::Hash | ( | Hash && | other | ) |
Move constructor.
Original hash will be invalid, and throw an exception if operations are attempted.
|
inline |
|
inline |
| int scc::crypto::Hash::final | ( | void * | , |
| int | |||
| ) |
Write out a final value, and reset the hash.
| loc | Hash value buffer |
| len | Size of buffer, must be the full hash size. |
Throws an exception if len is too small to store hash.
|
inline |
| int scc::crypto::Hash::get_tag | ( | void * | , |
| int | |||
| ) |
Get the current hash value without resetting the hash.
| loc | Hash value buffer |
| len | Size of buffer, can be between 1 and full hash size. |
Can request a subset of hash.
Throws exception if buffer size is out of range.
Move assign.
Original hash will be invalid, and throw an exception if operations are attempted.
| void scc::crypto::Hash::update | ( | const void * | , |
| int | |||
| ) |
Update the hash by adding data.
| loc | Update data buffer |
| len | Size of buffer |