资源简介
hash sha1 类, 接口简单, 可接受输入内存指针,文件等
代码片段和文件信息
#include “stdafx.h“
#include
#include
#include “SHA1.h“
#pragma warning (disable: 4996) // fopen sprintf
uint32 SHA1::K[4] = {
0x5A827999
0x6ED9EBA1
0x8F1BBCDC
0xCA62C1D6
};
SHA1::SHA1()
{
Reset();
}
SHA1::~SHA1()
{
}
void SHA1::Reset()
{
H[0] = 0x67452301;
H[1] = 0xEFCDAB89;
H[2] = 0x98BADCFE;
H[3] = 0x10325476;
H[4] = 0xC3D2E1F0;
m_dataIndex = 0;
m_dataSizeInBits = 0;
m_computed = false;
m_corrupted = false;
}
int SHA1::PushData( const void* data uint32 sizeInBytes )
{
if (m_computed)
{
return SHA1_DATA_ALREADY_COMPUTED;
}
if (uint64(-1) - m_dataSizeInBits < (uint64)sizeInBytes)
{
m_corrupted = true;
return SHA1_DATA_TOO_LONG;
}
uint8* dataByte = (uint8*)data;
while ( sizeInBytes-- )
{
m_dataBlock[m_dataIndex++] = *dataByte++;
m_dataSizeInBits += 8;
if (m_dataIndex == 64)
{
ProcessDataBlock();
}
}
return SHA1_DATA_PUSH_SUCCEED;
}
uint32 SHA1::CircularLeftShift32(int bits uint32 data)
{
return (data << bits) | (data >> (32 - bits));
}
void SHA1::ProcessDataBlock()
{
uint32 W[80];
int t;
uint32 A B C D E temp;
for (t = 0; t < 16; t++)
{
W[t] = (m_dataBlock[t * 4] << 24) | (m_dataBlock[t * 4 + 1] << 16)
| (m_dataBlock[t * 4 + 2] << 8) | (m_dataBlock[t * 4 + 3]);
}
for ( ; t < 80; t++)
{
W[t] = CircularLeftShift32(1 W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
}
A = H[0];
B = H[1];
C = H[2];
D = H[3];
E = H[4];
for (t = 0; t < 20; t++)
{
temp = CircularLeftShift32(5 A) + ((B & C)|((~B) & D)) + E + W[t] + K[0];
E = D;
D = C;
C = CircularLeftShift32(30 B);
B = A;
A = temp;
}
for (; t < 40; t++)
{
temp = CircularLeftShift32(5 A) + (B ^ C ^ D) + E + W[t] + K[1];
E = D;
D = C;
C = CircularLeftShift32(30 B);
B = A;
A = temp;
}
for (; t < 60; t++)
{
temp = CircularLeftShift32(5 A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
E = D;
D = C;
C = CircularLeftShift32(30 B);
B = A;
A = temp;
}
for (; t < 80; t++)
{
temp = CircularLeftShift32(5 A) + (B ^ C ^ D) + E + W[t] + K[3];
E = D;
D = C;
C = CircularLeftShift32(30 B);
B = A;
A = temp;
}
H[0] += A;
H[1] += B;
H[2] += C;
H[3] += D;
H[4] += E;
m_dataIndex = 0;
}
void SHA1::PadData()
{
m_dataBlock[m_dataIndex++] = 0x80;
if (m_dataIndex > 56) // not enough space for the length data;
{
while(m_dataIndex < 64)
{
m_dataBlock[m_dataIndex++] = 0;
}
ProcessDataBlock();
}
while(m_dataIndex < 56)
{
m_dataBlock[m_dataIndex++] = 0;
}
for (int i = 56; m_dataIndex < 64; i -= 8)
{
m_dataBlock[m_dataIndex++] = (m_dataSizeInBits >> i) & 0xFF;
}
ProcessDataBlock();
}
bool SHA1::GetResult(uint32 msgDigest[5])
{
if (m_corrupted)
{
return false;
}
if (!m_computed)
{
PadData();
m_computed = t
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 801 2014-04-26 14:44 Hash.h
文件 3780 2014-03-03 14:54 SHA1.cpp
文件 1174 2014-03-03 14:52 SHA1.h
文件 0 2012-05-11 09:20 stdafx.h
----------- --------- ---------- ----- ----
5755 4
相关资源
- PBKDF2_HMac_SHA1哈希算法
- c语言实现获取文件的md5哈希值
- 20多个常用的Hash算法C++ 实现
- SHA1算法C实现源码
- 利用Hash技术统计C源程序中关键字的频
- sha1实现源码
- SHA1算法C语言源代码
- HMAC SHA1加密 C语言源码
- HMAC-SHA256和HMAC-SHA1加密C语言代码(V
- C++文件sha1
- HMAC-SHA1 C++实现
- 阿里云 hamcsha1算法
- HMAC-SHA256和HMAC-SHA1加密C语言代码
- C++ HMAC SHA1
- mhash-0.9.9.9.tar.gz
- SHA1加密算法源代码
- c语言 SHA1 DES MD4 MD5
- mfc窗口分割和树形控件的结合使用
- sha1加密算法C++实现
- 编程实现SHA1/MD5杂凑密码算法,sha1程
- 链地址哈希表
- MD5 SHA1 SHA256 的C语言源码
评论
共有 条评论