Hi
I would like to use the crypto++ librairy to create a transaction from scratch.
My first question is why this code doesn't work:
#include "crypto++/cryptlib.h"
#include "crypto++/sha.h"
#include "crypto++/hex.h"
#include <iostream>
using namespace std;
int main () {
string myString = string("abc");
string myStringSHA = SHA256(myString);
std::cout << "SHA(abc): " << myStringSHA << std::endl;
}
the output is:
SHA(abc): #x####AA@###"##a##z###a####
I have the right result with the following:
#include "crypto++/cryptlib.h"
#include "crypto++/sha.h"
#include "crypto++/hex.h"
#include <iostream>
using namespace std;
string SHA256(string);
int main () {
CryptoPP::SHA256 hash;
byte digest[ CryptoPP::SHA256::DIGESTSIZE ];
string message = "abc";
hash.CalculateDigest( digest, reinterpret_cast<byte*>(&message[0]), message.length() );
CryptoPP::HexEncoder encoder;
std::string output;
encoder.Attach( new CryptoPP::StringSink( output ) );
encoder.Put( digest, sizeof(digest) );
encoder.MessageEnd();
std::cout << " " << std::endl << "SHA(abc): " << output << std::endl;
}
string SHA256(string data)
{
byte const* pbData = (byte*) data.data();
unsigned int nDataLen = data.size();
byte abDigest[CryptoPP::SHA256::DIGESTSIZE];
CryptoPP::SHA256().CalculateDigest(abDigest, pbData, nDataLen);
return string((char*)abDigest);
}
the output is:
SHA(abc): BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD
which is right according to:
http://www.xorbin.com/tools/sha256-hash-calculatorThanks