资源简介
标准SHA1算法的C++实现,讲解了该算法的计算原理,并对难懂代码进行解释和具体的使用方法。拿来就能用。
代码片段和文件信息
/*
* sha1.cpp
*
* Copyright (C) 1998
* Paul E. Jones
* All Rights Reserved.
*
*****************************************************************************
* $Id: sha1.cppv 1.9 2004/03/27 18:02:20 paulej Exp $
*****************************************************************************
*
* Description:
* This class implements the Secure Hashing Standard as defined
* in FIPS PUB 180-1 published April 17 1995.
*
* The Secure Hashing Standard which uses the Secure Hashing
* Algorithm (SHA) produces a 160-bit message digest for a
* given data stream. In theory it is highly improbable that
* two messages will produce the same message digest. 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 was
* written with the expectation that the processor has at least
* a 32-bit machine word size. If the machine word size is larger
* the code should still function properly. One caveat to that
* is that the input functions taking characters and character arrays
* assume that only 8 bits of information are stored in each character.
*
* 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 8
* bits.
*
*/
#include “sha1.h“
SHA1::SHA1()
{
SHAInit();
}
SHA1::~SHA1()
{
}
void SHA1::SHAInit()
{
Length_Low = 0;
Length_High = 0;
Message_Block_Index = 0;
H[0] = 0x67452301;
H[1] = 0xEFCDAB89;
H[2] = 0x98BADCFE;
H[3] = 0x10325476;
H[4] = 0xC3D2E1F0;
}
// space of lpSHACode_Output must be >= 20 bytes;
bool SHA1::SHA_GO( const char *lpData_Input char *lpSHACode_Output )
{
if (lpData_Input == NULL || lpSHACode_Output == NULL)
return false;
SHAInit();
// One times analyse 64Bytes 512 bits.
int nInputLen = strlen(lpData_Input);
int nDealDataLen = 0; // the length of can-deal-data this times;
for(int pos=0 ; pos<=nInputLen ; pos+=64)
{
if (nInputLen - pos >= 64)
{
nDealDataLen = 64; // input-data is enough fill 64bytes
memset(Message_Block 0 sizeof(Message_Block));
memcpy(Message_Block lpData_Input + pos nDealDataLen);
AddDataLen(nDealDataLen);
ProcessMessageBlock();
AddDataLen(0);
}
else
{
nDealDataLen = nInputLen - pos; // input-data isn‘t enough fill 64bytesneed fill 0x8000000000 and lenth of real-data.
memset(Message_Block 0 sizeof(Message_Block));
memcpy(Message_Block lpData_Input + pos nDealDataLen);
AddDataLen(nDealDataLen);
PadMessage();
}
}
// copy result to output
for (int i = 0; i < 5; i++)
{
sprintf(&(lpSHACode_Output[8*
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2648 2009-04-20 18:08 sha1.h
文件 7797 2009-04-20 17:58 sha1.cpp
----------- --------- ---------- ----- ----
10445 2
- 上一篇:C语言实现:Linux下的myshell
- 下一篇:VC++Spin(旋转)控件用法
相关资源
- VC++Spin(旋转)控件用法
- 导引头模型仿真
- ROBOOP --机器人正逆解C++版本开源开源
- C++实现日志库
- 基于IE浏览器的源代码
- C++音视频即时通讯源代码
- VC++摄像头视频采集及回放源程序
- 模拟退火遗传算法的C++程序
- c语言五子棋代码
- C++实现mqtt协议
- 21天学通C++第8版源代码
- c++利用遗传算法求解函数优化问题
- TXT文本里关键字检索
- 基于C++的边缘检测算法
- C++ USB HID
- vs2013 VC++项目里的Windows窗体应用程序
- C++标准库chm
- 协同过滤算法代码 C++
- socket文件传输C++
- 用c++设计一个日期类Date,包括年、月
- VC++ 实现增大可执行文件的体积 自
- c++ primer plus 第6版 中文版pdf 高清完整
- C++跑酷游戏
- C++简单的画图程序源代码
- c++ builder实现文件粉碎机的源码
- 简单电子英汉词典c++
- C++程序设计语言:第1~3部分原书第4版
- c++程序设计 谭浩强第二版答案
- c++学生信息管理系统附带报告
- C++builder编写的简单科学计算器
评论
共有 条评论