C++Builder Learn To Use Powerful Cryptographic Hash Functions In Modern C++ On Windows (SHA, SHA2, MD5, BobJenkins)

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn To Use Powerful Cryptographic Hash Functions In Modern C++ On Windows (SHA, SHA2, MD5, BobJenkins)
February 4, 2021 By Yilmaz Yoru

Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся.

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:
C++:
ustr = L"Hey! this unicode string will be hashed";
UnicodeString hash_sha = THashSHA1::GetHashString(ustr);
Generation Hash Codes with SHA2 function:
C++:
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:
C++:
ustr = L"Hey! this unicode string will be hashed";
UnicodeString hash_MD5 = THashMD5::GetHashString(ustr);
Generation Hash Codes with BobJenkins function:
C++:
ustr = L"Hey! this unicode string will be hashed";
UnicodeString hash_bob = THashBobJenkins::GetHashString(ustr);
Full Example in VCL
C++:
#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
C++:
#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);
}