資源簡(jiǎn)介
C++ SHA256加密計(jì)算
代碼片段和文件信息
/*********************************************************************
*?Filename:???sha256.c
*?Original?Author:?????Brad?Conte?(brad?AT?bradconte.com)
*?Labeled?and?modified?by:?AnSheng(https://github.com/monkeyDemon)
*?Copyright:
*?Disclaimer:?This?code?is?presented?“as?is“?without?any?guarantees.
*?Details:????Performs?known-answer?tests?on?the?corresponding?SHA1
??implementation.?These?tests?do?not?encompass?the?full
??range?of?available?test?vectors?however?if?the?tests
??pass?it?is?very?very?likely?that?the?code?is?correct
??and?was?compiled?properly.?This?code?also?serves?as
??example?usage?of?the?functions.
*********************************************************************/
/***************************?HEADER?FILES?***************************/
#include?
#include?
#include?“sha256.h“
/******************************?MACROS?******************************/
#define?ROTLEFT(ab)?(((a)?<(b))?|?((a)?>>?(32-(b))))
#define?ROTRIGHT(ab)?(((a)?>>?(b))?|?((a)?<(32-(b))))
#define?CH(xyz)?(((x)?&?(y))?^?(~(x)?&?(z)))
#define?MAJ(xyz)?(((x)?&?(y))?^?((x)?&?(z))?^?((y)?&?(z)))
#define?EP0(x)?(ROTRIGHT(x2)?^?ROTRIGHT(x13)?^?ROTRIGHT(x22))
#define?EP1(x)?(ROTRIGHT(x6)?^?ROTRIGHT(x11)?^?ROTRIGHT(x25))
#define?SIG0(x)?(ROTRIGHT(x7)?^?ROTRIGHT(x18)?^?((x)?>>?3))
#define?SIG1(x)?(ROTRIGHT(x17)?^?ROTRIGHT(x19)?^?((x)?>>?10))
/****************************?VARIABLES?*****************************/
static?const?WORD?k[64]?=?{
0x428a2f980x713744910xb5c0fbcf0xe9b5dba50x3956c25b0x59f111f10x923f82a40xab1c5ed5
0xd807aa980x12835b010x243185be0x550c7dc30x72be5d740x80deb1fe0x9bdc06a70xc19bf174
0xe49b69c10xefbe47860x0fc19dc60x240ca1cc0x2de92c6f0x4a7484aa0x5cb0a9dc0x76f988da
0x983e51520xa831c66d0xb00327c80xbf597fc70xc6e00bf30xd5a791470x06ca63510x14292967
0x27b70a850x2e1b21380x4d2c6dfc0x53380d130x650a73540x766a0abb0x81c2c92e0x92722c85
0xa2bfe8a10xa81a664b0xc24b8b700xc76c51a30xd192e8190xd69906240xf40e35850x106aa070
0x19a4c1160x1e376c080x2748774c0x34b0bcb50x391c0cb30x4ed8aa4a0x5b9cca4f0x682e6ff3
0x748f82ee0x78a5636f0x84c878140x8cc702080x90befffa0xa4506ceb0xbef9a3f70xc67178f2
};
/***********************?FUNCTION?DEFINITIONS?***********************/
void?sha256_transform(SHA256_CTX*?ctx?const?BYTE?data[])
{
WORD?a?b?c?d?e?f?g?h?i?j?t1?t2?m[64];
//?initialization?
for?(i?=?0?j?=?0;?i?16;?++i?j?+=?4)
m[i]?=?(data[j]?<24)?|?(data[j?+?1]?<16)?|?(data[j?+?2]?<8)?|?(data[j?+?3]);
for?(;?i?64;?++i)
m[i]?=?SIG1(m[i?-?2])?+?m[i?-?7]?+?SIG0(m[i?-?15])?+?m[i?-?16];
a?=?ctx->state[0];
b?=?ctx->state[1];
c?=?ctx->state[2];
d?=?ctx->state[3];
e?=?ctx->state[4];
f?=?ctx->state[5];
g?=?ctx->state[6];
h?=?ctx->state[7];
for?(i?=?0;?i?64;?++i)
{
t1?=?h?+?EP1(e)?+?CH(e?f?g)?+?k[i]?+?m[i];
t2?=?EP0(a)?+?MAJ(a?b?c);
h?=?g;
g?=?f;
f?=?e;
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????5477??2019-11-04?00:16??sha256.c
?????文件????????1059??2019-11-04?00:16??sha256.h
評(píng)論
共有 條評(píng)論