Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 这个sha 256函数有什么问题?_C++_Sha_Crypto++ - Fatal编程技术网

C++ 这个sha 256函数有什么问题?

C++ 这个sha 256函数有什么问题?,c++,sha,crypto++,C++,Sha,Crypto++,我开始使用crypto++lib,也许我有一些误解 我不明白为什么下面的代码会产生一个坏的sha # include <string> # include <iostream> # include "cryptopp/cryptlib.h" # include <cryptopp/sha.h> # include <cryptopp/hex.h> using namespace std; string sha256_hex(const stri

我开始使用crypto++lib,也许我有一些误解

我不明白为什么下面的代码会产生一个坏的sha

# include <string>
# include <iostream>
# include "cryptopp/cryptlib.h"
# include <cryptopp/sha.h>
# include <cryptopp/hex.h>

using namespace std;

string sha256_hex(const string & str)
{
  byte digest[CryptoPP::SHA256::DIGESTSIZE];
  CryptoPP::SHA256().CalculateDigest(digest, (byte*) &str[0], str.size());

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

  return ret;
}

int main(int argc, char *argv[])
{
  auto sha256 = sha256_hex(argv[1]);
  cout << "str     = " << argv[1] << endl
       << "sha 256 = " << sha256_hex(sha256) << endl;

  return 0;
}
生成以下输出

str     = hello
sha 256 = DD9F20FF4F1DD817C567DE6C16915DC0A731A4DF51088F55CEF4CD2F89CF9620
然而,根据这个在线计算器,正确的sha 256表示“你好”应该是
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

所以我的问题是我做错了什么

我还有一个问题:应该如何以及何时释放
StringSink
对象使用的内存


提前感谢

很可能,我发现问题在这里:

CryptoPP::SHA256().CalculateDigest(digest, (byte*) &str[0], str.size());

你最好通过
str.c_str()
。使用调试器查看传递的确切内容-它是否转换为
hello

最可能的情况是,我发现问题在这里:

CryptoPP::SHA256().CalculateDigest(digest, (byte*) &str[0], str.size());

你最好通过
str.c_str()
。使用调试器查看传递的确切内容-它是否转换为
hello

您不是在这里计算散列的散列吗?调用
sha256\u hex
两次,一次使用
argv[1]
作为参数,一次使用
sha256
本身作为参数。

您不是在计算散列的散列吗?调用
sha256\u hex
两次,一次使用
argv[1]
作为参数,一次使用
sha256
本身作为参数。

至于问题的第二部分:

在Crypto++中,过滤器和接收器由它们所连接的对象拥有。它们会被该对象的析构函数自动删除

以及以下中的“重要使用说明”:

如果a的构造函数获取指向对象B的指针(int和char等基本类型除外),则a拥有B并将在a销毁时删除B。如果a的构造函数引用了对象B,那么调用方将保留B的所有权,并且在a不再需要它之前不应销毁它


至于问题的第二部分:

在Crypto++中,过滤器和接收器由它们所连接的对象拥有。它们会被该对象的析构函数自动删除

以及以下中的“重要使用说明”:

如果a的构造函数获取指向对象B的指针(int和char等基本类型除外),则a拥有B并将在a销毁时删除B。如果a的构造函数引用了对象B,那么调用方将保留B的所有权,并且在a不再需要它之前不应销毁它


@是的,使用调试器和.or添加打印语句,这是开发代码的一部分。因此,您不是调试器,它是完成基本调试后的最后帮助。从
&str[0]
str.c_str()
返回的值有什么不同?当然,这不是由C++标准保证的,但是你有没有遇到过一个实现,这些表达式在语义上没有相同?我不知道为什么这个“答案”会被选上……
&str[0]
是正确的,因为它提供了一个非常量指针
str.c_str()
提供了一个cont指针,向其写入是未定义的行为。@lrleon是的,请使用调试器和.or添加打印语句,这是开发代码的一部分。因此,您不是调试器,它是完成基本调试后的最后帮助。从
&str[0]
str.c_str()
返回的值有什么不同?当然,这不是由C++标准保证的,但是你有没有遇到过一个实现,这些表达式在语义上没有相同?我不知道为什么这个“答案”会被选上……
&str[0]
是正确的,因为它提供了一个非常量指针
str.c_str()
提供了一个cont指针,向它写入是未定义的行为。Hi@lLeon我遇到了这种错误-->错误:未定义对“vtable for CryptoPP::SHA256”的引用我想用CryptoPP生成SHA256,你能帮我吗?你好@lrleon我遇到了这种错误-->错误:对“vtable for CryptoPP::SHA256”的未定义引用我想用CryptoPP生成SHA256,你能帮我吗?