原来指望sha1 这种烂大街的算法 不会出什么幺蛾子 结果《linux C编程实战Code》bt章节的sha1 代码 我在linux和windows下的结果不一样

然后用了哈希工具查看了下 发现结果也不一样。 windows和linux自带工具是一致的,但是和《linux C编程实战Code》的代码 无论在windows还是linux下都不一致

这里记录下新得代码 以后备用 (unbuntu wndows7 下执行 计算结果一致)

  1. /*
  2. * sha1.h
  3. *
  4. * Description:
  5. * This is the header file for code which implements the Secure
  6. * Hashing Algorithm 1 as defined in FIPS PUB 180-1 published
  7. * April 17, 1995.
  8. *
  9. * Many of the variable names in this code, especially the
  10. * single character names, were used because those were the names
  11. * used in the publication.
  12. *
  13. * Please read the file sha1.c for more information.
  14. *
  15. */
  16.  
  17. #ifndef _SHA1_H_
  18. #define _SHA1_H_
  19. #include <stdint.h>
  20. /*
  21. * If you do not have the ISO standard stdint.h header file, then you
  22. * must typdef the following:
  23. * name meaning
  24. * uint32_t unsigned 32 bit integer
  25. * uint8_t unsigned 8 bit integer (i.e., unsigned char)
  26. * int_least16_t integer of >= 16 bits
  27. *
  28. */
  29. #ifndef _SHA_enum_
  30. #define _SHA_enum_
  31. enum
  32. {
  33. shaSuccess = ,
  34. shaNull, /* Null pointer parameter */
  35. shaInputTooLong, /* input data too long */
  36. shaStateError /* called Input after Result */
  37. };
  38. #endif
  39. #define SHA1HashSize 20
  40. /*
  41. * This structure will hold context information for the SHA-1
  42. * hashing operation
  43. */
  44. typedef struct SHA1Context
  45. {
  46. uint32_t Intermediate_Hash[SHA1HashSize / ]; /* Message Digest */
  47. uint32_t Length_Low; /* Message length in bits */
  48. uint32_t Length_High; /* Message length in bits */
  49. /* Index into message block array */
  50. int_least16_t Message_Block_Index;
  51. uint8_t Message_Block[]; /* 512-bit message blocks */
  52. int Computed; /* Is the digest computed? */
  53. int Corrupted; /* Is the message digest corrupted? */
  54. } SHA1Context;
  55.  
  56. /*
  57. * Function Prototypes
  58. */
  59.  
  60. int SHA1Reset(SHA1Context *);
  61. int SHA1Input(SHA1Context *, const uint8_t *, unsigned int);
  62. int SHA1Result(SHA1Context *, uint8_t Message_Digest[SHA1HashSize]);
  63.  
  64. #endif

sha1.h

  1. /*
  2. * sha1.c
  3. *
  4. * Description:
  5. * This file implements the Secure Hashing Algorithm 1 as
  6. * defined in FIPS PUB 180-1 published April 17, 1995.
  7. *
  8. * The SHA-1, produces a 160-bit message digest for a given
  9. * data stream. It should take about 2**n steps to find a
  10. * message with the same digest as a given message and
  11. * 2**(n/2) to find any two messages with the same digest,
  12. * when n is the digest size in bits. Therefore, this
  13. * algorithm can serve as a means of providing a
  14. * "fingerprint" for a message.
  15. *
  16. * Portability Issues:
  17. * SHA-1 is defined in terms of 32-bit "words". This code
  18. * uses <stdint.h> (included via "sha1.h" to define 32 and 8
  19. * bit unsigned integer types. If your C compiler does not
  20. * support 32 bit unsigned integers, this code is not
  21. * appropriate.
  22. *
  23. * Caveats:
  24. * SHA-1 is designed to work with messages less than 2^64 bits
  25. * long. Although SHA-1 allows a message digest to be generated
  26. * for messages of any number of bits less than 2^64, this
  27. * implementation only works with messages with a length that is
  28. * a multiple of the size of an 8-bit character.
  29. *
  30. */
  31.  
  32. #include "SHA1.h"
  33.  
  34. #ifdef __cplusplus
  35. extern "C"
  36. {
  37. #endif
  38.  
  39. /*
  40. * Define the SHA1 circular left shift macro
  41. */
  42. #define SHA1CircularShift(bits,word) \
  43. (((word) << (bits)) | ((word) >> (-(bits))))
  44. /* Local Function Prototyptes */
  45. void SHA1PadMessage(SHA1Context *);
  46. void SHA1ProcessMessageBlock(SHA1Context *);
  47. /*
  48. * SHA1Reset
  49. *
  50. * Description:
  51. * This function will initialize the SHA1Context in preparation
  52. * for computing a new SHA1 message digest.
  53. *
  54. * Parameters:
  55. * context: [in/out]
  56. * The context to reset.
  57. *
  58. * Returns:
  59. * sha Error Code.
  60. *
  61. */
  62. int SHA1Reset(SHA1Context *context)//初始化状态
  63. {
  64. if (!context)
  65. {
  66. return shaNull;
  67. }
  68. context->Length_Low = ;
  69. context->Length_High = ;
  70. context->Message_Block_Index = ;
  71. context->Intermediate_Hash[] = 0x67452301;//取得的HASH结果(中间数据)
  72. context->Intermediate_Hash[] = 0xEFCDAB89;
  73. context->Intermediate_Hash[] = 0x98BADCFE;
  74. context->Intermediate_Hash[] = 0x10325476;
  75. context->Intermediate_Hash[] = 0xC3D2E1F0;
  76. context->Computed = ;
  77. context->Corrupted = ;
  78. return shaSuccess;
  79. }
  80.  
  81. /*
  82. * SHA1Result
  83. *
  84. * Description:
  85. * This function will return the 160-bit message digest into the
  86. * Message_Digest array provided by the caller.
  87. * NOTE: The first octet of hash is stored in the 0th element,
  88. * the last octet of hash in the 19th element.
  89. *
  90. * Parameters:
  91. * context: [in/out]
  92. * The context to use to calculate the SHA-1 hash.
  93. * Message_Digest: [out]
  94. * Where the digest is returned.
  95. *
  96. * Returns:
  97. * sha Error Code.
  98. *
  99. */
  100. int SHA1Result(SHA1Context *context, uint8_t Message_Digest[SHA1HashSize])
  101. {
  102. int i;
  103. if (!context || !Message_Digest)
  104. {
  105. return shaNull;
  106. }
  107. if (context->Corrupted)
  108. {
  109. return context->Corrupted;
  110. }
  111. if (!context->Computed)
  112. {
  113. SHA1PadMessage(context);
  114. for (i = ; i < ; ++i)
  115. {
  116. /* message may be sensitive, clear it out */
  117. context->Message_Block[i] = ;
  118. }
  119. context->Length_Low = ; /* and clear length */
  120. context->Length_High = ;
  121. context->Computed = ;
  122. }
  123. for (i = ; i < SHA1HashSize; ++i)
  124. {
  125. Message_Digest[i] = context->Intermediate_Hash[i >> ]
  126. >> * ( - (i & 0x03));
  127. }
  128. return shaSuccess;
  129. }
  130.  
  131. /*
  132. * SHA1Input
  133. *
  134. * Description:
  135. * This function accepts an array of octets as the next portion
  136. * of the message.
  137. *
  138. * Parameters:
  139. * context: [in/out]
  140. * The SHA context to update
  141. * message_array: [in]
  142. * An array of characters representing the next portion of
  143. * the message.
  144. * length: [in]
  145. * The length of the message in message_array
  146. *
  147. * Returns:
  148. * sha Error Code.
  149. *
  150. */
  151.  
  152. int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned length)
  153. {
  154. if (!length)
  155. {
  156. return shaSuccess;
  157. }
  158. if (!context || !message_array)
  159. {
  160. return shaNull;
  161. }
  162. if (context->Computed)
  163. {
  164. context->Corrupted = shaStateError;
  165. return shaStateError;
  166. }
  167. if (context->Corrupted)
  168. {
  169. return context->Corrupted;
  170. }
  171. while (length-- && !context->Corrupted)
  172. {
  173. context->Message_Block[context->Message_Block_Index++] =
  174. (*message_array & 0xFF);
  175. context->Length_Low += ;
  176. if (context->Length_Low == )
  177. {
  178. context->Length_High++;
  179. if (context->Length_High == )
  180. {
  181. /* Message is too long */
  182. context->Corrupted = ;
  183. }
  184. }
  185. if (context->Message_Block_Index == )
  186. {
  187. SHA1ProcessMessageBlock(context);
  188. }
  189. message_array++;
  190. }
  191. return shaSuccess;
  192. }
  193.  
  194. /*
  195. * SHA1ProcessMessageBlock
  196. *
  197. * Description:
  198. * This function will process the next 512 bits of the message
  199. * stored in the Message_Block array.
  200. *
  201. * Parameters:
  202. * None.
  203. *
  204. * Returns:
  205. * Nothing.
  206. *
  207. * Comments:
  208. * Many of the variable names in this code, especially the
  209. * single character names, were used because those were the
  210. * names used in the publication.
  211. *
  212. */
  213.  
  214. void SHA1ProcessMessageBlock(SHA1Context *context)
  215. {
  216. const uint32_t K[] = { /* Constants defined in SHA-1 */
  217. 0x5A827999,
  218. 0x6ED9EBA1,
  219. 0x8F1BBCDC,
  220. 0xCA62C1D6
  221. };
  222. int t; /* Loop counter */
  223. uint32_t temp; /* Temporary word value */
  224. uint32_t W[]; /* Word sequence */
  225. uint32_t A, B, C, D, E; /* Word buffers */
  226. /*
  227. * Initialize the first 16 words in the array W
  228. */
  229. for (t = ; t < ; t++)
  230. {
  231. W[t] = context->Message_Block[t * ] << ;
  232. W[t] |= context->Message_Block[t * + ] << ;
  233. W[t] |= context->Message_Block[t * + ] << ;
  234. W[t] |= context->Message_Block[t * + ];
  235. }
  236. for (t = ; t < ; t++)
  237. {
  238. W[t] = SHA1CircularShift(, W[t - ] ^ W[t - ] ^ W[t - ] ^ W[t - ]);
  239. }
  240. A = context->Intermediate_Hash[];
  241. B = context->Intermediate_Hash[];
  242. C = context->Intermediate_Hash[];
  243. D = context->Intermediate_Hash[];
  244. E = context->Intermediate_Hash[];
  245. for (t = ; t < ; t++)
  246. {
  247. temp = SHA1CircularShift(, A) +
  248. ((B & C) | ((~B) & D)) + E + W[t] + K[];
  249. E = D;
  250. D = C;
  251. C = SHA1CircularShift(, B);
  252. B = A;
  253. A = temp;
  254. }
  255. for (t = ; t < ; t++)
  256. {
  257. temp = SHA1CircularShift(, A) + (B ^ C ^ D) + E + W[t] + K[];
  258. E = D;
  259. D = C;
  260. C = SHA1CircularShift(, B);
  261. B = A;
  262. A = temp;
  263. }
  264. for (t = ; t < ; t++)
  265. {
  266. temp = SHA1CircularShift(, A) +
  267. ((B & C) | (B & D) | (C & D)) + E + W[t] + K[];
  268. E = D;
  269. D = C;
  270. C = SHA1CircularShift(, B);
  271. B = A;
  272. A = temp;
  273. }
  274. for (t = ; t < ; t++)
  275. {
  276. temp = SHA1CircularShift(, A) + (B ^ C ^ D) + E + W[t] + K[];
  277. E = D;
  278. D = C;
  279. C = SHA1CircularShift(, B);
  280. B = A;
  281. A = temp;
  282. }
  283. context->Intermediate_Hash[] += A;
  284. context->Intermediate_Hash[] += B;
  285. context->Intermediate_Hash[] += C;
  286. context->Intermediate_Hash[] += D;
  287. context->Intermediate_Hash[] += E;
  288. context->Message_Block_Index = ;
  289. }
  290.  
  291. /*
  292. * SHA1PadMessage
  293. *
  294. * Description:
  295. * According to the standard, the message must be padded to an even
  296. * 512 bits. The first padding bit must be a ’1’. The last 64
  297. * bits represent the length of the original message. All bits in
  298. * between should be 0. This function will pad the message
  299. * according to those rules by filling the Message_Block array
  300. * accordingly. It will also call the ProcessMessageBlock function
  301. * provided appropriately. When it returns, it can be assumed that
  302. * the message digest has been computed.
  303. *
  304. * Parameters:
  305. * context: [in/out]
  306. * The context to pad
  307. * ProcessMessageBlock: [in]
  308. * The appropriate SHA*ProcessMessageBlock function
  309. * Returns:
  310. * Nothing.
  311. *
  312. */
  313.  
  314. void SHA1PadMessage(SHA1Context *context)
  315. {
  316. /*
  317. * Check to see if the current message block is too small to hold
  318. * the initial padding bits and length. If so, we will pad the
  319. * block, process it, and then continue padding into a second
  320. * block.
  321. */
  322. if (context->Message_Block_Index > )
  323. {
  324. context->Message_Block[context->Message_Block_Index++] = 0x80;
  325. while (context->Message_Block_Index < )
  326. {
  327. context->Message_Block[context->Message_Block_Index++] = ;
  328. }
  329. SHA1ProcessMessageBlock(context);
  330. while (context->Message_Block_Index < )
  331. {
  332. context->Message_Block[context->Message_Block_Index++] = ;
  333. }
  334. }
  335. else
  336. {
  337. context->Message_Block[context->Message_Block_Index++] = 0x80;
  338. while (context->Message_Block_Index < )
  339. {
  340. context->Message_Block[context->Message_Block_Index++] = ;
  341. }
  342. }
  343.  
  344. /*
  345. * Store the message length as the last 8 octets
  346. */
  347. context->Message_Block[] = context->Length_High >> ;
  348. context->Message_Block[] = context->Length_High >> ;
  349. context->Message_Block[] = context->Length_High >> ;
  350. context->Message_Block[] = context->Length_High;
  351. context->Message_Block[] = context->Length_Low >> ;
  352. context->Message_Block[] = context->Length_Low >> ;
  353. context->Message_Block[] = context->Length_Low >> ;
  354. context->Message_Block[] = context->Length_Low;
  355. SHA1ProcessMessageBlock(context);
  356. }
  357.  
  358. #ifdef __cplusplus
  359. }
  360. #endif

sha1.c

sha1 算法源码的更多相关文章

  1. Atitit 图像清晰度 模糊度 检测 识别 评价算法 源码实现attilax总结

    Atitit 图像清晰度 模糊度 检测 识别 评价算法 源码实现attilax总结 1.1. 原理,主要使用像素模糊后的差别会变小1 1.2. 具体流程1 1.3. 提升性能 可以使用采样法即可..1 ...

  2. mahout算法源码分析之Collaborative Filtering with ALS-WR (四)评价和推荐

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 首先来总结一下 mahout算法源码分析之Collaborative Filtering with AL ...

  3. mahout算法源码分析之Collaborative Filtering with ALS-WR拓展篇

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 额,好吧,心头的一块石头总算是放下了.关于Collaborative Filtering with AL ...

  4. mahout算法源码分析之Collaborative Filtering with ALS-WR 并行思路

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. mahout算法源码分析之Collaborative Filtering with ALS-WR 这个算 ...

  5. diff.js 列表对比算法 源码分析

    diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...

  6. [Spark内核] 第34课:Stage划分和Task最佳位置算法源码彻底解密

    本課主題 Job Stage 划分算法解密 Task 最佳位置算法實現解密 引言 作业调度的划分算法以及 Task 的最佳位置的算法,因为 Stage 的划分是DAGScheduler 工作的核心,这 ...

  7. zookeeper集群搭建及Leader选举算法源码解析

    第一章.zookeeper概述 一.zookeeper 简介 zookeeper 是一个开源的分布式应用程序协调服务器,是 Hadoop 的重要组件. zooKeeper 是一个分布式的,开放源码的分 ...

  8. 基于单层决策树的AdaBoost算法源码

    基于单层决策树的AdaBoost算法源码 Mian.py # -*- coding: utf-8 -*- # coding: UTF-8 import numpy as np from AdaBoos ...

  9. OpenCV人脸识别Eigen算法源码分析

    1 理论基础 学习Eigen人脸识别算法需要了解一下它用到的几个理论基础,现总结如下: 1.1 协方差矩阵 首先需要了解一下公式: 共公式可以看出:均值描述的是样本集合的平均值,而标准差描述的则是样本 ...

随机推荐

  1. django 加载css、js和图片记载不上

    在django的setting里加以下配置 STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), 'Djago/static/',)

  2. python视频学习笔记2(if)

    一.if语句1.比较运算符,if语句语法 # 1. 输入用户年龄# 2. 判断是否满 18 岁 (**>=**)# 3. 如果满 18 岁,允许进网吧嗨皮# 4. 如果未满 18 岁,提示回家写 ...

  3. Kivy / Buildozer VM Ubuntu不能连接到网络的问题解决

    从kivy网站下载下来的Buildozer VM镜像在进入虚拟机以后无论虚拟机里边的虚拟网络编辑器以及网络适配器网络连接作何设置都不能连接到网络,在终端里边使用ifconfig查看ip地址是127.0 ...

  4. 浅谈openstack中使用linux_bridge实现vxlan网络

    openstack环境: 1 版本:ocata 2 系统:ubuntu16.04.2 3 控制节点 1个 + 计算节点 1个 4 控制节点网卡为ens33,ip = 172.171.5.200 ens ...

  5. [Ting's笔记Day9]活用套件Carrierwave gem:(4)使用Imagemagick修改图片大小

    前情提要: 这几天我都在实验Carrierwave这套图片上传套件,也顺利部署到Heroku架站正式环境了.:) 接下来我遇到了新的问题:要如何在上传的时候,让Carrierwave gem大型siz ...

  6. vuecli3.0安装搭建项目

    1. npm install -g @vue/cli 2. vue create wechat Linter / Formatter 可以不选 检查空格的 //选择less //标准eslint // ...

  7. 【原创】控制perl和python脚本执行过程中脚本文件是否关闭的方法

    引子 跟踪perl和python脚本对文件的访问,实际过程中,perl和python解析器在解析完脚本后,直接关闭了 脚本文件,在进程中查询不到是访问文件的脚本文件名称. shell.perl和pyt ...

  8. Homework:奇偶性

    // 程序功能: // 要求用户从键盘输入一个整数,判断其是奇数还是偶数 #include <stdio.h> int main() { int x; printf("输入一个整 ...

  9. python+Django+test 测试数据库生成报错

    前提: 使用Django自带的test进行单元测试. 问题描述: 运行:python manage.py test,报错,出现数据库乱码的现象,报错如下: Creating test database ...

  10. vue项目锚点的使用

    在vue项目中如何使用锚点呢? 在vue-router中定义 scrollBehavior scrollBehavior (to, from, savedPosition) { if (savedPo ...