[英]How to create a HMAC 256 using the Crypto++ library?
我有一个字符串,我需要使用 C++ 和 Crypto++ 在 HMAC 256 中对其进行编码。 来自图书馆维基的代码:
AutoSeededRandomPool prng;
SecByteBlock key(16);
prng.GenerateBlock(key, key.size());
string plain = "HMAC Test";
string mac, encoded;
/*********************************\
\*********************************/
// Pretty print key
encoded.clear();
StringSource ss1(key, key.size(), true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "key: " << encoded << endl;
cout << "plain text: " << plain << endl;
/*********************************\
\*********************************/
try
{
HMAC< SHA256 > hmac(key, key.size());
StringSource ss2(plain, true,
new HashFilter(hmac,
new StringSink(mac)
) // HashFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}
/*********************************\
\*********************************/
// Pretty print
encoded.clear();
StringSource ss3(mac, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "hmac: " << encoded << endl;
该示例提供了作品,但似乎做了很多工作。 我想要做的就是:
所以,我的问题是,上面示例代码中的所有步骤都是必需的吗? (字节块声明、十六进制编码等)
如果这是一个非常noobish 的问题,我们深表歉意。
不, 您的上述步骤当然不是必需的,例如以base 64编码已经是base 64编码的值。
Crypto ++主要基于带有接收器和源的流。 那只是库的建立方式,但是对于小的计算,它将有些冗长。
请注意,大多数示例代码仅是密钥生成,并打印出明文,密钥和身份验证标签(MAC值)以及一些异常处理。 所需的代码基本上只在try
/ catch
块内。
我发现有人(woodja)除了aws还在做同样的事情:
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include "cryptopp/cryptlib.h"
using CryptoPP::Exception;
#include "cryptopp/hmac.h"
using CryptoPP::HMAC;
#include "cryptopp/sha.h"
using CryptoPP::SHA256;
#include "cryptopp/base64.h"
using CryptoPP::Base64Encoder;
#include "cryptopp/filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::HashFilter;
string sign(string key, string plain)
{
string mac, encoded;
try
{
HMAC< SHA256 > hmac((byte*)key.c_str(), key.length());
StringSource(plain, true,
new HashFilter(hmac,
new StringSink(mac)
) // HashFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
}
encoded.clear();
StringSource(mac, true,
new Base64Encoder(
new StringSink(encoded)
) // Base64Encoder
); // StringSource
std::cout << "encode: " << encoded << endl;
return encoded;
}
int main()
{
string mykey = "m2hspk1ZxsjlsDU6JhMvD3TQQhm+zOwab3slKEILoSSnfk3b2+NUyeJiCrRAJ/D3V5y+QDZaIqRx9q9siMopaA==";
string msg = "GreatWallOfChina";
std::cout << "key: " << mykey << std::endl;
std::cout << "msg: " << msg << std::endl;
sign(mykey,msg);
return 0;
}
所以,我的问题是,上面示例代码中的所有步骤是否都必要?
好吧,您的问题的答案通常是“是”。 但是请记住,该代码是作为Wiki的示例编写的。
您可能需要也可能不需要。 但是好像您将要输入一个键和一个字符串。
AutoSeededRandomPool prng;
SecByteBlock key(16);
prng.GenerateBlock(key, key.size());
string plain = "HMAC Test";
string mac, encoded;
如果您不以ASCII格式打印,则可能不需要:
// Pretty print
encoded.clear();
StringSource ss(mac, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
删除上面的代码后,其余部分在下面,对我来说似乎并不多余:
try
{
HMAC< SHA256 > hmac(key, key.size());
StringSource ss(plain, true,
new HashFilter(hmac,
new StringSink(mac)
) // HashFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}
我需要一个最终的 32 个字符的结果。 不是 64/44 长。 如何使用 CryptoPP
SHA-256(256 位长)的编码选项:
Base64URL 编码 FIX ASCII:每个字符 6 位 = CHAR(44) 包括填充字符 十六进制:每个字符 4 位 = CHAR(64) 十六进制示例 → 617633EFEF2F98DCA32DD3BD8407122B9B375CF4BC076930F24C1F7A90F24C1F7A90F24C1F7A90F24C1F7A90F9 字节数组=Binary084F9 字节:Binary084F9 字节数组97, 118, 51, -17, -17, 47, -104, -36, -93, 45, -45, -67, -124, 7, 18, 43, -101, 55, 92, -12, -68, 7, 105, 48, -14, 76, 31, 122, 15, -109, 0, -108]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.