原来指望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 = 0, 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 / 4]; /* 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[64]; /* 512-bit message blocks */ 52 int Computed; /* Is the digest computed? */ 53 int Corrupted; /* Is the message digest corrupted? */ 54 } SHA1Context; 55 56 57 /* 58 * Function Prototypes 59 */ 60 61 int SHA1Reset(SHA1Context *); 62 int SHA1Input(SHA1Context *, const uint8_t *, unsigned int); 63 int SHA1Result(SHA1Context *, uint8_t Message_Digest[SHA1HashSize]); 64 65 #endif