md5和sha1.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. //#include <stdint.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. typedef unsigned int uint32_t;
  7. typedef unsigned long long uint64_t;
  8. //字节序的小头和大头的问题
  9. #define ZEN_LITTLE_ENDIAN 0x0123
  10. #define ZEN_BIG_ENDIAN 0x3210
  11. //目前所有的代码都是为了小头党服务的,不知道有生之年这套代码是否还会为大头党服务一次?
  12. #ifndef ZEN_BYTES_ORDER
  13. #define ZEN_BYTES_ORDER ZEN_LITTLE_ENDIAN
  14. #endif
  15. #ifndef ZEN_SWAP_UINT16
  16. #define ZEN_SWAP_UINT16(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
  17. #endif
  18. #ifndef ZEN_SWAP_UINT32
  19. #define ZEN_SWAP_UINT32(x) ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  20. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  21. #endif
  22. #ifndef ZEN_SWAP_UINT64
  23. #define ZEN_SWAP_UINT64(x) ((((x) & 0xff00000000000000) >> 56) | (((x) & 0x00ff000000000000) >> 40) | \
  24. (((x) & 0x0000ff0000000000) >> 24) | (((x) & 0x000000ff00000000) >> 8) | \
  25. (((x) & 0x00000000ff000000) << 8 ) | (((x) & 0x0000000000ff0000) << 24) | \
  26. (((x) & 0x000000000000ff00) << 40 ) | (((x) & 0x00000000000000ff) << 56))
  27. #endif
  28. //将一个(字符串)数组,拷贝到另外一个uint32_t数组,同时每个uint32_t反字节序
  29. void *swap_uint32_memcpy(void *to, const void *from, size_t length)
  30. {
  31. memcpy(to, from, length);
  32. size_t remain_len = (4 - (length & 3)) & 3;
  33. //数据不是4字节的倍数,补充0
  34. if (remain_len)
  35. {
  36. for (size_t i = 0; i < remain_len; ++i)
  37. {
  38. *((char *)(to) + length + i) = 0;
  39. }
  40. //调整成4的倍数
  41. length += remain_len;
  42. }
  43. //所有的数据反转
  44. for (size_t i = 0; i < length / 4; ++i)
  45. {
  46. ((uint32_t *)to)[i] = ZEN_SWAP_UINT32(((uint32_t *)to)[i]);
  47. }
  48. return to;
  49. }
  50. ///MD5的结果数据长度
  51. static const size_t ZEN_MD5_HASH_SIZE = 16;
  52. ///SHA1的结果数据长度
  53. static const size_t ZEN_SHA1_HASH_SIZE = 20;
  54. namespace ZEN_LIB
  55. {
  56. /*!
  57. @brief 求某个内存块的MD5,
  58. @return unsigned char* 返回的的结果,
  59. @param[in] buf 求MD5的内存BUFFER指针
  60. @param[in] size BUFFER长度
  61. @param[out] result 结果
  62. */
  63. unsigned char *md5(const unsigned char *buf,
  64. size_t size,
  65. unsigned char result[ZEN_MD5_HASH_SIZE]);
  66. /*!
  67. @brief 求内存块BUFFER的SHA1值
  68. @return unsigned char* 返回的的结果
  69. @param[in] buf 求SHA1的内存BUFFER指针
  70. @param[in] size BUFFER长度
  71. @param[out] result 结果
  72. */
  73. unsigned char *sha1(const unsigned char *buf,
  74. size_t size,
  75. unsigned char result[ZEN_SHA1_HASH_SIZE]);
  76. };
  77. //================================================================================================
  78. //MD5的算法
  79. //每次处理的BLOCK的大小
  80. static const size_t ZEN_MD5_BLOCK_SIZE = 64;
  81. //md5算法的上下文,保存一些状态,中间数据,结果
  82. typedef struct md5_ctx
  83. {
  84. //处理的数据的长度
  85. uint64_t length_;
  86. //还没有处理的数据长度
  87. uint64_t unprocessed_;
  88. //取得的HASH结果(中间数据)
  89. uint32_t hash_[4];
  90. } md5_ctx;
  91. #define ROTL32(dword, n) ((dword) << (n) ^ ((dword) >> (32 - (n))))
  92. #define ROTR32(dword, n) ((dword) >> (n) ^ ((dword) << (32 - (n))))
  93. #define ROTL64(qword, n) ((qword) << (n) ^ ((qword) >> (64 - (n))))
  94. #define ROTR64(qword, n) ((qword) >> (n) ^ ((qword) << (64 - (n))))
  95. /*!
  96. @brief 内部函数,初始化MD5的context,内容
  97. @param ctx
  98. */
  99. static void zen_md5_init(md5_ctx *ctx)
  100. {
  101. ctx->length_ = 0;
  102. ctx->unprocessed_ = 0;
  103. /* initialize state */
  104. ctx->hash_[0] = 0x67452301;
  105. ctx->hash_[1] = 0xefcdab89;
  106. ctx->hash_[2] = 0x98badcfe;
  107. ctx->hash_[3] = 0x10325476;
  108. }
  109. /* First, define four auxiliary functions that each take as input
  110. * three 32-bit words and returns a 32-bit word.*/
  111. /* F(x,y,z) = ((y XOR z) AND x) XOR z - is faster then original version */
  112. #define MD5_F(x, y, z) ((((y) ^ (z)) & (x)) ^ (z))
  113. #define MD5_G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  114. #define MD5_H(x, y, z) ((x) ^ (y) ^ (z))
  115. #define MD5_I(x, y, z) ((y) ^ ((x) | (~z)))
  116. /* transformations for rounds 1, 2, 3, and 4. */
  117. #define MD5_ROUND1(a, b, c, d, x, s, ac) { \
  118. (a) += MD5_F((b), (c), (d)) + (x) + (ac); \
  119. (a) = ROTL32((a), (s)); \
  120. (a) += (b); \
  121. }
  122. #define MD5_ROUND2(a, b, c, d, x, s, ac) { \
  123. (a) += MD5_G((b), (c), (d)) + (x) + (ac); \
  124. (a) = ROTL32((a), (s)); \
  125. (a) += (b); \
  126. }
  127. #define MD5_ROUND3(a, b, c, d, x, s, ac) { \
  128. (a) += MD5_H((b), (c), (d)) + (x) + (ac); \
  129. (a) = ROTL32((a), (s)); \
  130. (a) += (b); \
  131. }
  132. #define MD5_ROUND4(a, b, c, d, x, s, ac) { \
  133. (a) += MD5_I((b), (c), (d)) + (x) + (ac); \
  134. (a) = ROTL32((a), (s)); \
  135. (a) += (b); \
  136. }
  137. /*!
  138. @brief 内部函数,将64个字节,16个uint32_t的数组进行摘要(杂凑)处理,处理的数据自己序是小头数据
  139. @param state 存放处理的hash数据结果
  140. @param block 要处理的block,64个字节,16个uint32_t的数组
  141. */
  142. static void zen_md5_process_block(uint32_t state[4], const uint32_t block[ZEN_MD5_BLOCK_SIZE / 4])
  143. {
  144. register unsigned a, b, c, d;
  145. a = state[0];
  146. b = state[1];
  147. c = state[2];
  148. d = state[3];
  149. const uint32_t *x = NULL;
  150. //MD5里面计算的数据都是小头数据.大头党的数据要处理
  151. #if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIAN
  152. x = block;
  153. #else
  154. uint32_t swap_block[ZEN_MD5_BLOCK_SIZE / 4];
  155. swap_uint32_memcpy(swap_block, block, 64);
  156. x = swap_block;
  157. #endif
  158. MD5_ROUND1(a, b, c, d, x[ 0], 7, 0xd76aa478);
  159. MD5_ROUND1(d, a, b, c, x[ 1], 12, 0xe8c7b756);
  160. MD5_ROUND1(c, d, a, b, x[ 2], 17, 0x242070db);
  161. MD5_ROUND1(b, c, d, a, x[ 3], 22, 0xc1bdceee);
  162. MD5_ROUND1(a, b, c, d, x[ 4], 7, 0xf57c0faf);
  163. MD5_ROUND1(d, a, b, c, x[ 5], 12, 0x4787c62a);
  164. MD5_ROUND1(c, d, a, b, x[ 6], 17, 0xa8304613);
  165. MD5_ROUND1(b, c, d, a, x[ 7], 22, 0xfd469501);
  166. MD5_ROUND1(a, b, c, d, x[ 8], 7, 0x698098d8);
  167. MD5_ROUND1(d, a, b, c, x[ 9], 12, 0x8b44f7af);
  168. MD5_ROUND1(c, d, a, b, x[10], 17, 0xffff5bb1);
  169. MD5_ROUND1(b, c, d, a, x[11], 22, 0x895cd7be);
  170. MD5_ROUND1(a, b, c, d, x[12], 7, 0x6b901122);
  171. MD5_ROUND1(d, a, b, c, x[13], 12, 0xfd987193);
  172. MD5_ROUND1(c, d, a, b, x[14], 17, 0xa679438e);
  173. MD5_ROUND1(b, c, d, a, x[15], 22, 0x49b40821);
  174. MD5_ROUND2(a, b, c, d, x[ 1], 5, 0xf61e2562);
  175. MD5_ROUND2(d, a, b, c, x[ 6], 9, 0xc040b340);
  176. MD5_ROUND2(c, d, a, b, x[11], 14, 0x265e5a51);
  177. MD5_ROUND2(b, c, d, a, x[ 0], 20, 0xe9b6c7aa);
  178. MD5_ROUND2(a, b, c, d, x[ 5], 5, 0xd62f105d);
  179. MD5_ROUND2(d, a, b, c, x[10], 9, 0x2441453);
  180. MD5_ROUND2(c, d, a, b, x[15], 14, 0xd8a1e681);
  181. MD5_ROUND2(b, c, d, a, x[ 4], 20, 0xe7d3fbc8);
  182. MD5_ROUND2(a, b, c, d, x[ 9], 5, 0x21e1cde6);
  183. MD5_ROUND2(d, a, b, c, x[14], 9, 0xc33707d6);
  184. MD5_ROUND2(c, d, a, b, x[ 3], 14, 0xf4d50d87);
  185. MD5_ROUND2(b, c, d, a, x[ 8], 20, 0x455a14ed);
  186. MD5_ROUND2(a, b, c, d, x[13], 5, 0xa9e3e905);
  187. MD5_ROUND2(d, a, b, c, x[ 2], 9, 0xfcefa3f8);
  188. MD5_ROUND2(c, d, a, b, x[ 7], 14, 0x676f02d9);
  189. MD5_ROUND2(b, c, d, a, x[12], 20, 0x8d2a4c8a);
  190. MD5_ROUND3(a, b, c, d, x[ 5], 4, 0xfffa3942);
  191. MD5_ROUND3(d, a, b, c, x[ 8], 11, 0x8771f681);
  192. MD5_ROUND3(c, d, a, b, x[11], 16, 0x6d9d6122);
  193. MD5_ROUND3(b, c, d, a, x[14], 23, 0xfde5380c);
  194. MD5_ROUND3(a, b, c, d, x[ 1], 4, 0xa4beea44);
  195. MD5_ROUND3(d, a, b, c, x[ 4], 11, 0x4bdecfa9);
  196. MD5_ROUND3(c, d, a, b, x[ 7], 16, 0xf6bb4b60);
  197. MD5_ROUND3(b, c, d, a, x[10], 23, 0xbebfbc70);
  198. MD5_ROUND3(a, b, c, d, x[13], 4, 0x289b7ec6);
  199. MD5_ROUND3(d, a, b, c, x[ 0], 11, 0xeaa127fa);
  200. MD5_ROUND3(c, d, a, b, x[ 3], 16, 0xd4ef3085);
  201. MD5_ROUND3(b, c, d, a, x[ 6], 23, 0x4881d05);
  202. MD5_ROUND3(a, b, c, d, x[ 9], 4, 0xd9d4d039);
  203. MD5_ROUND3(d, a, b, c, x[12], 11, 0xe6db99e5);
  204. MD5_ROUND3(c, d, a, b, x[15], 16, 0x1fa27cf8);
  205. MD5_ROUND3(b, c, d, a, x[ 2], 23, 0xc4ac5665);
  206. MD5_ROUND4(a, b, c, d, x[ 0], 6, 0xf4292244);
  207. MD5_ROUND4(d, a, b, c, x[ 7], 10, 0x432aff97);
  208. MD5_ROUND4(c, d, a, b, x[14], 15, 0xab9423a7);
  209. MD5_ROUND4(b, c, d, a, x[ 5], 21, 0xfc93a039);
  210. MD5_ROUND4(a, b, c, d, x[12], 6, 0x655b59c3);
  211. MD5_ROUND4(d, a, b, c, x[ 3], 10, 0x8f0ccc92);
  212. MD5_ROUND4(c, d, a, b, x[10], 15, 0xffeff47d);
  213. MD5_ROUND4(b, c, d, a, x[ 1], 21, 0x85845dd1);
  214. MD5_ROUND4(a, b, c, d, x[ 8], 6, 0x6fa87e4f);
  215. MD5_ROUND4(d, a, b, c, x[15], 10, 0xfe2ce6e0);
  216. MD5_ROUND4(c, d, a, b, x[ 6], 15, 0xa3014314);
  217. MD5_ROUND4(b, c, d, a, x[13], 21, 0x4e0811a1);
  218. MD5_ROUND4(a, b, c, d, x[ 4], 6, 0xf7537e82);
  219. MD5_ROUND4(d, a, b, c, x[11], 10, 0xbd3af235);
  220. MD5_ROUND4(c, d, a, b, x[ 2], 15, 0x2ad7d2bb);
  221. MD5_ROUND4(b, c, d, a, x[ 9], 21, 0xeb86d391);
  222. state[0] += a;
  223. state[1] += b;
  224. state[2] += c;
  225. state[3] += d;
  226. }
  227. /*!
  228. @brief 内部函数,处理数据的前面部分(>64字节的部分),每次组成一个64字节的block就进行杂凑处理
  229. @param[out] ctx 算法的context,用于记录一些处理的上下文和结果
  230. @param[in] buf 处理的数据,
  231. @param[in] size 处理的数据长度
  232. */
  233. static void zen_md5_update(md5_ctx *ctx, const unsigned char *buf, size_t size)
  234. {
  235. //为什么不是=,因为在某些环境下,可以多次调用zen_md5_update,但这种情况,必须保证前面的调用,每次都没有unprocessed_
  236. ctx->length_ += size;
  237. //每个处理的块都是64字节
  238. while (size >= ZEN_MD5_BLOCK_SIZE)
  239. {
  240. zen_md5_process_block(ctx->hash_, reinterpret_cast<const uint32_t *>(buf));
  241. buf += ZEN_MD5_BLOCK_SIZE;
  242. size -= ZEN_MD5_BLOCK_SIZE;
  243. }
  244. ctx->unprocessed_ = size;
  245. }
  246. /*!
  247. @brief 内部函数,处理数据的末尾部分,我们要拼出最后1个(或者两个)要处理的BLOCK,加上0x80,加上长度进行处理
  248. @param[in] ctx 算法的context,用于记录一些处理的上下文和结果
  249. @param[in] buf 处理的数据
  250. @param[in] size 处理buffer的长度
  251. @param[out] result 返回的结果,
  252. */
  253. static void zen_md5_final(md5_ctx *ctx, const unsigned char *buf, size_t size, unsigned char *result)
  254. {
  255. uint32_t message[ZEN_MD5_BLOCK_SIZE / 4];
  256. //保存剩余的数据,我们要拼出最后1个(或者两个)要处理的块,前面的算法保证了,最后一个块肯定小于64个字节
  257. if (ctx->unprocessed_)
  258. {
  259. memcpy(message, buf + size - ctx->unprocessed_, static_cast<size_t>( ctx->unprocessed_));
  260. }
  261. //得到0x80要添加在的位置(在uint32_t 数组中),
  262. uint32_t index = ((uint32_t)ctx->length_ & 63) >> 2;
  263. uint32_t shift = ((uint32_t)ctx->length_ & 3) * 8;
  264. //添加0x80进去,并且把余下的空间补充0
  265. message[index] &= ~(0xFFFFFFFF << shift);
  266. message[index++] ^= 0x80 << shift;
  267. //如果这个block还无法处理,其后面的长度无法容纳长度64bit,那么先处理这个block
  268. if (index > 14)
  269. {
  270. while (index < 16)
  271. {
  272. message[index++] = 0;
  273. }
  274. zen_md5_process_block(ctx->hash_, message);
  275. index = 0;
  276. }
  277. //补0
  278. while (index < 14)
  279. {
  280. message[index++] = 0;
  281. }
  282. //保存长度,注意是bit位的长度,这个问题让我看着郁闷了半天,
  283. uint64_t data_len = (ctx->length_) << 3;
  284. //注意MD5算法要求的64bit的长度是小头LITTLE-ENDIAN编码,注意下面的比较是!=
  285. #if ZEN_BYTES_ORDER != ZEN_LITTLE_ENDIAN
  286. data_len = ZEN_SWAP_UINT64(data_len);
  287. #endif
  288. message[14] = (uint32_t) (data_len & 0x00000000FFFFFFFF);
  289. message[15] = (uint32_t) ((data_len & 0xFFFFFFFF00000000ULL) >> 32);
  290. zen_md5_process_block(ctx->hash_, message);
  291. //注意结果是小头党的,在大头的世界要进行转换
  292. #if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIAN
  293. memcpy(result, &ctx->hash_, ZEN_MD5_HASH_SIZE);
  294. #else
  295. swap_uint32_memcpy(result, &ctx->hash_, ZEN_MD5_HASH_SIZE);
  296. #endif
  297. }
  298. //计算一个内存数据的MD5值
  299. unsigned char *ZEN_LIB::md5(const unsigned char *buf,
  300. size_t size,
  301. unsigned char result[ZEN_MD5_HASH_SIZE])
  302. {
  303. assert(result != NULL);
  304. md5_ctx ctx;
  305. zen_md5_init(&ctx);
  306. zen_md5_update(&ctx, buf, size);
  307. zen_md5_final(&ctx, buf, size, result);
  308. return result;
  309. }
  310. //================================================================================================
  311. //SHA1的算法
  312. //每次处理的BLOCK的大小
  313. static const size_t ZEN_SHA1_BLOCK_SIZE = 64;
  314. //SHA1算法的上下文,保存一些状态,中间数据,结果
  315. typedef struct sha1_ctx
  316. {
  317. //处理的数据的长度
  318. uint64_t length_;
  319. //还没有处理的数据长度
  320. uint64_t unprocessed_;
  321. /* 160-bit algorithm internal hashing state */
  322. uint32_t hash_[5];
  323. } sha1_ctx;
  324. //内部函数,SHA1算法的上下文的初始化
  325. static void zen_sha1_init(sha1_ctx *ctx)
  326. {
  327. ctx->length_ = 0;
  328. ctx->unprocessed_ = 0;
  329. // 初始化算法的几个常量,魔术数;
  330. ctx->hash_[0] = 0x67452301;
  331. ctx->hash_[1] = 0xefcdab89;
  332. ctx->hash_[2] = 0x98badcfe;
  333. ctx->hash_[3] = 0x10325476;
  334. ctx->hash_[4] = 0xc3d2e1f0;
  335. }
  336. /*!
  337. @brief 内部函数,对一个64bit内存块进行摘要(杂凑)处理,
  338. @param hash 存放计算hash结果的的数组
  339. @param block 要计算的处理得内存块
  340. */
  341. static void zen_sha1_process_block(uint32_t hash[5],
  342. const uint32_t block[ZEN_SHA1_BLOCK_SIZE / 4])
  343. {
  344. size_t t;
  345. uint32_t wblock[80];
  346. register uint32_t a, b, c, d, e, temp;
  347. //SHA1算法处理的内部数据要求是大头党的,在小头的环境转换
  348. #if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIAN
  349. swap_uint32_memcpy(wblock, block, ZEN_SHA1_BLOCK_SIZE);
  350. #else
  351. ::memcpy(wblock, block, ZEN_SHA1_BLOCK_SIZE);
  352. #endif
  353. //处理
  354. for (t = 16; t < 80; t++)
  355. {
  356. wblock[t] = ROTL32(wblock[t - 3] ^ wblock[t - 8] ^ wblock[t - 14] ^ wblock[t - 16], 1);
  357. }
  358. a = hash[0];
  359. b = hash[1];
  360. c = hash[2];
  361. d = hash[3];
  362. e = hash[4];
  363. for (t = 0; t < 20; t++)
  364. {
  365. /* the following is faster than ((B & C) | ((~B) & D)) */
  366. temp = ROTL32(a, 5) + (((c ^ d) & b) ^ d)
  367. + e + wblock[t] + 0x5A827999;
  368. e = d;
  369. d = c;
  370. c = ROTL32(b, 30);
  371. b = a;
  372. a = temp;
  373. }
  374. for (t = 20; t < 40; t++)
  375. {
  376. temp = ROTL32(a, 5) + (b ^ c ^ d) + e + wblock[t] + 0x6ED9EBA1;
  377. e = d;
  378. d = c;
  379. c = ROTL32(b, 30);
  380. b = a;
  381. a = temp;
  382. }
  383. for (t = 40; t < 60; t++)
  384. {
  385. temp = ROTL32(a, 5) + ((b & c) | (b & d) | (c & d))
  386. + e + wblock[t] + 0x8F1BBCDC;
  387. e = d;
  388. d = c;
  389. c = ROTL32(b, 30);
  390. b = a;
  391. a = temp;
  392. }
  393. for (t = 60; t < 80; t++)
  394. {
  395. temp = ROTL32(a, 5) + (b ^ c ^ d) + e + wblock[t] + 0xCA62C1D6;
  396. e = d;
  397. d = c;
  398. c = ROTL32(b, 30);
  399. b = a;
  400. a = temp;
  401. }
  402. hash[0] += a;
  403. hash[1] += b;
  404. hash[2] += c;
  405. hash[3] += d;
  406. hash[4] += e;
  407. }
  408. /*!
  409. @brief 内部函数,处理数据的前面部分(>64字节的部分),每次组成一个64字节的block就进行杂凑处理
  410. @param ctx 算法的上下文,记录中间数据,结果等
  411. @param msg 要进行计算的数据buffer
  412. @param size 长度
  413. */
  414. static void zen_sha1_update(sha1_ctx *ctx,
  415. const unsigned char *buf,
  416. size_t size)
  417. {
  418. //为了让zen_sha1_update可以多次进入,长度可以累计
  419. ctx->length_ += size;
  420. //每个处理的块都是64字节
  421. while (size >= ZEN_SHA1_BLOCK_SIZE)
  422. {
  423. zen_sha1_process_block(ctx->hash_, reinterpret_cast<const uint32_t *>(buf));
  424. buf += ZEN_SHA1_BLOCK_SIZE;
  425. size -= ZEN_SHA1_BLOCK_SIZE;
  426. }
  427. ctx->unprocessed_ = size;
  428. }
  429. /*!
  430. @brief 内部函数,处理数据的最后部分,添加0x80,补0,增加长度信息
  431. @param ctx 算法的上下文,记录中间数据,结果等
  432. @param msg 要进行计算的数据buffer
  433. @param result 返回的结果
  434. */
  435. static void zen_sha1_final(sha1_ctx *ctx,
  436. const unsigned char *msg,
  437. size_t size,
  438. unsigned char *result)
  439. {
  440. uint32_t message[ZEN_SHA1_BLOCK_SIZE / 4];
  441. //保存剩余的数据,我们要拼出最后1个(或者两个)要处理的块,前面的算法保证了,最后一个块肯定小于64个字节
  442. if (ctx->unprocessed_)
  443. {
  444. memcpy(message, msg + size - ctx->unprocessed_, static_cast<size_t>( ctx->unprocessed_));
  445. }
  446. //得到0x80要添加在的位置(在uint32_t 数组中),
  447. uint32_t index = ((uint32_t)ctx->length_ & 63) >> 2;
  448. uint32_t shift = ((uint32_t)ctx->length_ & 3) * 8;
  449. //添加0x80进去,并且把余下的空间补充0
  450. message[index] &= ~(0xFFFFFFFF << shift);
  451. message[index++] ^= 0x80 << shift;
  452. //如果这个block还无法处理,其后面的长度无法容纳长度64bit,那么先处理这个block
  453. if (index > 14)
  454. {
  455. while (index < 16)
  456. {
  457. message[index++] = 0;
  458. }
  459. zen_sha1_process_block(ctx->hash_, message);
  460. index = 0;
  461. }
  462. //补0
  463. while (index < 14)
  464. {
  465. message[index++] = 0;
  466. }
  467. //保存长度,注意是bit位的长度,这个问题让我看着郁闷了半天,
  468. uint64_t data_len = (ctx->length_) << 3;
  469. //注意SHA1算法要求的64bit的长度是大头BIG-ENDIAN,在小头的世界要进行转换
  470. #if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIAN
  471. data_len = ZEN_SWAP_UINT64(data_len);
  472. #endif
  473. message[14] = (uint32_t) (data_len & 0x00000000FFFFFFFF);
  474. message[15] = (uint32_t) ((data_len & 0xFFFFFFFF00000000ULL) >> 32);
  475. zen_sha1_process_block(ctx->hash_, message);
  476. //注意结果是大头党的,在小头的世界要进行转换
  477. #if ZEN_BYTES_ORDER == ZEN_LITTLE_ENDIAN
  478. swap_uint32_memcpy(result, &ctx->hash_, ZEN_SHA1_HASH_SIZE);
  479. #else
  480. memcpy(result, &ctx->hash_, ZEN_SHA1_HASH_SIZE);
  481. #endif
  482. }
  483. //计算一个内存数据的SHA1值
  484. unsigned char *ZEN_LIB::sha1(const unsigned char *msg,
  485. size_t size,
  486. unsigned char result[ZEN_SHA1_HASH_SIZE])
  487. {
  488. assert(result != NULL);
  489. sha1_ctx ctx;
  490. zen_sha1_init(&ctx);
  491. zen_sha1_update(&ctx, msg, size);
  492. zen_sha1_final(&ctx, msg, size, result);
  493. return result;
  494. }
  495. /*
  496. int main(int argc, char * argv[])
  497. {
  498. int ret = 0;
  499. static unsigned char test_buf[7][81] =
  500. {
  501. { "" },
  502. { "a" },
  503. { "abc" },
  504. { "message digest" },
  505. { "abcdefghijklmnopqrstuvwxyz" },
  506. { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" },
  507. { "12345678901234567890123456789012345678901234567890123456789012345678901234567890" }
  508. };
  509. static const size_t test_buflen[7] =
  510. {
  511. 0, 1, 3, 14, 26, 62, 80
  512. };
  513. static const unsigned char md5_test_sum[7][16] =
  514. {
  515. { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
  516. { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8, 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
  517. { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0, 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
  518. { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D, 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
  519. { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00, 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
  520. { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5, 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
  521. { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55, 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
  522. };
  523. unsigned char result[32] ={0};
  524. for(size_t i=0;i<7;++i)
  525. {
  526. ZEN_LIB::md5(test_buf[i],test_buflen[i],result);
  527. ret = memcmp(result,md5_test_sum[i],16);
  528. if (ret != 0)
  529. {
  530. assert(false);
  531. }
  532. }
  533. static const unsigned char sha1_test_sum[7][20] =
  534. {
  535. { 0xda,0x39,0xa3,0xee,0x5e,0x6b,0x4b,0x0d,0x32,0x55,0xbf,0xef,0x95,0x60,0x18,0x90,0xaf,0xd8,0x07,0x09 },
  536. { 0x86,0xf7,0xe4,0x37,0xfa,0xa5,0xa7,0xfc,0xe1,0x5d,0x1d,0xdc,0xb9,0xea,0xea,0xea,0x37,0x76,0x67,0xb8 },
  537. { 0xa9,0x99,0x3e,0x36,0x47,0x06,0x81,0x6a,0xba,0x3e,0x25,0x71,0x78,0x50,0xc2,0x6c,0x9c,0xd0,0xd8,0x9d },
  538. { 0xc1,0x22,0x52,0xce,0xda,0x8b,0xe8,0x99,0x4d,0x5f,0xa0,0x29,0x0a,0x47,0x23,0x1c,0x1d,0x16,0xaa,0xe3 },
  539. { 0x32,0xd1,0x0c,0x7b,0x8c,0xf9,0x65,0x70,0xca,0x04,0xce,0x37,0xf2,0xa1,0x9d,0x84,0x24,0x0d,0x3a,0x89 },
  540. { 0x76,0x1c,0x45,0x7b,0xf7,0x3b,0x14,0xd2,0x7e,0x9e,0x92,0x65,0xc4,0x6f,0x4b,0x4d,0xda,0x11,0xf9,0x40 },
  541. { 0x50,0xab,0xf5,0x70,0x6a,0x15,0x09,0x90,0xa0,0x8b,0x2c,0x5e,0xa4,0x0f,0xa0,0xe5,0x85,0x55,0x47,0x32 },
  542. };
  543. for(size_t i=0;i<7;++i)
  544. {
  545. ZEN_LIB::sha1(test_buf[i],test_buflen[i],result);
  546. ret = memcmp(result,sha1_test_sum[i],20);
  547. if (ret != 0)
  548. {
  549. assert(false);
  550. }
  551. }
  552. return 0;
  553. }*/