资源简介
sha1算法实现,接口预先定义好,你可以参考资料进行扩展改进。
代码片段和文件信息
/********************************************************************
**Copyright (C) 2016 cugriver. All Rights Reserved
**
** FileName: sha1.cpp
** Author: cugriver
** Email: cugriver@163.com
** LastChange: 2016-06-19 15:39:01
********************************************************************/
#include “sha1.h“
SHA1::SHA1():_msgdgt(NULL) _k(NULL){}
SHA1::~SHA1(){
if(_msgdgt) delete[]_msgdgt;
if(_k) delete[] _k;
}
unsigned int* SHA1::preProcess(std::string data){
uint32_t _block_num_ = (data.length() + 8)/64 + 1;
uint32_t* _block_ = new uint32_t[_block_num_*16];
uint32_t i = 0;
for(i = 0; i < _block_num_*16; ++i)
_block_[i] = 0x00000000;
for(i = 0; i < data.length(); ++i){
_block_[i >> 2] |= ((uint32_t)data[i]) << ((i%4) * 8);
}
_block_[i >> 2] |= 0x80 << ((i%4)*8);
_block_[_block_num_*16-2] = data.length()*8;
uint32_t _low = _block_[_block_num_*16-2];
uint32_t _high = _block_[_block_num_*16-1];
_low = transform(_low);
_high = transform(_high);
_block_[_block_num_*16-2] = _high;
_block_[_block_num_*16-1] = _low;
return _block_;
}
void SHA1::handleBlock(uint32_t* block){
uint32_t a b c de temp = 0x00000000;
a = _msgdgt[0];
b = _msgdgt[1];
c = _msgdgt[2];
d = _msgdgt[3];
e = _msgdgt[4];
uint32_t* w = new uint32_t[80];
for(int i = 0; i < 16; ++i)
w[i] = transform(block[i]);
for(int i = 16; i < 80; ++i){
uint32_t t = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
w[i] = leftRotate(t1);
}
for(int i = 0; i < 80; ++i){
uint32_t f = 0x00000000;
uint32_t k = 0x00000000;
if(i < 20){
f = step1(b c d);
k = _k[0];
}else if(i < 40){
f = step2(b c d);
k = _k[1];
}else if(i < 60){
f = step3(b c d);
k = _k[2];
}else{
f = step4(b c d);
k = _k[3];
}
temp = leftRotate(a5) + f + e + k + w[i];
e = d;
d = c;
c = leftRotate(b 30);
b = a;
a = temp;
}
_msgdgt[0] = (_msgdgt[0] + a) & 0xFFFFFFFF;
_msgdgt[1] = (_msgdgt[1] + b) & 0xFFFFFFFF;
_msgdgt[2] = (_msgdgt[2] + c) & 0xFFFFFFFF;
_msgdgt[3] = (_msgdgt[3] + d) & 0xFFFFFFFF;
_msgdgt[4] = (_msgdgt[4] + e) & 0xFFFFFFFF;
}
unsigned int SHA1::transform(uint32_t value){
uint32_t _temp = 0x00000000;
_temp |= (value & 0x000000ff) << 24;
_temp |= (value & 0x0000ff00) << 8;
_temp |= (value & 0x00ff0000) >> 8;
_temp |= (value & 0xff000000) >> 24;
return _temp;
}
unsigned int SHA1::step1(uint32_t b uint32_t c uint32_t d){
return (b & c) | ((~b) & d);
}
unsigned int SHA1::step2(uint32_t b uint32_t c uint32_t d){
return (b ^ c ^ d);
}
unsigned int SHA1::step3(uint32_t b uint32_t c uint32_t d){
return (b & c) | (b & d) | (c & d);
}
unsigned int SHA1::step4(uint32_t b uint32_t c uint32_t d){
return (b ^ c ^ d);
相关资源
- 安全哈希算法(sha1)c语言实现,包括
- 常用加密算法AES、RSA、DES、MD5、TEA、
- C语言 3DES、AES、RC6、TEA、RSA、MD5、S
- MD5-SHA1-SHA256算法C源码
- c++之sha1类
- PBKDF2_HMac_SHA1哈希算法
- SHA1算法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
- SHA1加密算法源代码
- c语言 SHA1 DES MD4 MD5
- mfc窗口分割和树形控件的结合使用
- sha1加密算法C++实现
- 编程实现SHA1/MD5杂凑密码算法,sha1程
- MD5 SHA1 SHA256 的C语言源码
评论
共有 条评论