Ⅰ. はじめに
タイトルの通り「C++でSHA256ハッシュを計算する方法」です。
Ⅱ. やり方
1. 必要なパッケージをインストールする
vcpkg install botan
2. サンプルプログラムを書く
#include <botan/hash.h> // #include <botan/hex.h> #include <botan/base64.h> #include <iostream> std::string sha256(std::string str) { std::unique_ptr<Botan::HashFunction> hash(Botan::HashFunction::create("SHA-256")); hash->update(str); // return Botan::hex_encode(hash->final()); return Botan::base64_encode(hash->final()); } int main() { std::string str("helloworld"); std::cout << sha256(str) << std::endl; }
実行結果
k2oYXKqiZrucvpgengXLeM1zKwsygOuURBK7b4+PB68=