Cryptographic Hash Functions are a one-way algorithm that takes an input of any size and produces the same size output. The cryptographic hash functions are a more secure version of the hash functions. It is one-way in that there is information loss — you can’t easily go from the output to the input again. The cryptographic hash is a more secure version of the hash function. It is most often used in signing to validate that data hasn’t been modified. It is also used as a crypto function for usernames, passwords etc. Some of popular hash functions are SHA, SHA2, SHA3, MD5, BobJenkins. There is a good post by Jim McKeeth about SHA Hash with C++Builder and Delphi.
Cryptographic Hash Functions are good to store usernames and passwords because of their one-way algorithm. When a user creates a new account, the application should hash that username and password, and it should save hashed username password to the database. If someone is working on a database (i.e. db developer), he/she can see hash codes but it is impossible to know username and passwords there. When the user logins, again login event of application hashes entered username and password and you query these hashed variables if both are correct. If it is correct then your application allows users to login. Cryptographic Hash Functions are also good to understand if there are any changes in long texts or to check their originality.
In C++ Builder hash functions are included in System.Hash.hpp, so you must have this include to use these functions
Generation Hash Codes with SHA function:
1 2 3 4 |
ustr = L"Hey! this unicode string will be hashed"; UnicodeString hash_sha = THashSHA1::GetHashString(ustr); |
Generation Hash Codes with SHA2 function:
1 2 3 4 5 6 7 8 9 |
ustr = L"Hey! this unicode string will be hashed"; UnicodeString hash_sha224 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA224); UnicodeString hash_sha256 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA256); UnicodeString hash_sha384 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA384); UnicodeString hash_sha512 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512); UnicodeString hash_sha512224 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512_224); UnicodeString hash_sha512256 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512_256); |
Generation Hash Codes with MD5 function:
1 2 |
ustr = L"Hey! this unicode string will be hashed"; UnicodeString hash_MD5 = THashMD5::GetHashString(ustr); |
Generation Hash Codes with BobJenkins function:
1 2 3 4 |
ustr = L"Hey! this unicode string will be hashed"; UnicodeString hash_bob = THashBobJenkins::GetHashString(ustr); |
Full Example in VCL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <vcl.h> #include <System.Hash.hpp> void generate_hashcodes(UnicodeString ustr) { UnicodeString hash_sha = THashSHA1::GetHashString(ustr); UnicodeString hash_sha224 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA224); UnicodeString hash_sha256 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA256); UnicodeString hash_sha384 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA384); UnicodeString hash_sha512 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512); UnicodeString hash_sha512224 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512_224); UnicodeString hash_sha512256 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512_256); UnicodeString hash_MD5 = THashMD5::GetHashString(ustr); UnicodeString hash_bob = THashBobJenkins::GetHashString(ustr); } |
Full Example in FMX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <fmx.h> #include <System.Hash.hpp> void generate_hashcodes(UnicodeString ustr) { UnicodeString hash_sha = THashSHA1::GetHashString(ustr); UnicodeString hash_sha224 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA224); UnicodeString hash_sha256 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA256); UnicodeString hash_sha384 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA384); UnicodeString hash_sha512 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512); UnicodeString hash_sha512224 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512_224); UnicodeString hash_sha512256 = THashSHA2::GetHashString(ustr, THashSHA2::TSHA2Version::SHA512_256); UnicodeString hash_MD5 = THashMD5::GetHashString(ustr); UnicodeString hash_bob = THashBobJenkins::GetHashString(ustr); } |
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition