资源简介
SHA-256算法的C++实现及demo
代码片段和文件信息
#include “sha256.h“
/// same as reset()
SHA256::SHA256()
{
reset();
}
/// restart
void SHA256::reset()
{
m_numBytes = 0;
m_bufferSize = 0;
// according to RFC 1321
m_hash[0] = 0x6a09e667;
m_hash[1] = 0xbb67ae85;
m_hash[2] = 0x3c6ef372;
m_hash[3] = 0xa54ff53a;
m_hash[4] = 0x510e527f;
m_hash[5] = 0x9b05688c;
m_hash[6] = 0x1f83d9ab;
m_hash[7] = 0x5be0cd19;
}
namespace
{
inline uint32_t rotate(uint32_t a uint32_t c)
{
return (a >> c) | (a << (32 - c));
}
inline uint32_t swap(uint32_t x)
{
return _byteswap_ulong(x);
}
// mix functions for processBlock()
inline uint32_t f1(uint32_t e uint32_t f uint32_t g)
{
uint32_t term1 = rotate(e 6) ^ rotate(e 11) ^ rotate(e 25);
uint32_t term2 = (e & f) ^ (~e & g); //(g ^ (e & (f ^ g)))
return term1 + term2;
}
inline uint32_t f2(uint32_t a uint32_t b uint32_t c)
{
uint32_t term1 = rotate(a 2) ^ rotate(a 13) ^ rotate(a 22);
uint32_t term2 = ((a | b) & c) | (a & b); //(a & (b ^ c)) ^ (b & c);
return term1 + term2;
}
}
/// process 64 bytes
void SHA256::processBlock(const void* data)
{
// get last hash
uint32_t a = m_hash[0];
uint32_t b = m_hash[1];
uint32_t c = m_hash[2];
uint32_t d = m_hash[3];
uint32_t e = m_hash[4];
uint32_t f = m_hash[5];
uint32_t g = m_hash[6];
uint32_t h = m_hash[7];
// data represented as 16x 32-bit words
const uint32_t* input = (uint32_t*) data;
// convert to big endian
uint32_t words[64];
int i;
for (i = 0; i < 16; i++)
#if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
words[i] = input[i];
#else
words[i] = swap(input[i]);
#endif
uint32_t xy; // temporaries
// first round
x = h + f1(efg) + 0x428a2f98 + words[ 0]; y = f2(abc); d += x; h = x + y;
x = g + f1(def) + 0x71374491 + words[ 1]; y = f2(hab); c += x; g = x + y;
x = f + f1(cde) + 0xb5c0fbcf + words[ 2]; y = f2(gha); b += x; f = x + y;
x = e + f1(bcd) + 0xe9b5dba5 + words[ 3]; y = f2(fgh); a += x; e = x + y;
x = d + f1(abc) + 0x3956c25b + words[ 4]; y = f2(efg); h += x; d = x + y;
x = c + f1(hab) + 0x59f111f1 + words[ 5]; y = f2(def); g += x; c = x + y;
x = b + f1(gha) + 0x923f82a4 + words[ 6]; y = f2(cde); f += x; b = x + y;
x = a + f1(fgh) + 0xab1c5ed5 + words[ 7]; y = f2(bcd); e += x; a = x + y;
// secound round
x = h + f1(efg) + 0xd807aa98 + words[ 8]; y = f2(abc); d += x; h = x + y;
x = g + f1(def) + 0x12835b01 + words[ 9]; y = f2(hab); c += x; g = x + y;
x = f + f1(cde) + 0x243185be + words[10]; y = f2(gha); b += x; f = x + y;
x = e + f1(bcd) + 0x550c7dc3 + words[11]; y = f2(fgh); a += x; e = x + y;
x = d + f1(abc) + 0x72be5d74 + words[12]; y = f2(efg); h += x; d = x + y;
x = c + f1(hab) + 0x80deb1fe + words[13]; y = f2(def); g += x; c = x + y;
x = b + f1(gha) + 0x9bdc06a7 + words[14]; y = f2(cde); f += x; b = x + y;
x = a + f1(fgh) + 0xc19bf174 + words[15属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-05-31 14:54 testsha256\
目录 0 2016-05-31 14:35 testsha256\Debug\
文件 85504 2016-05-31 14:53 testsha256\Debug\testsha256.exe
文件 578048 2016-05-31 14:53 testsha256\Debug\testsha256.ilk
文件 1126400 2016-05-31 14:53 testsha256\Debug\testsha256.pdb
目录 0 2016-05-31 14:53 testsha256\testsha256\
目录 0 2016-05-31 14:53 testsha256\testsha256\Debug\
文件 174855 2016-05-31 14:52 testsha256\testsha256\Debug\sha256.obj
文件 11147 2016-05-31 14:35 testsha256\testsha256\Debug\stdafx.obj
文件 1445 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.log
文件 159772 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.obj
文件 2162688 2016-05-31 14:35 testsha256\testsha256\Debug\testsha256.pch
目录 0 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.tlog\
文件 27858 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.tlog\CL.read.1.tlog
文件 1980 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.tlog\CL.write.1.tlog
文件 1940 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.tlog\cl.command.1.tlog
文件 1442 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.tlog\li
文件 3044 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.tlog\li
文件 674 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.tlog\li
文件 163 2016-05-31 14:53 testsha256\testsha256\Debug\testsha256.tlog\testsha256.lastbuildstate
文件 371712 2016-05-31 14:53 testsha256\testsha256\Debug\vc120.idb
文件 430080 2016-05-31 14:53 testsha256\testsha256\Debug\vc120.pdb
文件 1531 2016-05-31 14:26 testsha256\testsha256\ReadMe.txt
文件 13115 2016-05-31 14:51 testsha256\testsha256\sha256.cpp
文件 1247 2016-05-31 14:50 testsha256\testsha256\sha256.h
文件 216 2016-05-31 14:26 testsha256\testsha256\stdafx.cpp
文件 234 2016-05-31 14:26 testsha256\testsha256\stdafx.h
文件 236 2016-05-31 14:26 testsha256\testsha256\targetver.h
文件 346 2016-05-31 14:53 testsha256\testsha256\testsha256.cpp
文件 4631 2016-05-31 14:35 testsha256\testsha256\testsha256.vcxproj
文件 1495 2016-05-31 14:34 testsha256\testsha256\testsha256.vcxproj.filters
............此处省略3个文件信息
相关资源
- C++获取计算机的CPU ID,硬盘序列号等
- C++头文件转delphi工具 + 源码
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
川公网安备 51152502000135号
评论
共有 条评论