在HMAC SHA256中编码字符串 [英] Encoding a string in HMAC SHA256

查看:221
本文介绍了在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?

推荐答案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆