简体   繁体   English

使用Crypto ++生成SHA256哈希,使用字符串作为输入和输出?

[英]Generating a SHA256 hash with Crypto++, using a string as input and output?

I need an example of how to use Crypto++ to generate a SHA256 hash from a std::string and output a std::string. 我需要一个如何使用Crypto ++从std :: string生成SHA256哈希并输出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: 这是interjay回答后的新代码:

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"); 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. 感谢interjay的回答,我能够生成最终的哈希值。

This outputs a base64 string using the CryptoPP::Base64Encoder : 这使用CryptoPP::Base64Encoder输出base64字符串:

#include "sha.h"
#include "filters.h"
#include "base64.h"

std::string SHA256HashString(std::string aString){
    std::string digest;
    CryptoPP::SHA256 hash;

    CryptoPP::StringSource foo(aString, true,
    new CryptoPP::HashFilter(hash,
      new CryptoPP::Base64Encoder (
         new CryptoPP::StringSink(digest))));

    return digest;
}

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() . 你想要的是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: 这是来自Crypto ++ Wiki的 MD5示例,如果用SHA256替换MD5,它应该适合您:

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;  

Your code will expect a null-terminated string from the buffer you supply to the string constructor! 您的代码将期望从您提供给字符串构造函数的缓冲区中以空字符结尾的字符串! Which means the result will almost certainly be wrong. 这意味着结果几乎肯定是错的。

To enforce the digest size and, use the following instead: 要强制执行摘要大小,请使用以下代码:

return std::string((char*)abDigest, CryptoPP::SHA256::DIGESTSIZE);

Also with respect to printing it the following correctly produces the test vector BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD for the string "abc" 另外,对于打印它,以下正确生成字符串"abc"的测试向量BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD

std::string string_to_hex(const std::string& input)
{
  static const char* const lut = "0123456789ABCDEF";
  size_t len = input.length();

  std::string output;
  output.reserve(2 * len);
  for (size_t i = 0; i < len; ++i)
  {
    const unsigned char c = input[i];
    output.push_back(lut[c >> 4]);
    output.push_back(lut[c & 15]);
  }
  return output;
}

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

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

  // return string((char*)abDigest);  -- BAD!!!
  return std::string((char*)abDigest, CryptoPP::SHA256::DIGESTSIZE);
}

void test_cryptopp() {
  std::cout << string_to_hex(SHA256("abc")) << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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