赞
踩
- #ifndef SHA1_H
- #define SHA1_H
-
- #include "stdint.h"
- //! #定义SHA 中的返回ENUM
- /*!
- @see enum
- */
- #ifndef _SHA_enum_
- #define _SHA_enum_
- enum
- {
- shaSuccess = 0,
- /*! <空指示参量 */
- shaNull,
- /*! < 输入数据太长提示 */
- shaInputTooLong,
- /*! <called Input after Result --以输入结果命名之 */
- shaStateError
- };
- #endif
- //! SHA1HashSize定义的是SHA1哈希表的大小
- #define SHA1HashSize 20
- //! #能够进行动态链接库编译的SHA1类
- /*!
- @see class _declspec(dllexport) SHA_1
- 将SHA1算法写成动态链接库的形式方便调用,生成消息使用
- */
- class _declspec(dllexport) SHA_1
- {
- public:
- //! #定义数据结构控制上下文消息 SHA1Context
- /*!
- 以下这种结构将会控制上下文消息 for the SHA-1
- hashing operation
- @see struct SHA1Context
- */
- typedef struct SHA1Context
- {
- uint32_t Intermediate_Hash[SHA1HashSize/4]; /*! <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[64]; /*! <512-bit message blocks */
-
- int Computed; /*! <Is the digest computed? */
- int Corrupted; /*! <Is the message digest corrupted? */
- } SHA1Context;
-
- public:
- //! #SHA_1 的构造函数
- /*!
- @see SHA_1()
- 其中应该对SHA_1类中的一些变量进行相应的初始化
- */
- SHA_1();
- //! #SHA_1的析构函数
- /*!
- @see ~SHA_1()
- 释放内存
- */
- ~SHA_1(void);
-
- /*----------------------------------函数原型----------------------------------*/
- //! #SHA1算法中的数据填充模块
- /*!
- @see void SHA1PadMessage(SHA1Context *);
- @param[SHA1Context* 定义填充信息指针
- @return[void] 不返回任何值
- */
- void SHA1PadMessage(SHA1Context *); /* 定义填充信息指针 */
- //! #SHA1的消息块描述函数
- /*!
- @see void SHA1ProcessMessageBlock(SHA1Context *);
- @param[SHA1Context* 定义填充信息指针
- @param[in] 消息块长度为固定之512比特
- @return[void] 不返回任何值
- */
- void SHA1ProcessMessageBlock(SHA1Context *);
- //! #SHA1的数据初始化操作函数
- /*!
- @see int SHA1Reset( SHA1Context *);
- @param[SHA1Context* 定义填充信息指针
- @return[int] 成功返回shaNull,失败返回shaSuccess
- @see SHA1 enum
- */
- int SHA1Reset( SHA1Context *);
- //! #SHA1的输入描述函数
- /*!
- @see int SHA1Input( SHA1Context *, const uint8_t *, unsigned int);
- @param[SHA1Context* 定义填充信息指针
- @param[uint8_t 接收单位长度为8字节倍数的消息
- @return[enum] 成功返回shaNull,失败返回shaSuccess,错误返回shaStateError
- @see SHA1 enum
- */
- int SHA1Input( SHA1Context *, const uint8_t *, unsigned int);
- //! #SHA1的结果描述函数
- /*!
- @see int SHA1Result( SHA1Context *, uint8_t Message_Digest[SHA1HashSize]);
- @param[SHA1Context* 定义填充信息指针
- @param[uint8_t 160比特的消息摘要队列
- @attention 返回一个160比特的消息摘要队列
- @return[enum] 成功返回shaNull,失败返回shaSuccess,错误返回shaStateError
- @see SHA1 enum
- */
- int SHA1Result( SHA1Context *, uint8_t Message_Digest[SHA1HashSize]);
-
- private:
- };
-
- #endif // SHA1_H
- #include "SHA1.h"
- /*!以下所用各种参量名称皆为sha-1在出版物上所用之公用名称 */
- /*
- * 以下是为 SHA1 向左环形移位宏 之定义
- */
- #define SHA1CircularShift(bits,word) \
- (((word) << (bits)) | ((word) >> (32-(bits))))
-
- SHA_1::SHA_1()
- {
- }
-
- SHA_1::~SHA_1(void)
- {
- }
-
- /*
- * 以下为sha-1消息块描述:
- * 消息块长度为固定之512比特
- */
- void SHA_1::SHA1ProcessMessageBlock(SHA1Context *context)
- {
- const uint32_t K[] = { /* Constants defined in SHA-1 */
- 0x5A827999,
- 0x6ED9EBA1,
- 0x8F1BBCDC,
- 0xCA62C1D6
- };
- int t; /* 循环计数 */
- uint32_t temp; /* 临时缓存 */
- uint32_t W[80]; /* 字顺序 */
- uint32_t A, B, C, D, E; /* 设置系统磁盘缓存块 */
-
- /*
- * 以下为初始化在W队列中的头16字数据
- */
- for(t = 0; t < 16; t++)
- {
- W[t] = context->Message_Block[t * 4] << 24;
- W[t] |= context->Message_Block[t * 4 + 1] << 16;
- W[t] |= context->Message_Block[t * 4 + 2] << 8;
- W[t] |= context->Message_Block[t * 4 + 3];
- }
-
- for(t = 16; t < 80; t++)
- {
- W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
- }
-
- A = context->Intermediate_Hash[0];
- B = context->Intermediate_Hash[1];
- C = context->Intermediate_Hash[2];
- D = context->Intermediate_Hash[3];
- E = context->Intermediate_Hash[4];
- /*
- * 以下为定义算法所用之数学函数及其迭代算法描述
- */
- for(t = 0; t < 20; t++)
- {
- temp = SHA1CircularShift(5,A) +
- ((B & C) | ((~B) & D)) + E + W[t] + K[0];
- E = D;
- D = C;
- C = SHA1CircularShift(30,B);
- B = A;
- A = temp;
- }
-
- for(t = 20; t < 40; t++)
- {
- temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
- E = D;
- D = C;
- C = SHA1CircularShift(30,B);
- B = A;
- A = temp;
- }
-
- for(t = 40; t < 60; t++)
- {
- temp = SHA1CircularShift(5,A) +
- ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
- E = D;
- D = C;
- C = SHA1CircularShift(30,B);
- B = A;
- A = temp;
- }
-
- for(t = 60; t < 80; t++)
- {
- temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
- E = D;
- D = C;
- C = SHA1CircularShift(30,B);
- B = A;
- A = temp;
- }
- /*
- * 以下为迭代算法第80步(最后一步)描述
- */
- context->Intermediate_Hash[0] += A;
- context->Intermediate_Hash[1] += B;
- context->Intermediate_Hash[2] += C;
- context->Intermediate_Hash[3] += D;
- context->Intermediate_Hash[4] += E;
-
- context->Message_Block_Index = 0;
- }
-
-
- /*
- * SHA1PadMessage
- * 数据填充模块
- */
-
- void SHA_1::SHA1PadMessage(SHA1Context *context)
- {
-
- if (context->Message_Block_Index > 55)
- {
- context->Message_Block[context->Message_Block_Index++] = 0x80;
- while(context->Message_Block_Index < 64)
- {
- context->Message_Block[context->Message_Block_Index++] = 0;
- }
-
- SHA1ProcessMessageBlock(context);
-
- while(context->Message_Block_Index < 56)
- {
- context->Message_Block[context->Message_Block_Index++] = 0;
- }
- }
- else
- {
- context->Message_Block[context->Message_Block_Index++] = 0x80;
- while(context->Message_Block_Index < 56)
- {
- context->Message_Block[context->Message_Block_Index++] = 0;
- }
- }
-
- /*
- * 把最后64位保存为数据长度
- */
- context->Message_Block[56] = context->Length_High >> 24;
- context->Message_Block[57] = context->Length_High >> 16;
- context->Message_Block[58] = context->Length_High >> 8;
- context->Message_Block[59] = context->Length_High;
- context->Message_Block[60] = context->Length_Low >> 24;
- context->Message_Block[61] = context->Length_Low >> 16;
- context->Message_Block[62] = context->Length_Low >> 8;
- context->Message_Block[63] = context->Length_Low;
-
- SHA1ProcessMessageBlock(context);
- }
-
- /*
- * SHA1Reset
- *
- * 以下为数据初始化之操作
- * Parameters:(参数设置)
- * context: [in/out]
- * The context to reset.
- *
- */
- int SHA_1::SHA1Reset(SHA1Context *context)
- {
- if (!context)
- {
- return shaNull;
- }
-
- context->Length_Low = 0;
- context->Length_High = 0;
- context->Message_Block_Index = 0;
-
- context->Intermediate_Hash[0] = 0x67452301;
- context->Intermediate_Hash[1] = 0xEFCDAB89;
- context->Intermediate_Hash[2] = 0x98BADCFE;
- context->Intermediate_Hash[3] = 0x10325476;
- context->Intermediate_Hash[4] = 0xC3D2E1F0;
-
- context->Computed = 0;
- context->Corrupted = 0;
- return shaSuccess;
- }
-
- /*
- * SHA1Result
- *
- * 以下为sha-1结果描述:
- *:
- * 该算法将会返回一个160比特的消息摘要队列
- *
- * 或者输出计算错误
- *
- */
- int SHA_1::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=0; i<64; ++i)
- {
- /* 消息清零 */
- context->Message_Block[i] = 0;
- }
- context->Length_Low = 0; /* 长度清零 */
- context->Length_High = 0;
- context->Computed = 1;
- }
-
- for(i = 0; i < SHA1HashSize; ++i)
- {
- Message_Digest[i] = context->Intermediate_Hash[i>>2]
- >> 8 * ( 3 - ( i & 0x03 ) );
- }
-
- return shaSuccess;
- }
-
- /*
- * 以下为sha-1输入描述:
- *
- * 接收单位长度为8字节倍数的消息
- *
- */
- int SHA_1::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 += 8;
- if (context->Length_Low == 0)
- {
- context->Length_High++;
- if (context->Length_High == 0)
- {
- /* Message is too long */
- context->Corrupted = 1;
- }
- }
-
- if (context->Message_Block_Index == 64)
- {
- SHA1ProcessMessageBlock(context);
- }
-
- message_array++;
- }
-
- return shaSuccess;
- }
- #ifndef MD5_H
- #define MD5_H
-
-
- typedef struct
- {
- unsigned int count[2];
- unsigned int state[4];
- unsigned char buffer[64];
- } MD5_CTX;
-
- #define F(x,y,z) ((x & y) | (~x & z))
- #define G(x,y,z) ((x & z) | (y & ~z))
- #define H(x,y,z) (x^y^z)
- #define I(x,y,z) (y ^ (x | ~z))
- #define ROTATE_LEFT(x,n) ((x << n) | (x >> (32-n)))
-
- #define FF(a,b,c,d,x,s,ac) \
- { \
- a += F(b,c,d) + x + ac; \
- a = ROTATE_LEFT(a,s); \
- a += b; \
- }
- #define GG(a,b,c,d,x,s,ac) \
- { \
- a += G(b,c,d) + x + ac; \
- a = ROTATE_LEFT(a,s); \
- a += b; \
- }
- #define HH(a,b,c,d,x,s,ac) \
- { \
- a += H(b,c,d) + x + ac; \
- a = ROTATE_LEFT(a,s); \
- a += b; \
- }
- #define II(a,b,c,d,x,s,ac) \
- { \
- a += I(b,c,d) + x + ac; \
- a = ROTATE_LEFT(a,s); \
- a += b; \
- }
- void MD5Init(MD5_CTX *context);
- void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen);
- void MD5Final(MD5_CTX *context, unsigned char digest[16]);
- void MD5Transform(unsigned int state[4], unsigned char block[64]);
- void MD5Encode(unsigned char *output, unsigned int *input, unsigned int len);
- void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len);
-
- #endif
-
-
-
- #include "md5.h"
- #include <memory.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include<fcntl.h>
- unsigned char PADDING[] =
- {
- 0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- };
-
- void MD5Init(MD5_CTX *context)
- {
- context->count[0] = 0;
- context->count[1] = 0;
- context->state[0] = 0x67452301;
- context->state[1] = 0xEFCDAB89;
- context->state[2] = 0x98BADCFE;
- context->state[3] = 0x10325476;
- }
-
- void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputlen)
- {
- unsigned int i = 0;
- unsigned int index = 0;
- unsigned int partlen = 0;
-
- index = (context->count[0] >> 3) & 0x3F;
- partlen = 64 - index;
- context->count[0] += inputlen << 3;
-
- if(context->count[0] < (inputlen << 3))
- context->count[1]++;
- context->count[1] += inputlen >> 29;
-
- if(inputlen >= partlen)
- {
- memcpy(&context->buffer[index], input,partlen);
- MD5Transform(context->state, context->buffer);
-
- for(i = partlen; i+64 <= inputlen; i+=64)
- MD5Transform(context->state, &input[i]);
-
- index = 0;
- }
- else
- {
- i = 0;
- }
- memcpy(&context->buffer[index], &input[i], inputlen-i);
- }
-
- void MD5Final(MD5_CTX *context, unsigned char digest[16])
- {
- unsigned int index = 0,padlen = 0;
- unsigned char bits[8];
-
- index = (context->count[0] >> 3) & 0x3F;
- padlen = (index < 56)?(56-index):(120-index);
- MD5Encode(bits, context->count, 8);
- MD5Update(context, PADDING, padlen);
- MD5Update(context, bits, 8);
- MD5Encode(digest, context->state, 16);
- }
-
- void MD5Encode(unsigned char *output,unsigned int *input,unsigned int len)
- {
- unsigned int i = 0;
- unsigned int j = 0;
-
- while(j < len)
- {
- output[j] = input[i] & 0xFF;
- output[j+1] = (input[i] >> 8) & 0xFF;
- output[j+2] = (input[i] >> 16) & 0xFF;
- output[j+3] = (input[i] >> 24) & 0xFF;
- i++;
- j += 4;
- }
- }
-
- void MD5Decode(unsigned int *output, unsigned char *input, unsigned int len)
- {
- unsigned int i = 0;
- unsigned int j = 0;
-
- while(j < len)
- {
- output[i] = (input[j]) |
- (input[j+1] << 8) |
- (input[j+2] << 16) |
- (input[j+3] << 24);
- i++;
- j += 4;
- }
- }
-
- void MD5Transform(unsigned int state[4], unsigned char block[64])
- {
- unsigned int a = state[0];
- unsigned int b = state[1];
- unsigned int c = state[2];
- unsigned int d = state[3];
- unsigned int x[64];
-
- MD5Decode(x,block,64);
-
- FF(a, b, c, d, x[ 0], 7, 0xd76aa478); /* 1 */
- FF(d, a, b, c, x[ 1], 12, 0xe8c7b756); /* 2 */
- FF(c, d, a, b, x[ 2], 17, 0x242070db); /* 3 */
- FF(b, c, d, a, x[ 3], 22, 0xc1bdceee); /* 4 */
- FF(a, b, c, d, x[ 4], 7, 0xf57c0faf); /* 5 */
- FF(d, a, b, c, x[ 5], 12, 0x4787c62a); /* 6 */
- FF(c, d, a, b, x[ 6], 17, 0xa8304613); /* 7 */
- FF(b, c, d, a, x[ 7], 22, 0xfd469501); /* 8 */
- FF(a, b, c, d, x[ 8], 7, 0x698098d8); /* 9 */
- FF(d, a, b, c, x[ 9], 12, 0x8b44f7af); /* 10 */
- FF(c, d, a, b, x[10], 17, 0xffff5bb1); /* 11 */
- FF(b, c, d, a, x[11], 22, 0x895cd7be); /* 12 */
- FF(a, b, c, d, x[12], 7, 0x6b901122); /* 13 */
- FF(d, a, b, c, x[13], 12, 0xfd987193); /* 14 */
- FF(c, d, a, b, x[14], 17, 0xa679438e); /* 15 */
- FF(b, c, d, a, x[15], 22, 0x49b40821); /* 16 */
-
- /* Round 2 */
- GG(a, b, c, d, x[ 1], 5, 0xf61e2562); /* 17 */
- GG(d, a, b, c, x[ 6], 9, 0xc040b340); /* 18 */
- GG(c, d, a, b, x[11], 14, 0x265e5a51); /* 19 */
- GG(b, c, d, a, x[ 0], 20, 0xe9b6c7aa); /* 20 */
- GG(a, b, c, d, x[ 5], 5, 0xd62f105d); /* 21 */
- GG(d, a, b, c, x[10], 9, 0x2441453); /* 22 */
- GG(c, d, a, b, x[15], 14, 0xd8a1e681); /* 23 */
- GG(b, c, d, a, x[ 4], 20, 0xe7d3fbc8); /* 24 */
- GG(a, b, c, d, x[ 9], 5, 0x21e1cde6); /* 25 */
- GG(d, a, b, c, x[14], 9, 0xc33707d6); /* 26 */
- GG(c, d, a, b, x[ 3], 14, 0xf4d50d87); /* 27 */
- GG(b, c, d, a, x[ 8], 20, 0x455a14ed); /* 28 */
- GG(a, b, c, d, x[13], 5, 0xa9e3e905); /* 29 */
- GG(d, a, b, c, x[ 2], 9, 0xfcefa3f8); /* 30 */
- GG(c, d, a, b, x[ 7], 14, 0x676f02d9); /* 31 */
- GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); /* 32 */
-
- /* Round 3 */
- HH(a, b, c, d, x[ 5], 4, 0xfffa3942); /* 33 */
- HH(d, a, b, c, x[ 8], 11, 0x8771f681); /* 34 */
- HH(c, d, a, b, x[11], 16, 0x6d9d6122); /* 35 */
- HH(b, c, d, a, x[14], 23, 0xfde5380c); /* 36 */
- HH(a, b, c, d, x[ 1], 4, 0xa4beea44); /* 37 */
- HH(d, a, b, c, x[ 4], 11, 0x4bdecfa9); /* 38 */
- HH(c, d, a, b, x[ 7], 16, 0xf6bb4b60); /* 39 */
- HH(b, c, d, a, x[10], 23, 0xbebfbc70); /* 40 */
- HH(a, b, c, d, x[13], 4, 0x289b7ec6); /* 41 */
- HH(d, a, b, c, x[ 0], 11, 0xeaa127fa); /* 42 */
- HH(c, d, a, b, x[ 3], 16, 0xd4ef3085); /* 43 */
- HH(b, c, d, a, x[ 6], 23, 0x4881d05); /* 44 */
- HH(a, b, c, d, x[ 9], 4, 0xd9d4d039); /* 45 */
- HH(d, a, b, c, x[12], 11, 0xe6db99e5); /* 46 */
- HH(c, d, a, b, x[15], 16, 0x1fa27cf8); /* 47 */
- HH(b, c, d, a, x[ 2], 23, 0xc4ac5665); /* 48 */
-
- /* Round 4 */
- II(a, b, c, d, x[ 0], 6, 0xf4292244); /* 49 */
- II(d, a, b, c, x[ 7], 10, 0x432aff97); /* 50 */
- II(c, d, a, b, x[14], 15, 0xab9423a7); /* 51 */
- II(b, c, d, a, x[ 5], 21, 0xfc93a039); /* 52 */
- II(a, b, c, d, x[12], 6, 0x655b59c3); /* 53 */
- II(d, a, b, c, x[ 3], 10, 0x8f0ccc92); /* 54 */
- II(c, d, a, b, x[10], 15, 0xffeff47d); /* 55 */
- II(b, c, d, a, x[ 1], 21, 0x85845dd1); /* 56 */
- II(a, b, c, d, x[ 8], 6, 0x6fa87e4f); /* 57 */
- II(d, a, b, c, x[15], 10, 0xfe2ce6e0); /* 58 */
- II(c, d, a, b, x[ 6], 15, 0xa3014314); /* 59 */
- II(b, c, d, a, x[13], 21, 0x4e0811a1); /* 60 */
- II(a, b, c, d, x[ 4], 6, 0xf7537e82); /* 61 */
- II(d, a, b, c, x[11], 10, 0xbd3af235); /* 62 */
- II(c, d, a, b, x[ 2], 15, 0x2ad7d2bb); /* 63 */
- II(b, c, d, a, x[ 9], 21, 0xeb86d391); /* 64 */
- state[0] += a;
- state[1] += b;
- state[2] += c;
- state[3] += d;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。