在HMAC SHA256中编码字符串 [英] Encoding a string in HMAC SHA256
问题描述
如何使用HMAC SHA256算法编码字符串?我浏览了OpenSSL库,但没有发现任何有价值的东西.您有什么建议吗?
How can I encode a string using HMAC SHA256 algorithm? I went through OpenSSL library but didn't find anything valuable. Your suggestions?
推荐答案
SHA256 是OpenSSL中的独立组件,您需要自己将它们粘合在一起. (请注意,这使用速记方法通过单片缓冲区一次完成所有操作;增量处理更为复杂.)
HMAC and SHA256 are separate components in OpenSSL, you'll need to glue them together yourself. (Note that this uses the shorthand methods for doing everything in one shot with monolithic buffers; incremental processing is more complex.)
#include <openssl/evp.h>
#include <openssl/hmac.h>
unsigned char* hmac_sha256(const void *key, int keylen,
const unsigned char *data, int datalen,
unsigned char *result, unsigned int* resultlen)
{
return HMAC(EVP_sha256(), key, keylen, data, datalen, result, resultlen);
}
即使您的输入是字符串,结果也是任意字节数组;如果也需要使用字符串,则必须应用其他一些转换,例如十六进制扩展,Base64或适合您应用程序的任何转换.
Even if your input is a string, the result is an arbitrary byte array; if that too needs to be a string then you'll have to apply some other transformation like hexadecimal expansion, Base64 or whatever suits your application.
这篇关于在HMAC SHA256中编码字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!