使用Crypto++与SHA1生成随机哈希

Using Crypto++ to generate random hashes with SHA1

本文关键字:随机 哈希 SHA1 Crypto++ 使用      更新时间:2023-10-16

我需要使用Crypto++生成随机哈希,使用SHA1。现在我有:

#include <cryptopp/sha.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>
...
CryptoPP::SHA1 sha1;
string source = "Hello";  //This will be randomly generated somehow
string hash = "";
StringSource(source, true, new HashFilter(sha1, new HexEncoder(new StringSink(hash))));

当我开始编译时,我得到以下错误报告:

error: expected type-specifier before 'HashFilter'
error: expected ')' before 'HashFilter'
error: 'StringSource' was not declared in this scope
谁能帮我把这个修好?是否有一种更简单的方法使用这个库来执行此操作?我是使用Crypto++的新手,所以所有的帮助都会非常感激。

谢谢。

只需正确且仔细地指定您的名称空间:

#include <cryptopp/sha.h>
#include <cryptopp/filters.h>
#include <cryptopp/hex.h>
#include <string>
int main()
{
  CryptoPP::SHA1 sha1;
  std::string source = "Hello";  //This will be randomly generated somehow
  std::string hash = "";
  CryptoPP::StringSource(source, true, new CryptoPP::HashFilter(sha1, new CryptoPP::HexEncoder(new CryptoPP::StringSink(hash))));
}