基本介绍

HMAC(散列消息身份验证码: Hashed Message Authentication Code)

它不是散列函数,而是采用散列函数(MD5 or 或SHA)与共享密钥一起使用的消息身份验证机制。

详细见RFC 2104

使用场景

  • 服务端生成key,传给客户端;
  • 客户端使用key将帐号和密码做HMAC,生成一串散列值,传给服务端;
  • 服务端使用key和数据库中用户和密码做HMAC计算散列值,比对来自客户端的散列值。

按照散列函数的不同,可以有如下实现。

Hmac_MD5,Hmac_sha1,Hmac_sha224,Hmac_sha256,Hmac_sha384,Hmac_sha512。

Hmac_MD5:

/**
* MD5(Key XOR opad, MD5(Key XOR ipad, text))
*
* where K is an n byte key
* ipad is the byte 0x36 repeated 64 times
* opad is the byte 0x5c repeated 64 times
* and text is the data being protected(maybe user or password).
*/ 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
MD5Init(&context); /* init context for 1st pass */
MD5Update(&context, k_ipad, KEY_IOPAD_SIZE); /* start with inner pad */
MD5Update(&context, (unsigned char*)text, text_len); /* then text of datagram */
MD5Final(hmac, &context); /* finish up 1st pass */ // perform outer MD5
MD5Init(&context); /* init context for 2nd pass */
MD5Update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
MD5Update(&context, hmac, MD5_DIGEST_SIZE); /* then results of 1st hash */
MD5Final(hmac, &context); /* finish up 2nd pass */

Hmac_sha1:

    /**
* SHA(Key XOR opad, SHA(Key XOR ipad, text))
*
* where K is an n byte key
* ipad is the byte 0x36 repeated 64 times
* opad is the byte 0x5c repeated 64 times
* and text is the data being protected(maybe user or password).
*/ 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 SHA
SHA_Init(&context); /* init context for 1st pass */
SHA_Bytes(&context, k_ipad, KEY_IOPAD_SIZE); /* start with inner pad */
SHA_Bytes(&context, text, text_len); /* then text of datagram */
SHA_Final(&context, hmac); /* finish up 1st pass */ // perform outer SHA
SHA_Init(&context); /* init context for 2nd pass */
SHA_Bytes(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
SHA_Bytes(&context, hmac, SHA1_DIGEST_SIZE); /* then results of 1st hash */
SHA_Final(&context, hmac); /* finish up 2nd pass */

Hmac_sha224,mac_sha256 和 Hmac_sh啊类似,把SHA换成SHA224或SHA256即可,注意 ipad和opad的长度为64.

Hmac_sha384:Hmac_sha512和Hmac_sha384类似,把SHA384换成SHA512即可,注意 ipad和opad的长度为128.

    /**
* SHA384(Key XOR opad, SHA(Key XOR ipad, text))
*
* where K is an n byte key
* ipad is the byte 0x36 repeated 128 times
* opad is the byte 0x5c repeated 128 times
* and text is the data being protected(maybe user or password).
*/ 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_SIZE128; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
} // perform inner SHA384
SHA384_Init(&context); /* init context for 1st pass */
SHA384_Bytes(&context, k_ipad, KEY_IOPAD_SIZE128); /* start with inner pad */
SHA384_Bytes(&context, text, text_len); /* then text of datagram */
SHA384_Final(&context, hmac); /* finish up 1st pass */ // perform outer SHA384
SHA384_Init(&context); /* init context for 2nd pass */
SHA384_Bytes(&context, k_opad, KEY_IOPAD_SIZE128); /* start with outer pad */
SHA384_Bytes(&context, hmac, SHA384_DIGEST_SIZE); /* then results of 1st hash */
SHA384_Final(&context, hmac); /* finish up 2nd pass */

  

C implement at github: https://github.com/mygityf/cipher/blob/master/cipher/hmac.c

Done.

HMac基本介绍的更多相关文章

  1. MAC与HMAC的介绍及其在AWS和Azure中的应用

    MAC 在密码学中,(消息认证码)Message Authentication Code是用来认证消息的比较短的信息.换言之,MAC用来保证消息的数据完整性和消息的数据源认证. MAC由消息本身和一个 ...

  2. Python之数据加密与解密及相关操作(hashlib模块、hmac模块、random模块、base64模块、pycrypto模块)

    本文内容 数据加密概述 Python中实现数据加密的模块简介 hashlib与hmac模块介绍 random与secrets模块介绍 base64模块介绍 pycrypto模块介绍 总结 参考文档 提 ...

  3. Python之数据加密与解密及相关操作(hashlib、hmac、random、base64、pycrypto)

    本文内容 数据加密概述 Python中实现数据加密的模块简介 hashlib与hmac模块介绍 random与secrets模块介绍 base64模块介绍 pycrypto模块介绍 总结 参考文档 提 ...

  4. 简要介绍BASE64、MD5、SHA、HMAC几种方法。

    加密解密,曾经是我一个毕业设计的重要组件.在工作了多年以后回想当时那个加密.解密算法,实在是太单纯了.     言归正传,这里我们主要描述Java已经实现的一些加密解密算法,最后介绍数字证书.     ...

  5. python 全栈开发,Day36(作业讲解(大文件下载以及进度条展示),socket的更多方法介绍,验证客户端链接的合法性hmac,socketserver)

     先来回顾一下昨天的内容 黏包现象粘包现象的成因 : tcp协议的特点 面向流的 为了保证可靠传输 所以有很多优化的机制 无边界 所有在连接建立的基础上传递的数据之间没有界限 收发消息很有可能不完全相 ...

  6. 本篇内容简要介绍BASE64、MD5、SHA、HMAC几种加密算法。

    BASE64编码算法不算是真正的加密算法.     MD5.SHA.HMAC这三种加密算法,可谓是非可逆加密,就是不可解密的加密方法,我们称之为单向加密算法.我们通常只把他们作为加密的基础.单纯的以上 ...

  7. When I see you again(加密原理介绍,代码实现DES、AES、RSA、Base64、MD5)

    关于网络安全的数据加密部分,本来打算总结一篇博客搞定,没想到东西太多,这已是第三篇了,而且这篇写了多次,熬了多次夜,真是again and again.起个名字:数据加密三部曲,前两部链接如下: 整体 ...

  8. 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希

    据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...

  9. 加密算法中BASE64、MD5、SHA、HMAC等之间的区别

    http://blog.csdn.net/lplj717/article/details/51828692 根据项目需要了解了一下几种加密算法(参考其他博客),内容简要介绍BASE64.MD5.SHA ...

随机推荐

  1. 洛谷P1262 间谍网络[强连通分量 BFS]

    题目描述 由于外国间谍的大量渗入,国家安全正处于高度的危机之中.如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B.有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手中掌握的全部情报 ...

  2. AC日记——校门外的树(增强版) 洛谷 P1276

    题目描述 校门外马路上本来从编号0到L,每一编号的位置都有1棵树.有砍树者每次从编号A到B处连续砍掉每1棵树,就连树苗也不放过(记 0 A B ,含A和B):幸运的是还有植树者每次从编号C到D 中凡是 ...

  3. unity 绘制三角形

    哎 该学的还是要学 参考:http://www.narkii.com/club/thread-369573-1.html unity 顶点绘制三角形 脚本绘制; 其实filter和render就是进行 ...

  4. nvl函数 oracle

    Oracle中函数以前介绍的字符串处理,日期函数,数学函数,以及转换函数等等,还有一类函数是通用函数.主要有:NVL,NVL2,NULLIF,COALESCE,这几个函数用在各个类型上都可以. 下面简 ...

  5. angular学习笔记(二十九)-$q服务

    angular中的$q是用来处理异步的(主要当然是http交互啦~). $q采用的是promise式的异步编程.什么是promise异步编程呢? 异步编程最重要的核心就是回调,因为有回调函数,所以才构 ...

  6. java线程跟多线程

    java创建线程两种方式: 1.继承Thread创建线程 /** * Created by lsf on 16/4/18. */ class NewThread extends Thread { Ne ...

  7. Cordova Ionic AngularJS

    实践分享:开始用Cordova+Ionic+AngularJS开发App http://www.cocoachina.com/webapp/20150707/12395.html

  8. Learning to rank 介绍

    PS:文章主要转载自CSDN大神hguisu的文章"机器学习排序":          http://blog.csdn.net/hguisu/article/details/79 ...

  9. Django用已有的数据库

    python mysite/manage.py inspectdb  会按照你的数据库生成Model  拷贝进去用就可以了!

  10. 解决:笔记本安装mint18时,安装界面显示不全

    近日在给自己的笔记本安装mint18时,安装界面显示不全,就是安装时到了分区界面后看不到下一步. 很无奈.... 于是胡乱摸索,得到解决的办法. 按住键盘上的ALT键,用鼠标向上拖动安装的界面,最好是 ...