How to Generate Whirlpool Hash with C++ and OpenSSL
The Whirlpool Galaxy (M51) inspired the name of the algorithm.
In the previous article we showed how to generate Hash with MD5, as described there, the concepts will be similar.
Whirlpool (sometimes called WHIRLPOOL ) is a cryptographic hash function based on an AES block cipher.
It generates a hash of length of 128 characters.
Using with C++
For this example, we are going to hash the word Terminal Root . Follow the code below:
whirlpool.cpp
#include <iostream>
#include <iomanip>
#include <sstream>
#include <openssl/whrlpool.h>
std::string whirlpool(const std::string &str){
unsigned char hash[WHIRLPOOL_DIGEST_LENGTH];
WHIRLPOOL_CTX whirlpool;
WHIRLPOOL_Init(&whirlpool);
WHIRLPOOL_Update(&whirlpool, str.c_str(), str.size());
WHIRLPOOL_Final(hash, &whirlpool);
std::stringstream ss;
for(int i = 0; i < WHIRLPOOL_DIGEST_LENGTH; i++){
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>( hash[i] );
}
return ss.str();
}
int main() {
std::cout << whirlpool("Terminal Root") << '\n';
return 0;
}
To compile, run:
g++ whirlpool.cpp -lcrypto
The possible and expected output will be:
edef01c730c67430584283292a59e3a6e910ecf7c903c0b1040243498c67053a849c0eacc07dfdbd76be36d2d1e50eaa0e0b6324f96743828eccdf8dac471891
To check if it is correct, use the rhash
command with the following arguments:
rhash --whirlpool -m "Terminal Root"
Links Ășteis
- https://www.openssl.org/docs/man3.0/man3/EVP_whirlpool.html
- https://en.wikipedia.org/wiki/Whirlpool_(hash_function)
- https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
Comments