HMAC-SHA1を計算したかった。
win32にはcrypt32とかあるけどよくわからなかったので
便利
#include "../sha1.h" #include "../hmac.h" #include <string> #include <vector> #include <iostream> int main() { std::string input= "what do ya want for nothing?"; std::string key = "Jefe"; std::string hash = hmac<SHA1>(&input[0], input.size(), &key[0], key.size()); std::cout << hash << std::endl; return 0; }
Test Cases for HMAC-MD5 and HMAC-SHA-1
ただし出力が16進数のstringなので、一旦char arrayなどにして解釈し直さないと、間違えることがある。
バイト列でほしいなら
std::string hash = hmac<SHA1>(&input[0], input.size(), &key[0], key.size()); std::vector<unsigned char> v(hash.length() / 2, 0); for (size_t i = 0; i < hash.length() / 2; i++) { std::string b = hash.substr(i * 2, 2); v[i] = std::stoi(b, NULL, 16); }
こんな感じにする必要がある。