資源簡介
阿里云設(shè)備登錄一機(jī)一密和一型一密都需要用到HMAC-MD5算法
什么是 HMAC-MD5?
1、比如你和對方共享了一個密鑰K,現(xiàn)在你要發(fā)消息給對方,既要保證消息沒有被篡改,又要能證明信息確實是你本人發(fā)的,那么就把原信息和使用K計算的HMAC的值一起發(fā)過去。對方接到之后,使用自己手中的K把消息計算一下HMAC,如果和你發(fā)送的HMAC一致,那么可以認(rèn)為這個消息既沒有被篡改也沒有冒充。
2、MD5就是通過散列對要輸出的數(shù)據(jù)進(jìn)行摘要,接收到數(shù)據(jù)時,再同樣進(jìn)行MD5散列,與給定的MD5散列值比較,一致不一致就很清楚了。通常來說,傳輸?shù)臄?shù)據(jù)和MD5是不同的渠道給出的,比如網(wǎng)頁上顯示MD5,下載鏈接是某個鏡像網(wǎng)站的。如果要通過同一個渠道發(fā)送數(shù)據(jù)和散列值的話(比如消息認(rèn)證碼),就要考慮數(shù)據(jù)和MD5同時被篡改的問題,如果第三方修改了數(shù)據(jù),然后進(jìn)行MD5散列,并一塊發(fā)給接收方,接收方并不能察覺到數(shù)據(jù)被篡改。HMAC-MD5就可以用一把發(fā)送方和接收方都有的key進(jìn)行計算,而沒有這把key的第三方是無法計算出正確的散列值的,這樣就可以防止數(shù)據(jù)被篡改。

代碼片段和文件信息
#include?“md5.h“
/*????The?below?was?retrieved?from
?*????http://www.openbsd.org/cgi-bin/cvsweb/~checkout~/src/sys/crypto/md5.c?rev=1.1
?*????with?the?following?changes:
?*????#includes?commented?out.
?*????Support?context->count?as?unsigned?int[2]?instead?of?uint64_t
?*????u_int*?to?uint*
?*/
/*
?*?This?code?implements?the?MD5?message-digest?algorithm.
?*?The?algorithm?is?due?to?Ron?Rivest.????This?code?was
?*?written?by?Colin?Plumb?in?1993?no?copyright?is?claimed.
?*?This?code?is?in?the?public?domain;?do?with?it?what?you?wish.
?*
?*?Equivalent?code?is?available?from?RSA?Data?Security?Inc.
?*?This?code?has?been?tested?against?that?and?is?equivalent
?*?except?that?you?don‘t?need?to?include?two?pages?of?legalese
?*?with?every?copy.
?*
?*?To?compute?the?message?digest?of?a?chunk?of?bytes?declare?an
?*?MD5Context?structure?pass?it?to?MD5Init?call?MD5Update?as
?*?needed?on?buffers?full?of?bytes?and?then?call?MD5Final?which
?*?will?fill?a?supplied?16-byte?array?with?the?digest.
?*/
/*#include?*/
/*#include?*/
/*#include?*/
#define?PUT_64BIT_LE(cp?value)?do?{????????????????\
????(cp)[7]?=?(value)[1]?>>?24;????????????????????\
????(cp)[6]?=?(value)[1]?>>?16;????????????????????\
????(cp)[5]?=?(value)[1]?>>?8;????????????????????\
????(cp)[4]?=?(value)[1];????????????????????????\
????(cp)[3]?=?(value)[0]?>>?24;????????????????????\
????(cp)[2]?=?(value)[0]?>>?16;????????????????????\
????(cp)[1]?=?(value)[0]?>>?8;????????????????????\
????(cp)[0]?=?(value)[0];?}?while?(0)
#define?PUT_32BIT_LE(cp?value)?do?{????????????????????\
????(cp)[3]?=?(value)?>>?24;????????????????????\
????(cp)[2]?=?(value)?>>?16;????????????????????\
????(cp)[1]?=?(value)?>>?8;????????????????????????\
????(cp)[0]?=?(value);?}?while?(0)
static?unsigned?char?PADDING[MD5_BLOCK_LENGTH]?=?{
????0x80?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0
????0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0
????0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0?0
};
/*
?*?Start?MD5?accumulation.??Set?bit?count?to?0?and?buffer?to?mysterious
?*?initialization?constants.
?*/
void
MD5Init(MD5_CTX?*ctx)
{
????ctx->count[0]?=?0;
????ctx->count[1]?=?0;
????ctx->state[0]?=?0x67452301;
????ctx->state[1]?=?0xefcdab89;
????ctx->state[2]?=?0x98badcfe;
????ctx->state[3]?=?0x10325476;
}
/*
?*?Update?context?to?reflect?the?concatenation?of?another?buffer?full
?*?of?bytes.
?*/
void
MD5Update(MD5_CTX?*ctx?unsigned?char?const?*input?size_t?len)
{
????size_t?have?need;
????/*?Check?how?many?bytes?we?already?have?and?how?many?more?we?need.?*/
????have?=?(size_t)((ctx->count[0]?>>?3)?&?(MD5_BLOCK_LENGTH?-?1));
????need?=?MD5_BLOCK_LENGTH?-?have;
????/*?Update?bitcount?*/
/*????ctx->count?+=?(uint64_t)len?<3;*/
????if?((ctx->count[0]?+=?((unsigned?int)len?<3))?(unsigned?int)len)?{
????/*?Overflowed?ctx->count[0]?*/
????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3005??2019-04-09?20:13??md5.h
?????文件??????12029??2019-04-09?20:10??md5.c
-----------?---------??----------?-----??----
????????????????15034????????????????????2
- 上一篇:IC卡水表程序
- 下一篇:峰值檢測系統(tǒng)課程設(shè)計
評論
共有 條評論