加密++库中的SHA512哈希抛出异常

问题描述

我在 Visual Studio 2010 中构建了 crypto++ 8.4 并制作了一些测试代码。 它在“Win32|Debug”中工作正常,但在“Win32|Release”哈希中。CalculateDigest 抛出异常。

    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));

发生异常的位置如下。 (sha.cpp)

enter image description here

我做错了什么?

解决方法

我发现了一个明确的例外情况。 如果将 'hash' 声明为指针而不是局部变量,则不会引发异常。

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

我不知道为什么会发生这种情况,但在 SHA512 的情况下,我怀疑 VS 2010 中的 cryptopp 中存在内存管理问题。