sha1 算法源码
原来指望sha1 这种烂大街的算法 不会出什么幺蛾子 结果《linux C编程实战Code》bt章节的sha1 代码 我在linux和windows下的结果不一样
然后用了哈希工具查看了下 发现结果也不一样。 windows和linux自带工具是一致的,但是和《linux C编程实战Code》的代码 无论在windows还是linux下都不一致
这里记录下新得代码 以后备用 (unbuntu wndows7 下执行 计算结果一致)
- /*
- * sha1.h
- *
- * Description:
- * This is the header file for code which implements the Secure
- * Hashing Algorithm 1 as defined in FIPS PUB 180-1 published
- * April 17, 1995.
- *
- * Many of the variable names in this code, especially the
- * single character names, were used because those were the names
- * used in the publication.
- *
- * Please read the file sha1.c for more information.
- *
- */
- #ifndef _SHA1_H_
- #define _SHA1_H_
- #include <stdint.h>
- /*
- * If you do not have the ISO standard stdint.h header file, then you
- * must typdef the following:
- * name meaning
- * uint32_t unsigned 32 bit integer
- * uint8_t unsigned 8 bit integer (i.e., unsigned char)
- * int_least16_t integer of >= 16 bits
- *
- */
- #ifndef _SHA_enum_
- #define _SHA_enum_
- enum
- {
- shaSuccess = ,
- shaNull, /* Null pointer parameter */
- shaInputTooLong, /* input data too long */
- shaStateError /* called Input after Result */
- };
- #endif
- #define SHA1HashSize 20
- /*
- * This structure will hold context information for the SHA-1
- * hashing operation
- */
- typedef struct SHA1Context
- {
- uint32_t Intermediate_Hash[SHA1HashSize / ]; /* Message Digest */
- uint32_t Length_Low; /* Message length in bits */
- uint32_t Length_High; /* Message length in bits */
- /* Index into message block array */
- int_least16_t Message_Block_Index;
- uint8_t Message_Block[]; /* 512-bit message blocks */
- int Computed; /* Is the digest computed? */
- int Corrupted; /* Is the message digest corrupted? */
- } SHA1Context;
- /*
- * Function Prototypes
- */
- int SHA1Reset(SHA1Context *);
- int SHA1Input(SHA1Context *, const uint8_t *, unsigned int);
- int SHA1Result(SHA1Context *, uint8_t Message_Digest[SHA1HashSize]);
- #endif
sha1.h
- /*
- * sha1.c
- *
- * Description:
- * This file implements the Secure Hashing Algorithm 1 as
- * defined in FIPS PUB 180-1 published April 17, 1995.
- *
- * The SHA-1, produces a 160-bit message digest for a given
- * data stream. It should take about 2**n steps to find a
- * message with the same digest as a given message and
- * 2**(n/2) to find any two messages with the same digest,
- * when n is the digest size in bits. Therefore, this
- * algorithm can serve as a means of providing a
- * "fingerprint" for a message.
- *
- * Portability Issues:
- * SHA-1 is defined in terms of 32-bit "words". This code
- * uses <stdint.h> (included via "sha1.h" to define 32 and 8
- * bit unsigned integer types. If your C compiler does not
- * support 32 bit unsigned integers, this code is not
- * appropriate.
- *
- * Caveats:
- * SHA-1 is designed to work with messages less than 2^64 bits
- * long. Although SHA-1 allows a message digest to be generated
- * for messages of any number of bits less than 2^64, this
- * implementation only works with messages with a length that is
- * a multiple of the size of an 8-bit character.
- *
- */
- #include "SHA1.h"
- #ifdef __cplusplus
- extern "C"
- {
- #endif
- /*
- * Define the SHA1 circular left shift macro
- */
- #define SHA1CircularShift(bits,word) \
- (((word) << (bits)) | ((word) >> (-(bits))))
- /* Local Function Prototyptes */
- void SHA1PadMessage(SHA1Context *);
- void SHA1ProcessMessageBlock(SHA1Context *);
- /*
- * SHA1Reset
- *
- * Description:
- * This function will initialize the SHA1Context in preparation
- * for computing a new SHA1 message digest.
- *
- * Parameters:
- * context: [in/out]
- * The context to reset.
- *
- * Returns:
- * sha Error Code.
- *
- */
- int SHA1Reset(SHA1Context *context)//初始化状态
- {
- if (!context)
- {
- return shaNull;
- }
- context->Length_Low = ;
- context->Length_High = ;
- context->Message_Block_Index = ;
- context->Intermediate_Hash[] = 0x67452301;//取得的HASH结果(中间数据)
- context->Intermediate_Hash[] = 0xEFCDAB89;
- context->Intermediate_Hash[] = 0x98BADCFE;
- context->Intermediate_Hash[] = 0x10325476;
- context->Intermediate_Hash[] = 0xC3D2E1F0;
- context->Computed = ;
- context->Corrupted = ;
- return shaSuccess;
- }
- /*
- * SHA1Result
- *
- * Description:
- * This function will return the 160-bit message digest into the
- * Message_Digest array provided by the caller.
- * NOTE: The first octet of hash is stored in the 0th element,
- * the last octet of hash in the 19th element.
- *
- * Parameters:
- * context: [in/out]
- * The context to use to calculate the SHA-1 hash.
- * Message_Digest: [out]
- * Where the digest is returned.
- *
- * Returns:
- * sha Error Code.
- *
- */
- int SHA1Result(SHA1Context *context, uint8_t Message_Digest[SHA1HashSize])
- {
- int i;
- if (!context || !Message_Digest)
- {
- return shaNull;
- }
- if (context->Corrupted)
- {
- return context->Corrupted;
- }
- if (!context->Computed)
- {
- SHA1PadMessage(context);
- for (i = ; i < ; ++i)
- {
- /* message may be sensitive, clear it out */
- context->Message_Block[i] = ;
- }
- context->Length_Low = ; /* and clear length */
- context->Length_High = ;
- context->Computed = ;
- }
- for (i = ; i < SHA1HashSize; ++i)
- {
- Message_Digest[i] = context->Intermediate_Hash[i >> ]
- >> * ( - (i & 0x03));
- }
- return shaSuccess;
- }
- /*
- * SHA1Input
- *
- * Description:
- * This function accepts an array of octets as the next portion
- * of the message.
- *
- * Parameters:
- * context: [in/out]
- * The SHA context to update
- * message_array: [in]
- * An array of characters representing the next portion of
- * the message.
- * length: [in]
- * The length of the message in message_array
- *
- * Returns:
- * sha Error Code.
- *
- */
- int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned length)
- {
- if (!length)
- {
- return shaSuccess;
- }
- if (!context || !message_array)
- {
- return shaNull;
- }
- if (context->Computed)
- {
- context->Corrupted = shaStateError;
- return shaStateError;
- }
- if (context->Corrupted)
- {
- return context->Corrupted;
- }
- while (length-- && !context->Corrupted)
- {
- context->Message_Block[context->Message_Block_Index++] =
- (*message_array & 0xFF);
- context->Length_Low += ;
- if (context->Length_Low == )
- {
- context->Length_High++;
- if (context->Length_High == )
- {
- /* Message is too long */
- context->Corrupted = ;
- }
- }
- if (context->Message_Block_Index == )
- {
- SHA1ProcessMessageBlock(context);
- }
- message_array++;
- }
- return shaSuccess;
- }
- /*
- * SHA1ProcessMessageBlock
- *
- * Description:
- * This function will process the next 512 bits of the message
- * stored in the Message_Block array.
- *
- * Parameters:
- * None.
- *
- * Returns:
- * Nothing.
- *
- * Comments:
- * Many of the variable names in this code, especially the
- * single character names, were used because those were the
- * names used in the publication.
- *
- */
- void SHA1ProcessMessageBlock(SHA1Context *context)
- {
- const uint32_t K[] = { /* Constants defined in SHA-1 */
- 0x5A827999,
- 0x6ED9EBA1,
- 0x8F1BBCDC,
- 0xCA62C1D6
- };
- int t; /* Loop counter */
- uint32_t temp; /* Temporary word value */
- uint32_t W[]; /* Word sequence */
- uint32_t A, B, C, D, E; /* Word buffers */
- /*
- * Initialize the first 16 words in the array W
- */
- for (t = ; t < ; t++)
- {
- W[t] = context->Message_Block[t * ] << ;
- W[t] |= context->Message_Block[t * + ] << ;
- W[t] |= context->Message_Block[t * + ] << ;
- W[t] |= context->Message_Block[t * + ];
- }
- for (t = ; t < ; t++)
- {
- W[t] = SHA1CircularShift(, W[t - ] ^ W[t - ] ^ W[t - ] ^ W[t - ]);
- }
- A = context->Intermediate_Hash[];
- B = context->Intermediate_Hash[];
- C = context->Intermediate_Hash[];
- D = context->Intermediate_Hash[];
- E = context->Intermediate_Hash[];
- for (t = ; t < ; t++)
- {
- temp = SHA1CircularShift(, A) +
- ((B & C) | ((~B) & D)) + E + W[t] + K[];
- E = D;
- D = C;
- C = SHA1CircularShift(, B);
- B = A;
- A = temp;
- }
- for (t = ; t < ; t++)
- {
- temp = SHA1CircularShift(, A) + (B ^ C ^ D) + E + W[t] + K[];
- E = D;
- D = C;
- C = SHA1CircularShift(, B);
- B = A;
- A = temp;
- }
- for (t = ; t < ; t++)
- {
- temp = SHA1CircularShift(, A) +
- ((B & C) | (B & D) | (C & D)) + E + W[t] + K[];
- E = D;
- D = C;
- C = SHA1CircularShift(, B);
- B = A;
- A = temp;
- }
- for (t = ; t < ; t++)
- {
- temp = SHA1CircularShift(, A) + (B ^ C ^ D) + E + W[t] + K[];
- E = D;
- D = C;
- C = SHA1CircularShift(, B);
- B = A;
- A = temp;
- }
- context->Intermediate_Hash[] += A;
- context->Intermediate_Hash[] += B;
- context->Intermediate_Hash[] += C;
- context->Intermediate_Hash[] += D;
- context->Intermediate_Hash[] += E;
- context->Message_Block_Index = ;
- }
- /*
- * SHA1PadMessage
- *
- * Description:
- * According to the standard, the message must be padded to an even
- * 512 bits. The first padding bit must be a ’1’. The last 64
- * bits represent the length of the original message. All bits in
- * between should be 0. This function will pad the message
- * according to those rules by filling the Message_Block array
- * accordingly. It will also call the ProcessMessageBlock function
- * provided appropriately. When it returns, it can be assumed that
- * the message digest has been computed.
- *
- * Parameters:
- * context: [in/out]
- * The context to pad
- * ProcessMessageBlock: [in]
- * The appropriate SHA*ProcessMessageBlock function
- * Returns:
- * Nothing.
- *
- */
- void SHA1PadMessage(SHA1Context *context)
- {
- /*
- * Check to see if the current message block is too small to hold
- * the initial padding bits and length. If so, we will pad the
- * block, process it, and then continue padding into a second
- * block.
- */
- if (context->Message_Block_Index > )
- {
- context->Message_Block[context->Message_Block_Index++] = 0x80;
- while (context->Message_Block_Index < )
- {
- context->Message_Block[context->Message_Block_Index++] = ;
- }
- SHA1ProcessMessageBlock(context);
- while (context->Message_Block_Index < )
- {
- context->Message_Block[context->Message_Block_Index++] = ;
- }
- }
- else
- {
- context->Message_Block[context->Message_Block_Index++] = 0x80;
- while (context->Message_Block_Index < )
- {
- context->Message_Block[context->Message_Block_Index++] = ;
- }
- }
- /*
- * Store the message length as the last 8 octets
- */
- context->Message_Block[] = context->Length_High >> ;
- context->Message_Block[] = context->Length_High >> ;
- context->Message_Block[] = context->Length_High >> ;
- context->Message_Block[] = context->Length_High;
- context->Message_Block[] = context->Length_Low >> ;
- context->Message_Block[] = context->Length_Low >> ;
- context->Message_Block[] = context->Length_Low >> ;
- context->Message_Block[] = context->Length_Low;
- SHA1ProcessMessageBlock(context);
- }
- #ifdef __cplusplus
- }
- #endif
sha1.c
sha1 算法源码的更多相关文章
- Atitit 图像清晰度 模糊度 检测 识别 评价算法 源码实现attilax总结
Atitit 图像清晰度 模糊度 检测 识别 评价算法 源码实现attilax总结 1.1. 原理,主要使用像素模糊后的差别会变小1 1.2. 具体流程1 1.3. 提升性能 可以使用采样法即可..1 ...
- mahout算法源码分析之Collaborative Filtering with ALS-WR (四)评价和推荐
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 首先来总结一下 mahout算法源码分析之Collaborative Filtering with AL ...
- mahout算法源码分析之Collaborative Filtering with ALS-WR拓展篇
Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. 额,好吧,心头的一块石头总算是放下了.关于Collaborative Filtering with AL ...
- 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 这个算 ...
- diff.js 列表对比算法 源码分析
diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...
- [Spark内核] 第34课:Stage划分和Task最佳位置算法源码彻底解密
本課主題 Job Stage 划分算法解密 Task 最佳位置算法實現解密 引言 作业调度的划分算法以及 Task 的最佳位置的算法,因为 Stage 的划分是DAGScheduler 工作的核心,这 ...
- zookeeper集群搭建及Leader选举算法源码解析
第一章.zookeeper概述 一.zookeeper 简介 zookeeper 是一个开源的分布式应用程序协调服务器,是 Hadoop 的重要组件. zooKeeper 是一个分布式的,开放源码的分 ...
- 基于单层决策树的AdaBoost算法源码
基于单层决策树的AdaBoost算法源码 Mian.py # -*- coding: utf-8 -*- # coding: UTF-8 import numpy as np from AdaBoos ...
- OpenCV人脸识别Eigen算法源码分析
1 理论基础 学习Eigen人脸识别算法需要了解一下它用到的几个理论基础,现总结如下: 1.1 协方差矩阵 首先需要了解一下公式: 共公式可以看出:均值描述的是样本集合的平均值,而标准差描述的则是样本 ...
随机推荐
- django 加载css、js和图片记载不上
在django的setting里加以下配置 STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), 'Djago/static/',)
- python视频学习笔记2(if)
一.if语句1.比较运算符,if语句语法 # 1. 输入用户年龄# 2. 判断是否满 18 岁 (**>=**)# 3. 如果满 18 岁,允许进网吧嗨皮# 4. 如果未满 18 岁,提示回家写 ...
- Kivy / Buildozer VM Ubuntu不能连接到网络的问题解决
从kivy网站下载下来的Buildozer VM镜像在进入虚拟机以后无论虚拟机里边的虚拟网络编辑器以及网络适配器网络连接作何设置都不能连接到网络,在终端里边使用ifconfig查看ip地址是127.0 ...
- 浅谈openstack中使用linux_bridge实现vxlan网络
openstack环境: 1 版本:ocata 2 系统:ubuntu16.04.2 3 控制节点 1个 + 计算节点 1个 4 控制节点网卡为ens33,ip = 172.171.5.200 ens ...
- [Ting's笔记Day9]活用套件Carrierwave gem:(4)使用Imagemagick修改图片大小
前情提要: 这几天我都在实验Carrierwave这套图片上传套件,也顺利部署到Heroku架站正式环境了.:) 接下来我遇到了新的问题:要如何在上传的时候,让Carrierwave gem大型siz ...
- vuecli3.0安装搭建项目
1. npm install -g @vue/cli 2. vue create wechat Linter / Formatter 可以不选 检查空格的 //选择less //标准eslint // ...
- 【原创】控制perl和python脚本执行过程中脚本文件是否关闭的方法
引子 跟踪perl和python脚本对文件的访问,实际过程中,perl和python解析器在解析完脚本后,直接关闭了 脚本文件,在进程中查询不到是访问文件的脚本文件名称. shell.perl和pyt ...
- Homework:奇偶性
// 程序功能: // 要求用户从键盘输入一个整数,判断其是奇数还是偶数 #include <stdio.h> int main() { int x; printf("输入一个整 ...
- python+Django+test 测试数据库生成报错
前提: 使用Django自带的test进行单元测试. 问题描述: 运行:python manage.py test,报错,出现数据库乱码的现象,报错如下: Creating test database ...
- vue项目锚点的使用
在vue项目中如何使用锚点呢? 在vue-router中定义 scrollBehavior scrollBehavior (to, from, savedPosition) { if (savedPo ...