Search code examples
c++exceptionvisual-c++crypto++sha512

SHA512 hash in crypto++ library throws exception


I built crypto++ 8.4 in visual studio 2010 and made some test code. It works fine in 'Win32|Debug', but in 'Win32|Release' hash.CalculateDigest throws an exception.

    string source = "I am a programmer.";

    CryptoPP::SHA512 hash;
    CryptoPP::byte digest[CryptoPP::SHA512::DIGESTSIZE];            
    hash.CalculateDigest(digest, (const CryptoPP::byte*)source.c_str(), source.length());

    string result;  
    CryptoPP::HexEncoder encoder;

    encoder.Attach(new CryptoPP::StringSink(result));
    encoder.Put(digest, sizeof(digest));

The location where the exception occurs is as follows. (sha.cpp) enter image description here

What have I done wrong?


Solution

  • I have found definite case of an exception. If 'hash' is declared as a pointer rather than a local variable, no exception is raised.

    CryptoPP::SHA512 * hash = new CryptoPP::SHA512;
    

    I don't know why this is happening, but in the case of SHA512, I doubt that there is a memory management problem in cryptopp in VS 2010.