hmac加密

hmac加密

前面介绍了mbedtls常用的一些加密校验算法,还有部分加密算法还在研究阶段。今天介绍下加密算法的的hmac加密方法。

在线加密工具点击这里
在这里插入图片描述
1.工具在线加密
hmac sha1
在这里插入图片描述
hmac md5
在这里插入图片描述
2.ubuntu编写程序实现
主要介绍下hmac md5实现,其它模仿md5实现即可
hmac_test.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"hmac.h"

int main(int argc, const char *argv[])
{
        char date[32]="hello world";

//      char key[16] = {'1','2','3','4','5','6'};
        char key[16] = "123456";
        char out[256];

        memset(out,0,256);
        //hmac md5
        utils_hmac_md5(date,strlen(date),out,key,strlen(key));

        printf("outstring hmac md5:%s\n",out);

        memset(out,0,256);

        //hmac sha1
        utils_hmac_sha1(date,strlen(date),out,key,strlen(key));

        printf("outstring hmac sha1:%s\n",out);
        return 0;
}

hmac.c

#include <string.h>
#include "md5.h"

#define KEY_IOPAD_SIZE 64

#define MD5_DIGEST_SIZE 16

void utils_hmac_md5(const char *msg, int msg_len, char *digest, const char *key, int key_len)
{
	//对函数参数判空
    if((NULL == msg) || (NULL == digest) || (NULL == key)) {
        return;
    }
	//限制密钥长度
    if(key_len > KEY_IOPAD_SIZE) {
        return;
    }
	//hmac md5加密处理
    iot_md5_context context;
    unsigned char k_ipad[KEY_IOPAD_SIZE];    /* inner padding - key XORd with ipad  */
    unsigned char k_opad[KEY_IOPAD_SIZE];    /* outer padding - key XORd with opad */
    unsigned char out[MD5_DIGEST_SIZE];
    int i;

    /* start out by storing key in pads */
    memset(k_ipad, 0, sizeof(k_ipad));
    memset(k_opad, 0, sizeof(k_opad));
    memcpy(k_ipad, key, key_len);
    memcpy(k_opad, key, key_len);

    /* XOR key with ipad and opad values */
    for (i = 0; i < KEY_IOPAD_SIZE; i++) {
        k_ipad[i] ^= 0x36;
        k_opad[i] ^= 0x5c;
    }

    /* perform inner MD5 */
    utils_md5_init(&context);                                      /* init context for 1st pass */
    utils_md5_starts(&context);                                    /* setup context for 1st pass */
    utils_md5_update(&context, k_ipad, KEY_IOPAD_SIZE);            /* start with inner pad */
    utils_md5_update(&context, (unsigned char *) msg, msg_len);    /* then text of datagram */
    utils_md5_finish(&context, out);                               /* finish up 1st pass */

    /* perform outer MD5 */
    utils_md5_init(&context);                              /* init context for 2nd pass */
    utils_md5_starts(&context);                            /* setup context for 2nd pass */
    utils_md5_update(&context, k_opad, KEY_IOPAD_SIZE);    /* start with outer pad */
    utils_md5_update(&context, out, MD5_DIGEST_SIZE);      /* then results of 1st hash */
    utils_md5_finish(&context, out);                       /* finish up 2nd pass */
    
	//加密后的数据16进制输出
    for (i = 0; i < MD5_DIGEST_SIZE; ++i) {
        digest[i * 2] = utils_hb2hex(out[i] >> 4);
        digest[i * 2 + 1] = utils_hb2hex(out[i]);
    }
}

上面实现hmac_*关键性代码已经给出,其它都在各自的算法基础上按上述逻辑实现即可。

编译后验证:
在这里插入图片描述
通过对比数据,代码通过key:123456,对date:hello world分别进行hmac md5与hmac sha1加密后得到的数据与在线加密是一致的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值