Skip to content

Instantly share code, notes, and snippets.

@vincenzopalazzo
Created February 24, 2020 11:54
Show Gist options
  • Save vincenzopalazzo/b6c5112ed4e53c15c386033aad7d56e4 to your computer and use it in GitHub Desktop.
Save vincenzopalazzo/b6c5112ed4e53c15c386033aad7d56e4 to your computer and use it in GitHub Desktop.
This is code implemented inside the Bitcoin stack aswer
/**
* Convert array bytes to string.
**/
std::string ToString(uint8_t bytes[Ripemd160::HASH_LEN]){
std::string hashResult;
std::stringstream stream;
for(int i = 0; i < Ripemd160::HASH_LEN; i++){
int valueInt = static_cast<int>(bytes[i]);
stream << std::hex << std::setprecision(2) << std::setw(2) << std::setfill('0') << valueInt;
}
hashResult = stream.str();
return hashResult;
}
/**
* Convert the public key to hash.
**/
void ToHashPublicKey(std::string pubKey)
{
//TODO check if the string is valid.
cout << "\n\n\n------------- Test for Bitcoin.stack question -------------" << endl;
cout << "Public Key: " << "02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737\n";
string keyTmp = "02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737";
string flag = keyTmp.substr(0, 2);
if(flag == "02"){
cout << "Compressed with y event\n";
}else if(flag == "03"){
cout << "Compressed with y odd\n";
}else{
assert(flag == "04");
}
Bytes bytesTmp = hexBytes(keyTmp.c_str());
//SHA256
Sha256Hash shaHash = Sha256::getHash(bytesTmp.data(), bytesTmp.size());
uint8_t result[Ripemd160::HASH_LEN];
Ripemd160::getHash(shaHash.value, sizeof(shaHash), result);
cout << "Hash Public key: " << ToString(result) << endl;
}
@vincenzopalazzo
Copy link
Author

References

The reference of the project are off-topics here but I think without it this post can be fake, so I'm adding the reference of the library used to implement my code in this answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment