# sha256算法实现

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#define ROR(x,n) ((x << (32-n)) | (x>>n))

unsigned int rInit(double d) {
double ip;
unsigned long r = 0;
d = modf(d, &ip);

for (int i=0; i<8; i++) {
d = d * 16;
d = modf(d, &ip);
r = (r << 4) + (int)ip;
}
return r;
}

void sha256(char* text, unsigned char* result) {
int reg[8];
reg[0] = rInit(sqrt(2)),
reg[1] = rInit(sqrt(3)),
reg[2] = rInit(sqrt(5)),
reg[3] = rInit(sqrt(7)),
reg[4] = rInit(sqrt(11)),
reg[5] = rInit(sqrt(13)),
reg[6] = rInit(sqrt(17)),
reg[7] = rInit(sqrt(19));

int p[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311};
unsigned int k[64];
for (int i=0; i<64; i++) {
k[i] = rInit(pow(p[i], 1.0/3));
}

long long textLen = strlen(text);
long long allLen = textLen + (64- (textLen % 64));
unsigned char* newText = malloc(allLen);
memset(newText, 0, allLen);
strcpy((char*)newText, text);
newText[textLen] = (char)0x80;

long long bitLen = textLen * 8;

newText[allLen - 8] = (char) (bitLen >> 56);
newText[allLen - 7] = (char) (bitLen >> 48);
newText[allLen - 6] = (char) (bitLen >> 40);
newText[allLen - 5] = (char) (bitLen >> 32);
newText[allLen - 4] = (char) (bitLen >> 24);
newText[allLen - 3] = (char) (bitLen >> 16);
newText[allLen - 2] = (char) (bitLen >> 8);
newText[allLen - 1] = (char) (bitLen);

for (int i=0; i<allLen / 64 ; i++) {
unsigned int a = reg[0], b = reg[1], c = reg[2], d = reg[3], e = reg[4], f = reg[5], g = reg[6], h = reg[7];

unsigned int w[64];
for (int j=0, k=i*64; j<16; j++, k+=4) {
w[j] = (newText[k] << 24) | (newText[k+1] << 16) | (newText[k+2] << 8) | (newText[k+3]);
}

for (int j=16; j<64; j++) {
w[j] = (ROR(w[j-2],17) ^ ROR(w[j-2],19) ^ (w[j-2] >> 10)) + w[j-7] + (ROR(w[j-15],7) ^ ROR(w[j-15],18) ^ (w[j-15] >> 3)) + w[j-16];
}

for (int j=0; j<64; j++) {
/*           unsigned int ch = Ch(e, f, g);
unsigned int ma = Ma(a, b, c);
unsigned int sigma0 = Sigma0(a);
unsigned int sigma1 = Sigma1(e);
*/           unsigned int t1 = h + (ROR(e,6) ^ ROR(e,11) ^ ROR(e,25)) + ((e & f) ^ ( (~e) & g)) + k[j] + w[j];
unsigned int t2 = (ROR(a,2) ^ ROR(a,13) ^ ROR(a,22)) + ((a & b) ^ (a & c) ^ (b & c));

h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;

}

reg[0] = a + reg[0];
reg[1] = b + reg[1];
reg[2] = c + reg[2];
reg[3] = d + reg[3];
reg[4] = e + reg[4];
reg[5] = f + reg[5];
reg[6] = g + reg[6];
reg[7] = h + reg[7];

}

for (int i=0; i<8; i++) {
result[i*4] = (char) (reg[i] >> 24);
result[i*4+1] = (char)(reg[i] >> 16);
result[i*4+2] = (char)(reg[i] >> 8);
result[i*4+3] = (char)reg[i];
}

}

int main()
{

unsigned char r[32];
sha256("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789", r);
for (int i=0; i<32; i++) {
printf("%x ", r[i]);
}

return 0;
}


• 1
点赞
• 1
收藏
• 打赏
• 1
评论
10-14
10-10
07-03 34万+
11-16
08-09 6841
02-11 5744

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

vcshcn

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。