简体   繁体   中英

How to use sha256 in c++ from cryptopp

First of all thank you for reading my question.

I am trying to reproduce what i recieved in python. So bsically I have two numbers and I am trying to make a hash out of them. But with crypto++ library i am a bit confused.

Basically I dont get how to use the update and calculate part.

My codes are

    vHash(uint8_t *X,uint8_t *Y)
{
    CryptoPP::SHA256 hash;
    byte digest[ CryptoPP::SHA256::DIGESTSIZE ];

    hash.CalculateDigest( digest, (byte*) X, 32 );
    hash.CalculateDigest( digest, (byte*) y, 32 );


    CryptoPP::HexEncoder encoder;
    std::string output;
    encoder.Attach( new CryptoPP::StringSink( output ) );
    encoder.Put( digest, sizeof(digest) );
    encoder.MessageEnd();

    std::cout << output << std::endl;

}

and from main function i am passing

std::string expTA = "67950298974562277680845951570422977358023181305748156081323854942335052287978";
std::string expTB = "43115149028018416730354385099369257625840461867770683759285196743048111210215";

HexDecoder decoder;
decoder.Put((byte*)&exp1[0], expTA.size());
decoder.MessageEnd();

Integer testA;
testA.Decode(decoder, decoder.MaxRetrievable());

HexDecoder decoder1;
decoder1.Put((byte*)&exp1[0], expTB.size());
decoder1.MessageEnd();

Integer testB;
testB.Decode(decoder1, decoder1.MaxRetrievable());


vHash((uint8_t *)&(testA),(uint8_t *)&(testB));

I found the solution I am pasting it here So that it may be useful for someone else.

For calculating right sha256 first to update and then call the calculate function in the manner mentioned below.

hash.Update((byte*) X, 32 );
hash.CalculateDigest( digest, (byte*) y, 32 );

This is optional but to make sure what we are comparing with than be sure to enter eiher the int values or hex values ur answer will be generated in that manner. ie std::string expTA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM