快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

1593太阳集团城所有网址:SHA1和MD5算法详解和C++源码



1 吐槽一下

近来在收拾一些代码,发明自己的库里面缺少一些HASH的的代码,于是抉择移植一套代码进来,原先觉得是个极其轻松的工作,结果却搞的小小蛋痛了一把。很多开源代码都有一点杂乱。

移植历程代码主要参考过rhash这个库,好处是后面发明,着实辛亏参考的是这套库。后面发明其他库,在某些环节陷得更深,这套库在某些程度重构过。当然此库的小bug也不算少,比如冗余代码,某些地方字节序处置惩罚差错等。

原先以为拖几个代码进来,迅速搞掂的一件工作,结果发明,很多地方看不懂,不明究理,我稀里糊涂的看了1天多,同时参考了4-5个库的代码(着实有点越参考越糊涂),着末抉择看懂算法再着手。

我小我总结这些代码这样难以看懂的缘故原由大年夜致如下:

大年夜体大年夜产业年可以参考的代码有几套,(可以看出流派区别),RSA的代码, openssl的代码等,这些代码昔时预计预计来自很多半学家,数学家很多时刻写的代码不具备可读性,比如大年夜部分算法里眼前面BLOCK先调用xxx_update函数,后面调用着末几个BLOCK处置惩罚的xxx_final函数,,但xxx_final函数里面又调用xxx_update函数,以是upadate函数就有处置惩罚2种环境的代码,让整体代码思路乖乖的,可能数学家他们太聪清楚明了,思维可以多路径化。而今朝的代码多是在这些根基上改进的。很多着手改的人也没有真正理解问题,就动了手,结果很多代码反而让我这种吹毛求疵的疑心。比如早期机械的字节序预计都是一种(BE),而后面的改进历程,字节序的问题逐步浮现,而很多篡改并不完全理解道理和初衷,结果代码就改的的有点乱了。

别的,很多书和阐明,对付MD5,SHA1算法的阐明都很含糊,比如《利用密码学》里面对付SHA1的每次处置惩罚的块BLOCK只有一句话描述,和MD5一样,但实际呢?SHA1算法里面的数据都是用BE编码的(着末一个长度也要求用BE款式), 而MD5算法内部数据是LE,这些含糊的阐明也造成了理解的苦楚。

着末在rhash和维基的赞助下,完成了代码。厚着面皮说我的代码实现敢说是今朝MD5,SHA1算法中写的最清晰的一套之一,至少我看懂了MD5,SHA1的BLOCK数据处置惩罚部分了,才动的手。

2 SHA1和MD5的算法阐明

SHA1和MD5的算法都是从MD4算法改进而来的2种算法,基础思路都是将信息分成N个分组,每组64个字节,每个分组都进行择要运算。当一个分组的择要运算完毕后,将上一个分组的结果也用于下一个分组的运算。

信息的长度(留意是bit位长度,不是字节长度)用64位表示,也要参加信息择要运算,而且是放在着末一个分组的末端,以是长度信息要盘踞8个字节。

假如信息数据着末一个分组长度小于64个字节,在后面添加0x80标志停止,假云云时数据+停止标志已经<=56个字节,还可以放入长度数据,就在停止标志到第56个字节补0,然后放入长度,假云云时信息数据+停止标志已经大年夜于56字节,那么这个分组后面补0,进行一次择要运算,然后再建立一个分组,前面整个补0,着末16个字节放长度,再进行一次择要。

必要留意的地方如下。

MD5着末天生的择要信息是16个字节,SHA1是20个字节。

MD5和SHA1的分组信息运算,分组里面的的数据都邑被视为16个DWORD,而MD5算法觉得这些DWORD的字节序列是LITTLE-ENDIAN,而SHA1的算法觉得DWORD是BIG-ENDIAN的。以是在不合字节序的主机上要进行转换。

放入着末一个分组的长度信息,是原始数据长度,而且是BIT位长度,其是一个uint64_t,而MD5算法要求放入的长度是LITTLE-ENDIAN的,而SHA1算轨则要求这个长度是BIG-ENDIAN的。不合的平台要进行转换。

当然天生的结果,MD5也要求是LITTLE-ENDIAN,SHA1也要求结果是BIG-ENDIAN的,不合的平台照样要进行转换。

我们贴几个择要处置惩罚历程的分组信息,赞助大年夜家理解。假如要处置惩罚的数据是3个字节字符串”abc”,其在MD5的算法中,只必要一个分组参加,数据是16进制,如下:

61 62 63 80 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00

而SHA1算法中,也只有一个分组,如下,大年夜家留意长度位置上的区别。十六进制的18标识24个bit3个字节。

61 62 63 80 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18

假如要处置惩罚的数据是80个字节的"12345678901234567890123456789012345678901234567890123456789012345678901234567890",其在MD5的算法会被分成2个分组,

第一个分组如下,

31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36

37 38 39 30 31 32 33 34 35 36 37 38 39 30 31 32

33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38

39 30 31 32 33 34 35 36 37 38 39 30 31 32 33 34

第二个分组如下

35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30

80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00 80 02 00 00 00 00 00 00

3 上源码

好了,不罗嗦了,直接上代码,包管清晰可读,注释量足!

为了大年夜家方便,我把代码放入一个文件,在VS2012编译测试经由过程。

#include

#include#include

#include

//字节序的小头和大年夜头的问题#define ZEN_LITTLE_ENDIAN0x0123

#define ZEN_BIG_ENDIAN0x3210

//今朝所有的代码都是为了小头党办事的,不知道有生之年这套代码是否还会为大年夜头党办事一次?#ifndef ZEN_BYTES_ORDER

#define ZEN_BYTES_ORDERZEN_LITTLE_ENDIAN#endif

#ifndef ZEN_SWAP_UINT16

#define ZEN_SWAP_UINT16(x)((((x) & 0xff00) >>8) | (((x) & 0x00ff)#endif

#ifndef ZEN_SWAP_UINT32#define ZEN_SWAP_UINT32(x)((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>8) | \

(((x) & 0x0000ff00)#endif

#ifndef ZEN_SWAP_UINT64#define ZEN_SWAP_UINT64(x)((((x) & 0xff00000000000000) >> 56) | (((x) & 0x00ff000000000000) >>40) | \

(((x) & 0x0000ff0000000000) >> 24) | (((x) & 0x000000ff00000000) >>8) | \(((x) & 0x00000000ff000000)

(((x) & 0x000000000000ff00)#endif

//将一个(字符串)数组,拷贝到别的一个uint32_t数组,同时每个uint32_t反字节序

void *swap_uint32_memcpy(void *to, const void *from, size_t length){

memcpy(to, from, length);size_t remain_len =(4 - (length & 3)) & 3;

//数据不是4字节的倍数,弥补0

if (remain_len){

for (size_t i = 0; i{

*((char *)(to) + length + i) = 0;}

//调剂成4的倍数length += remain_len;

}

//所有的数据反转for (size_t i = 0; i

{((uint32_t *)to)[i] = ZEN_SWAP_UINT32(((uint32_t *)to)[i]);

}

return to;}

///MD5的结果数据长度

static const size_t ZEN_MD5_HASH_SIZE= 16;///SHA1的结果数据长度

static const size_t ZEN_SHA1_HASH_SIZE= 20;

namespace ZEN_LIB{

/*!@brief求某个内存块的MD5,

@returnunsigned char* 返回的的结果,@param[in]buf求MD5的内存BUFFER指针

@param[in]sizeBUFFER长度@param[out] result 结果

*/unsigned char *md5(const unsigned char *buf,

size_t size,unsigned char result[ZEN_MD5_HASH_SIZE]);

/*!@brief求内存块BUFFER的SHA1值

@returnunsigned char* 返回的的结果@param[in]buf求SHA1的内存BUFFER指针

@param[in]sizeBUFFER长度@param[out] result 结果

*/unsigned char *sha1(const unsigned char *buf,

size_t size,unsigned char result[ZEN_SHA1_HASH_SIZE]);

};

//================================================================================================

//MD5的算法

//每次处置惩罚的BLOCK的大年夜小static const size_t ZEN_MD5_BLOCK_SIZE = 64;

//md5算法的高低文,保存一些状态,中心数据,结果

typedef struct md5_ctx{

//处置惩罚的数据的长度uint64_t length_;

//还没有处置惩罚的数据长度uint64_t unprocessed_;

//取得的HASH结果(中心数据)uint32_thash_[4];

} md5_ctx;

#define ROTL32(dword, n) ((dword) > (32 - (n))))

#define ROTR32(dword, n) ((dword) >> (n) ^ ((dword)#define ROTL64(qword, n) ((qword) > (64 - (n))))

#define ROTR64(qword, n) ((qword) >> (n) ^ ((qword)

/*!

@brief内部函数,初始化MD5的context,内容@paramctx

*/static void zen_md5_init(md5_ctx *ctx)

{ctx->length_ = 0;

ctx->unprocessed_ = 0;

/* initialize state */ctx->hash_[0] = 0x67452301;

ctx->hash_[1] = 0xefcdab89;ctx->hash_[2] = 0x98badcfe;

ctx->hash_[3] = 0x10325476;}

/* First, define four auxiliary functions that each take as input

* three 32-bit words and returns a 32-bit word.*/

/* F(x,y,z) = ((y XOR z) AND x) XOR z - is faster then original version */#define MD5_F(x, y, z) ((((y) ^ (z)) & (x)) ^ (z))

#define MD5_G(x, y, z) (((x) & (z)) | ((y) & (~z)))#define MD5_H(x, y, z) ((x) ^ (y) ^ (z))

#define MD5_I(x, y, z) ((y) ^ ((x) | (~z)))

/* transformations for rounds 1, 2, 3, and 4. */#define MD5_ROUND1(a, b, c, d, x, s, ac) { \

(a) += MD5_F((b), (c), (d)) + (x) + (ac); \(a) = ROTL32((a), (s)); \

(a) += (b); \}

#define MD5_ROUND2(a, b, c, d, x, s, ac) { \(a) += MD5_G((b), (c), (d)) + (x) + (ac); \

(a) = ROTL32((a), (s)); \(a) += (b); \

}#define MD5_ROUND3(a, b, c, d, x, s, ac) { \

(a) += MD5_H((b), (c), (d)) + (x) + (ac); \(a) = ROTL32((a), (s)); \

(a) += (b); \}

#define MD5_ROUND4(a, b, c, d, x, s, ac) { \(a) += MD5_I((b), (c), (d)) + (x) + (ac); \

(a) = ROTL32((a), (s)); \(a) += (b); \

}

/*!

@brief内部函数,将64个字节,16个uint32_t的数组进行择要(杂凑)处置惩罚,处置惩罚的数据自己序是小头数据@paramstate 寄放处置惩罚的hash数据结果

@paramblock 要处置惩罚的block,64个字节,16个uint32_t的数组*/

static void zen_md5_process_block(uint32_t state[4], const uint32_t block[ZEN_MD5_BLOCK_SIZE / 4]){

register unsigned a, b, c, d;a = state[0];

b = state[1];c = state[2];

d = state[3];

const uint32_t *x = NULL;

//MD5里面谋略的数据都是小头数据.大年夜头党的数据要处置惩罚#if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIAN

x = block;#else

uint32_t swap_block[ZEN_MD5_BLOCK_SIZE / 4];swap_uint32_memcpy(swap_block, block, 64);

x = swap_block;#endif

MD5_ROUND1(a, b, c, d, x[ 0],7, 0xd76aa478);MD5_ROUND1(d, a, b, c, x[ 1], 12, 0xe8c7b756);

MD5_ROUND1(c, d, a, b, x[ 2], 17, 0x242070db);MD5_ROUND1(b, c, d, a, x[ 3], 22, 0xc1bdceee);

MD5_ROUND1(a, b, c, d, x[ 4],7, 0xf57c0faf);MD5_ROUND1(d, a, b, c, x[ 5], 12, 0x4787c62a);

MD5_ROUND1(c, d, a, b, x[ 6], 17, 0xa8304613);MD5_ROUND1(b, c, d, a, x[ 7], 22, 0xfd469501);

MD5_ROUND1(a, b, c, d, x[ 8],7, 0x698098d8);MD5_ROUND1(d, a, b, c, x[ 9], 12, 0x8b44f7af);

MD5_ROUND1(c, d, a, b, x[10], 17, 0xffff5bb1);MD5_ROUND1(b, c, d, a, x[11], 22, 0x895cd7be);

MD5_ROUND1(a, b, c, d, x[12],7, 0x6b901122);MD5_ROUND1(d, a, b, c, x[13], 12, 0xfd987193);

MD5_ROUND1(c, d, a, b, x[14], 17, 0xa679438e);MD5_ROUND1(b, c, d, a, x[15], 22, 0x49b40821);

MD5_ROUND2(a, b, c, d, x[ 1],5, 0xf61e2562);

MD5_ROUND2(d, a, b, c, x[ 6],9, 0xc040b340);MD5_ROUND2(c, d, a, b, x[11], 14, 0x265e5a51);

MD5_ROUND2(b, c, d, a, x[ 0], 20, 0xe9b6c7aa);MD5_ROUND2(a, b, c, d, x[ 5],5, 0xd62f105d);

MD5_ROUND2(d, a, b, c, x[10],9,0x2441453);MD5_ROUND2(c, d, a, b, x[15], 14, 0xd8a1e681);

MD5_ROUND2(b, c, d, a, x[ 4], 20, 0xe7d3fbc8);MD5_ROUND2(a, b, c, d, x[ 9],5, 0x21e1cde6);

MD5_ROUND2(d, a, b, c, x[14],9, 0xc33707d6);MD5_ROUND2(c, d, a, b, x[ 3], 14, 0xf4d50d87);

MD5_ROUND2(b, c, d, a, x[ 8], 20, 0x455a14ed);MD5_ROUND2(a, b, c, d, x[13],5, 0xa9e3e905);

MD5_ROUND2(d, a, b, c, x[ 2],9, 0xfcefa3f8);MD5_ROUND2(c, d, a, b, x[ 7], 14, 0x6761593太阳集团城所有网址f02d9);

MD5_ROUND2(b, c, d, a, x[12], 20, 0x8d2a4c8a);

MD5_ROUND3(a, b, c, d, x[ 5],4, 0xfffa3942);MD5_ROUND3(d, a, b, c, x[ 8], 11, 0x8771f681);

MD5_ROUND3(c, d, a, b, x[11], 16, 0x6d9d6122);MD5_ROUND3(b, c, d, a, x[14], 23, 0xfde5380c);

MD5_ROUND3(a, b, c, d, x[ 1],4, 0xa4beea44);MD5_ROUND3(d, a, b, c, x[ 4], 11, 0x4bdecfa9);

MD5_ROUND3(c, d, a, b, x[ 7], 16, 0xf6bb4b60);MD5_ROUND3(b, c, d, a, x[10], 23, 0xbebfbc70);

MD5_ROUND3(a, b, c, d, x[13],4, 0x289b7ec6);MD5_ROUND3(d, a, b, c, x[ 0], 11, 0xeaa127fa);

MD5_ROUND3(c, d, a, b, x[ 3], 16, 0xd4ef3085);MD5_ROUND3(b, c, d, a, x[ 6], 23,0x4881d05);

MD5_ROUND3(a, b, c, d, x[ 9],4, 0xd9d4d039);MD5_ROUND3(d, a, b, c, x[12], 11, 0xe6db99e5);

MD5_ROUND3(c, d, a, b, x[15], 16, 0x1fa27cf8);MD5_ROUND3(b, c, d, a, x[ 2], 23, 0xc4ac5665);

MD5_ROUND4(a, b, c, d, x[ 0],6, 0xf4292244);

MD5_ROUND4(d, a, b, c, x[ 7], 10, 0x432aff97);MD5_ROUND4(c, d, a, b, x[14], 15, 0xab9423a7);

MD5_ROUND4(b, c, d, a, x[ 5], 21, 0xfc93a039);MD5_ROUND4(a, b, c, d, x[12],6, 0x655b59c3);

MD5_ROUND4(d, a, b, c, x[ 3], 10, 0x8f0ccc92);MD5_ROUND4(c, d, a, b, x[10], 15, 0xffeff47d);

MD5_ROUND4(b, c, d, a, x[ 1], 21, 0x85845dd1);MD5_ROUND4(a, b, c, d, x[ 8],6, 0x6fa87e4f);

MD5_ROUND4(d, a, b, c, x[15], 10, 0xfe2ce6e0);MD5_ROUND4(c, d, a, b, x[ 6], 15, 0xa3014314);

MD5_ROUND4(b, c, d, a, x[13], 21, 0x4e0811a1);MD5_ROUND4(a, b, c, d, x[ 4],6, 0xf7537e82);

MD5_ROUND4(d, a, b, c, x[11], 10, 0xbd3af235);MD5_ROUND4(c, d, a, b, x[ 2], 15, 0x2ad7d2bb);

MD5_ROUND4(b, c, d, a, x[ 9], 21, 0xeb86d391);

state[0] += a;state[1] += b;

state[2] += c;state[3] += d;

}

/*!

@brief内部函数,处置惩罚数据的前面部分(>64字节的部分),每次组成一个64字节的block就进行杂凑处置惩罚@param[out] ctx算法的context,用于记录一些处置惩罚的高低文和结果

@param[in]buf处置惩罚的数据,@param[in]size 处置惩罚的数据长度

*/static void zen_md5_update(md5_ctx *ctx, const unsigned char *buf, size_t size)

{//为什么不是=,由于在某些情况下,可以多次调用zen_md5_update,但这种环境,必须包管前面的调用,每次都没有unprocessed_

ctx->length_ += size;

//每个处置惩罚的块都是64字节while (size >= ZEN_MD5_BLOCK_SIZE)

{zen_md5_process_block(ctx->hash_, reinterpret_castconst uint32_t *>(buf));

buf+= ZEN_MD5_BLOCK_SIZE;size -= ZEN_MD5_BLOCK_SIZE;

}

ctx->unprocessed_ = size;}

/*!@brief内部函数,处置惩罚数据的末端部分,我们要拼出着末1个(或者两个)要处置惩罚的BLOCK,加上0x80,加上长度进行处置惩罚

@param[in]ctx算法的context,用于记录一些处置惩罚的高低文和结果@param[in]buf处置惩罚的数据

@param[in]size处置惩罚buffer的长度@param[out] result 返回的结果,

*/static void zen_md5_final(md5_ctx *ctx, const unsigned char *buf, size_t size, unsigned char *result)

{uint32_t message[ZEN_MD5_BLOCK_SIZE / 4];

//保存残剩的数据,我们要拼出着末1个(或者两个)要处置惩罚的块,前面的算法包管了,着末一个块肯定小于64个字节

if (ctx->unprocessed_){

memcpy(message, buf + size - ctx->unprocessed_, static_castsize_t>( ctx->unprocessed_));}

//获得0x80要添加在的位置(在uint32_t 数组中),

uint32_t index = ((uint32_t)ctx->length_ & 63) >> 2;uint32_t shift = ((uint32_t)ctx->length_ & 3) * 8;

//添加0x80进去,并且把余下的空间弥补0

message[index]&= ~(0xFFFFFFFFmessage[index++] ^= 0x80

//假如这个block还无法处置惩罚,其后面的长度无法容纳长度64bit,那么先处置惩罚这个block

if (index > 14){

while (index{

message[index++] = 0;}

zen_md5_process_block(ctx->hash_, message);

index = 0;}

//补0

while (index{

message[index++] = 0;}

//保存长度,留意是bit位的长度,这个问题让我看着愁闷了半天,

uint64_t data_len = (ctx->length_)

//留意MD5算法要求的64bit的长度是小头LITTLE-ENDIAN编码,留意下面的对照是!=#if ZEN_BYTES_ORDER != ZEN_LITTLE_ENDIAN

data_len = Z1593太阳集团城所有网址EN_SWAP_UINT64(data_len);#endif

message[14] = (uint32_t) (data_len & 0x00000000FFFFFFFF);

message[15] = (uint32_t) ((data_len & 0xFFFFFFFF00000000ULL) >> 32);

zen_md5_process_block(ctx->hash_, message);

//留意结果是小头党的,在大年夜头的天下要进行转换#if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIAN

memcpy(result, &ctx->hash_, Z1593太阳集团城所有网址EN_MD5_HASH_SIZE);#else

swap_uint32_memcpy(result, &ctx->hash_, ZEN_MD5_HASH_SIZE);#endif

}

//谋略一个内存数据的MD5值unsigned char *ZEN_LIB::md5(const unsigned char *buf,

size_t size,unsigned char result[ZEN_MD5_HASH_SIZE])

{assert(result != NULL);

md5_ctx ctx;

zen_md5_init(&ctx);zen_md5_update(&ctx, buf, size);

zen_md5_final(&ctx, buf, size, result);return result;

}

//================================================================================================

//SHA1的算法

//每次处置惩罚的BLOCK的大年夜小static const size_t ZEN_SHA1_BLOCK_SIZE = 64;

//SHA1算法的高低文,保存一些状态,中心数据,结果

typedef struct sha1_ctx{

//处置惩罚的数据的长度

uint64_t length_;//还没有处置惩罚的数据长度

uint64_t unprocessed_;/* 160-bit algorithm internal hashing state */

uint32_t hash_[5];} sha1_ctx;

//内部函数,SHA1算法的高低文的初始化

static void zen_sha1_init(sha1_ctx *ctx){

ctx->length_ = 0;ctx->unprocessed_ = 0;

// 初始化算法的几个常量,魔术数ctx->hash_[0] = 0x67452301;

ctx->hash_[1] = 0xefcdab89;ctx->hash_[2] = 0x98badcfe;

ctx->hash_[3] = 0x10325476;ctx->hash_[4] = 0xc3d2e1f0;

}

/*!

@brief内部函数,对一个64bit内存块进行择要(杂凑)处置惩罚,@paramhash寄放谋略hash结果的的数组

@paramblock 要谋略的处置惩罚得内存块*/

static void zen_sha1_process_block(uint32_t hash[5],const uint32_t block[ZEN_SHA1_BLOCK_SIZE / 4])

{size_tt;

uint32_twblock[80];register uint32_ta, b, c, d, e, temp;

//SHA1算法处置惩罚的内部数据要求是大年夜头党的,在小头的情况转换

#if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIANswap_uint32_memcpy(wblock, block, ZEN_SHA1_BLOCK_SIZE);

#else::memcpy(wblock, block, ZEN_SHA1_BLOCK_SIZE);

#endif

//处置惩罚for (t = 16; t

{wblock[t] = ROTL32(wblock[t - 3] ^ wblock[t - 8] ^ wblock[t - 14] ^ wblock[t - 16], 1);

}

a = hash[0];b = hash[1];

c = hash[2];d = hash[3];

e = hash[4];

for (t = 0; t{

/* the following is faster than ((B & C) | ((~B) & D)) */temp =ROTL32(a, 5) + (((c ^ d) & b) ^ d)

+ e + wblock[t] + 0x5A827999;e = d;

d = c;c = ROTL32(b, 30);

b = a;a = temp;

}

for (t = 20; t{

temp = ROTL32(a, 5) + (b ^ c ^ d) + e + wblock[t] + 0x6ED9EBA1;e = d;

d = c;c = ROTL32(b, 30);

b = a;a = temp;

}

for (t = 40; t{

temp = ROTL32(a, 5) + ((b & c) | (b & d) | (c & d))+ e + wblock[t] + 0x8F1BBCDC;

e = d;d = c;

c = ROTL32(b, 30);b = a;

a = temp;}

for (t = 60; t

{temp = ROTL32(a, 5) + (b ^ c ^ d) + e + wblock[t] + 0xCA62C1D6;

e = d;d = c;

c = ROTL32(b, 30);b = a;

a = temp;}

hash[0] += a;

hash[1] += b;hash[2] += c;

hash[3] += d;hash[4] += e;

}

/*!

@brief内部函数,处置惩罚数据的前面部分(>64字节的部分),每次组成一个64字节的block就进行杂凑处置惩罚@paramctx算法的高低文,记录中心数据,结果等

@parammsg要进行谋略的数据buffer@paramsize 长度

*/static void zen_sha1_update(sha1_ctx *ctx,

const unsigned char *buf,size_t size)

{//为了让zen_sha1_update可以多次进入,长度可以累计

ctx->length_ += size;

//每个处置惩罚的块都是64字节while (size 1593太阳集团城所有网址>= ZEN_SHA1_BLOCK_SIZE)

{zen_sha1_process_block(ctx->hash_, reinterpret_castconst uint32_t *>(buf));

buf+= ZEN_SHA1_BLOCK_SIZE;size -= ZEN_SHA1_BLOCK_SIZE;

}

ctx->unprocessed_ = size;}

/*!@brief内部函数,处置惩罚数据的着末部分,添加0x80,补0,增添长度信息

@paramctx算法的高低文,记录中心数据,结果等@parammsg要进行谋略的数据buffer

@paramresult 返回的结果*/

static void zen_sha1_final(sha1_ctx *ctx,const unsigned char *msg,

size_t size,unsigned char *result)

{

uint32_t message[ZEN_SHA1_BLOCK_SIZE / 4];

//保存残剩的数据,我们要拼出着末1个(或者两个)要处置惩罚的块,前面的算法包管了,着末一个块肯定小于64个字节if (ctx->unprocessed_)

{memcpy(message, msg + size - ctx->unprocessed_, static_castsize_t>( ctx->unprocessed_));

}

//获得0x80要添加在的位置(在uint32_t 数组中),uint32_t index = ((uint32_t)ctx->length_ & 63) >> 2;

uint32_t shift = ((uint32_t)ctx->length_ & 3) * 8;

//添加0x80进去,并且把余下的空间弥补0message[index]&= ~(0xFFFFFFFF

message[index++] ^= 0x80

//假如这个block还无法处置惩罚,其后面的长度无法容纳长度64bit,那么先处置惩罚这个blockif (index > 14)

{while (index

{message[index++] = 0;

}

zen_sha1_process_block(ctx->hash_, message);index = 0;

}

//补0while (index

{message[index++] = 0;

}

//保存长度,留意是bit位的长度,这个问题让我看着愁闷了半天,uint64_t data_len = (ctx->length_)

//留意SHA1算法要求的64bit的长度是大年夜头BIG-ENDIAN,在小头的天下要进行转换

#if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIANdata_len = ZEN_SWAP_UINT64(data_len);

#endif

message[14] = (uint32_t) (data_len & 0x00000000FFFFFFFF);message[15] = (uint32_t) ((data_len & 0xFFFFFFFF00000000ULL) >> 32);

zen_sha1_process_block(ctx->hash_, message);

//留意结果是大年夜头党的,在小头的天下要进行转换

#if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIANswap_uint32_memcpy(result, &ctx->hash_, ZEN_SHA1_HASH_SIZE);

#elsememcpy(result, &ctx->hash_, ZEN_SHA1_HASH_SIZE);

#endif}

//谋略一个内存数据的SHA1值

unsigned char *ZEN_LIB::sha1(const unsigned char *msg,size_t size,

unsigned char result[ZEN_SHA1_HASH_SIZE]){

assert(result != NULL);

sha1_ctx ctx;zen_sha1_init(&ctx);

zen_sha1_update(&ctx, msg, size);zen_sha1_final(&ctx, msg, size, result);

return result;}

int main(int /*argc*/, char * /*argv*/[])

{

int ret = 0;static unsigned char test_buf[7][81] =

{{ "" },

{ "a" },{ "abc" },

{ "message digest" },{ "abcdefghijklmnopqrstuvwxyz" },

{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },{ "12345678901234567890123456789012345678901234567890123456789012345678901234567890" }

};

static const size_t test_buflen[7] ={

0, 1, 3, 14, 26, 62, 80};

static const unsigned char md5_test_sum[7][16] =

{{ 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },

{ 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },{ 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },

{ 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },{ 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },

{ 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },{ 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }

};unsigned char result[32] ={0};

for(size_t i=0;i

{ZEN_LIB::md5(test_buf[i],test_buflen[i],result);

ret = memcmp(result,md5_test_sum[i],16);if (ret != 0)

{assert(false);

}}

static const unsigned char sha1_test_sum[7][20] =

{{ 0xda,0x39,0xa3,0xee,0x5e,0x6b,0x4b,0x0d,0x32,0x55,0xbf,0xef,0x95,0x60,0x18,0x90,0xaf,0xd8,0x07,0x09 },

{ 0x86,0xf7,0xe4,0x37,0xfa,0xa5,0xa7,0xfc,0xe1,0x5d,0x1d,0xdc,0xb9,0xea,0xea,0xea,0x37,0x76,0x67,0xb8 },{ 0xa9,0x99,0x3e,0x36,0x47,0x06,0x81,0x6a,0xba,0x3e,0x25,0x71,0x78,0x50,0xc2,0x6c,0x9c,0xd0,0xd8,0x9d },

{ 0xc1,0x22,0x52,0xce,0xda,0x8b,0xe8,0x99,0x4d,0x5f,0xa0,0x29,0x0a,0x47,0x23,0x1c,0x1d,0x16,0xaa,0xe3 },{ 0x32,0xd1,0x0c,0x7b,0x8c,0xf9,0x65,0x70,0xca,0x04,0xce,0x37,0xf2,0xa1,0x9d,0x84,0x24,0x0d,0x3a,0x89 },

{ 0x76,0x1c,0x45,0x7b,0xf7,0x3b,0x14,0xd2,0x7e,0x9e,0x92,0x65,0xc4,0x6f,0x4b,0x4d,0xda,0x11,0xf9,0x40 },{ 0x50,0xab,0xf5,0x70,0x6a,0x15,0x09,0x90,0xa0,0x8b,0x2c,0x5e,0xa4,0x0f,0xa0,0xe5,0x85,0x51593太阳集团城所有网址5,0x47,0x32 },

};for(size_t i=0;i

{ZEN_LIB::sha1(test_buf[i],test_buflen[i],result);

ret = memcmp(result,sha1_test_sum[i],20);if (ret != 0)

{assert(false);

}}

return 0;}

rhashlib采纳的协议是MIT,在此再次谢谢原本的作者,别的维基上面的伪代码赞助异常大年夜。

转自:http://www.cnblogs.com/fullsail/archive/2013/02/22/2921505.html

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: