资源简介
给出了SHA算法的一种C语言版的实现。经与Openssl对照测试,通过本方法计算的摘要是正确的。

代码片段和文件信息
// SHA11.cpp: implementation of the CSHA1 class.
//
//////////////////////////////////////////////////////////////////////
#include “stdafx.h“
#include “Z32KMSK.h“
#include “SHA1.h“
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSHA1::CSHA1()
{
}
CSHA1::~CSHA1()
{
}
int CSHA1::SHA1(unsigned char* data int dataLen unsigned char* md)
{
unsigned char* pbytes = (unsigned char*)data;
unsigned int nbyte = dataLen;
static unsigned int words[80];
unsigned int H[5] = {0x67452301 0xEFCDAB89 0x98BADCFE 0x10325476 0xC3D2E1F0};
unsigned int a b c d e f k temp bitlen[2] word;
unsigned int i j index p1 p2 maxlen;
unsigned char spec[4] = {0};
i = nbyte % 4;
//处理尾项
p1 = nbyte - i;
spec[i] = 1 << 7;
while (i--)
{
spec[i] = pbytes[p1 + i];
}
maxlen = (nbyte + 1) % 64;
if(maxlen <= 56)
{
maxlen = (nbyte + 1) - maxlen + 64;
}
else
{
maxlen = (nbyte + 1) - maxlen + 128;
}
p2= maxlen - 8;
WriteBigEndian64((unsigned char*)bitlen nbyte * 8);
for(j = 0; j < maxlen; j += 64)
{
a = H[0];
b = H[1];
c = H[2];
d = H[3];
e = H[4];
for(i = 0; i < 80; i++)
{
if(i < 16)
{
index = j + (i << 2);
if(index < p1)
{
word = *((unsigned int*)(pbytes + index));
}
else if(index == p1)
{
word = *(unsigned int*)spec;
}
else if(index < p2)
{
word = 0;
}
else
{
word = (index < maxlen - 4) ? bitlen[0] : bitlen[1];
}
words[i] = FromBigEndian(word);
}
else
{
words[i] = LeftRol(words[i - 3] ^ words[i - 8] ^ words[i - 14] ^ words[i - 16] 1);
}
if(i < 20)
{
f = (b & c) | ((~b) & d);
k = 0x5A827999;
}
else if (i < 40)
{
f=b ^ c ^ d;
k = 0x6ED9EBA1;
}
else if (i < 60)
{
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
}
else
{
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
temp = LeftRol(a 5) + f + e + k + words[i];
e = d;
d = c;
c = LeftRol(b 30);
b = a;
a = temp;
}
H[0] += a;
H[1] += b;
H[2] += c;
H[3] += d;
H[4] += e;
}
int ct = 0;
for (i = 0; i < 5; i++)
{
unsigned char buf[4] = {0};
memcpy(buf &(H[i]) 4);
for (int r = 3; r >= 0; r--)
{
md[ct] = buf[r];
ct++;
}
}
return 0;
}
unsigned int CSHA1::FromBigEndian(unsigned int v)
{
return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) |
((v & 0xff000000) >> 24);
}
void CSHA1::WriteBigEndian64(unsigned char *p unsigned int v)
{
unsigned char t;
memset(p 0 8);
memcpy(p &v 4);
int i=0;
for(i = 0; i < 4; i++)
{
t = p[i];
p[i] = p[7 - i];
p[7 - i] = t;
}
}
u
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3098 2010-03-10 17:10 sha1.cpp
文件 698 2010-03-10 17:10 sha1.h
----------- --------- ---------- ----- ----
3796 2
- 上一篇:c++ 实现线程池的使用
- 下一篇:学生系统C语言成绩排名
相关资源
- C++ SHA256加密计算
- ResHacker 3.5 汉化 绝对可用
- lbm模拟液滴从壁面滑落
- 各种加密算法C语言版
- C++商品管理系统50页报告+源码。代码
- 使用itextsharp.dll把两个pdf文件合并成一
- VC读取shapefile文件源码,处理点线面
- 浅水方程C++源代码
- Hash算法之SHA1实现c++
- 数据结构与算法分析C++版 Clifford A.
- 基于Skinsharp的MFC界面美化(破解版含
- (VC)MFC Skinsharp换肤套装(lib+dll+h+编
- opengl简单的阴影贴图
- SkinSharp开发库+百款皮肤+皮肤编辑器
- VB皮肤控件 SkinSharp (SkinH_VB6.dll 破解
- SkinSharp静态库完善破解版
- C++使用Openssl进行RSA加密解密及签名验
- 仿Wireshark抓包工具MFC实现
- SHA常用算法实现SHA-1 SHA256 SHA384 SHA51
- 编译好的c++机器学习库shark4.0
- ADSP SHARC系列DSP应用系统设计及附赠数
- C#_IFC_Viewer_Editor 最新Csharp源码 2017_
- C# Csharp 调用 C++的DLL中的回调函数
- 数据结构与算法分析(C++)(第二版
- Skinsharp+最新破解版+150个皮肤
- Skinsharp-VS2013可用
- opencvsharp-20个
- ue4蓝图c++动态改变staticmeshactor材质动
- SHA-256算法的C++实现及demo
- sha256-512加密算法
评论
共有 条评论