使用Crypto ++生成SHA256哈希,使用字符串作为输入和输出? [英] Generating a SHA256 hash with Crypto++, using a string as input and output?

查看:1957
本文介绍了使用Crypto ++生成SHA256哈希,使用字符串作为输入和输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个如何使用Crypto ++从std :: string生成SHA256哈希并输出std :: string的示例。我似乎无法弄清楚。我试过的所有东西都给了我无效的输出。



以下是interjay回答后的新代码:

  string SHA256(string data)
{
byte const * pbData =(byte *)data.data();
unsigned int nDataLen = data.size();
字节abDigest [CryptoPP :: SHA256 :: DIGESTSIZE];

CryptoPP :: SHA256()。CalculateDigest(abDigest,pbData,nDataLen);

返回字符串((char *)abDigest);

SHA256(A)的输出;是



我怎样才能把它变成可读格式?



感谢interjay的回答,我能够生成最终的散列。 / b>

解决方案

这行会给出错误的结果:

  unsigned int nDataLen = sizeof(pbData); 

它总会给你一个指针的大小。你需要的是 data.size()



另外,你不需要这部分:

  if(!CryptoPP :: SHA256()。VerifyDigest(abDigest,pbData,nDataLen))
{
返回SHA256(数据);
}

它应该始终验证正确,因为您刚刚计算基于相同的摘要数据。如果没有,你会进入无限递归。



要获得可读的输出,可以将它转换为十六进制。以下是来自 Crypto ++ Wiki 的MD5示例,如果您将MD5替换为SHA256,它应该适合您:

  CryptoPP :: MD5 hash; 
字节摘要[CryptoPP :: MD5 :: DIGESTSIZE];
std :: string message =abcdefghijklmnopqrstuvwxyz;

hash.CalculateDigest(digest,(byte *)message.c_str(),message.length());

CryptoPP :: HexEncoder编码器;
std :: string输出;
encoder.Attach(new CryptoPP :: StringSink(output));
encoder.Put(digest,sizeof(digest));
encoder.MessageEnd();

std :: cout<<输出<的std :: ENDL;


I need an example of how to use Crypto++ to generate a SHA256 hash from a std::string and output a std::string. I can't seem to figure it out. Everything I've tried gives me invalid output.

Here's the new code after interjay's answer:

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 for SHA256("A"); is

How can I turn this into a readable format?

Thanks to interjay's answer I was able to generate the final hash.

解决方案

This line will give the wrong results:

unsigned int nDataLen = sizeof(pbData);

It will always give you the size of a pointer. What you want instead is data.size().

Also, you don't need this part:

if(!CryptoPP::SHA256().VerifyDigest(abDigest, pbData, nDataLen))
{
    return SHA256(data);
}

It should always verify correctly, since you just calculated the digest based on the same data. And if it didn't, you'd go into infinite recursion.

To get readable output, you can convert it to hex. Here's an example for MD5 from the Crypto++ Wiki, it should work for you if you replace MD5 with SHA256:

CryptoPP::MD5 hash;
byte digest[ CryptoPP::MD5::DIGESTSIZE ];
std::string message = "abcdefghijklmnopqrstuvwxyz";

hash.CalculateDigest( digest, (byte*) message.c_str(), message.length() );

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

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

这篇关于使用Crypto ++生成SHA256哈希,使用字符串作为输入和输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆