gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
hi
- User
hi
- Assistant
Hello! How can I assist you today?
- User
c++ 用 cryptopp 写个 sha256加密验证的代码
- Assistant
当然!以下是一个使用Crypto++库在C++中编写SHA256加密和验证的示例代码:
#include <iostream> #include <iomanip> #include <string> #include "cryptopp/sha.h" #include "cryptopp/hex.h" std::string sha256(const std::string& message) { CryptoPP::SHA256 hash; std::string digest; CryptoPP::StringSource s(message, true, new CryptoPP::HashFilter(hash, new CryptoPP::HexEncoder(new CryptoPP::StringSink(digest)))); return digest; } bool verifySHA256(const std::string& message, const std::string& hash) { std::string calculatedHash = sha256(message); return (calculatedHash == hash); } int main() { std::string message = "Hello, World!"; std::string hash = sha256(message); std::cout << "Message: " << message << std::endl; std::cout << "SHA256 Hash: " << hash << std::endl; bool isHashValid = verifySHA256(message, hash); if (isHashValid) { std::cout << "Hash verification successful!" << std::endl; } else { std::cout << "Hash verification failed!" << std::endl; } return 0; }在这个示例中,我们首先定义了两个函数
sha256和verifySHA256。sha256函数接受一个字符串作为输入,并返回其SHA256哈希值。verifySHA256函数用于验证消息和哈希值是否匹配。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.