|
|
|
@ -2894,7 +2894,7 @@ unsigned int *ComputeMD5(unsigned char *data, int dataSize) |
|
|
|
// NOTE: Returns a static int[5] array (20 bytes) |
|
|
|
unsigned int *ComputeSHA1(unsigned char *data, int dataSize) |
|
|
|
{ |
|
|
|
#define ROTATE_LEFT(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) |
|
|
|
#define SHA1_ROTATE_LEFT(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) |
|
|
|
|
|
|
|
static unsigned int hash[5] = { 0 }; // Hash to be returned |
|
|
|
|
|
|
|
@ -2937,7 +2937,7 @@ unsigned int *ComputeSHA1(unsigned char *data, int dataSize) |
|
|
|
} |
|
|
|
|
|
|
|
// Message schedule: extend the sixteen 32-bit words into eighty 32-bit words: |
|
|
|
for (int i = 16; i < 80; i++) w[i] = ROTATE_LEFT(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1); |
|
|
|
for (int i = 16; i < 80; i++) w[i] = SHA1_ROTATE_LEFT(w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16], 1); |
|
|
|
|
|
|
|
// Initialize hash value for this chunk |
|
|
|
unsigned int a = hash[0]; |
|
|
|
@ -2972,10 +2972,10 @@ unsigned int *ComputeSHA1(unsigned char *data, int dataSize) |
|
|
|
k = 0xCA62C1D6; |
|
|
|
} |
|
|
|
|
|
|
|
unsigned int temp = ROTATE_LEFT(a, 5) + f + e + k + w[i]; |
|
|
|
unsigned int temp = SHA1_ROTATE_LEFT(a, 5) + f + e + k + w[i]; |
|
|
|
e = d; |
|
|
|
d = c; |
|
|
|
c = ROTATE_LEFT(b, 30); |
|
|
|
c = SHA1_ROTATE_LEFT(b, 30); |
|
|
|
b = a; |
|
|
|
a = temp; |
|
|
|
} |
|
|
|
@ -2997,9 +2997,9 @@ unsigned int *ComputeSHA1(unsigned char *data, int dataSize) |
|
|
|
// NOTE: Returns a static int[8] array (32 bytes) |
|
|
|
unsigned int *ComputeSHA256(unsigned char *data, int dataSize) |
|
|
|
{ |
|
|
|
#define ROTATE_RIGHT(x, c) ((x >> c) | (x << ((sizeof(unsigned int) * 8) - c))) |
|
|
|
#define SHA256_A0(x) (ROTATE_RIGHT(x, 7) ^ ROTATE_RIGHT(x, 18) ^ (x >> 3)) |
|
|
|
#define SHA256_A1(x) (ROTATE_RIGHT(x, 17) ^ ROTATE_RIGHT(x, 19) ^ (x >> 10)) |
|
|
|
#define SHA256_ROTATE_RIGHT(x, c) ((x >> c) | (x << ((sizeof(unsigned int)*8) - c))) |
|
|
|
#define SHA256_A0(x) (SHA256_ROTATE_RIGHT(x, 7) ^ SHA256_ROTATE_RIGHT(x, 18) ^ (x >> 3)) |
|
|
|
#define SHA256_A1(x) (SHA256_ROTATE_RIGHT(x, 17) ^ SHA256_ROTATE_RIGHT(x, 19) ^ (x >> 10)) |
|
|
|
|
|
|
|
static const unsigned int k[64] = { |
|
|
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
|
|
|
@ -3020,7 +3020,7 @@ unsigned int *ComputeSHA256(unsigned char *data, int dataSize) |
|
|
|
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
|
|
|
}; |
|
|
|
|
|
|
|
static unsigned int hash[8]; |
|
|
|
static unsigned int hash[8] = { 0 }; |
|
|
|
hash[0] = 0x6A09e667; |
|
|
|
hash[1] = 0xbb67ae85; |
|
|
|
hash[2] = 0x3c6ef372; |
|
|
|
@ -3033,13 +3033,15 @@ unsigned int *ComputeSHA256(unsigned char *data, int dataSize) |
|
|
|
const unsigned long long int bitLen = ((unsigned long long int)dataSize)*8; |
|
|
|
unsigned long long int paddedSize = dataSize + sizeof(dataSize); |
|
|
|
paddedSize += (64 - (paddedSize%64)); |
|
|
|
unsigned char *buffer = RL_CALLOC(paddedSize, sizeof(unsigned char)); |
|
|
|
unsigned char *buffer = p">(unsigned char *)RL_CALLOC(paddedSize, sizeof(unsigned char)); |
|
|
|
|
|
|
|
memcpy(buffer, data, dataSize); |
|
|
|
buffer[dataSize] = 0x80; |
|
|
|
for (int i = 1; i <= sizeof(bitLen); i++) |
|
|
|
{ |
|
|
|
buffer[(paddedSize - sizeof(bitLen)) + (i - 1)] = (bitLen >> (8*(sizeof(bitLen) - i))) & 0xFF; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (unsigned long long int blockN = 0; blockN < paddedSize/64; blockN++) |
|
|
|
{ |
|
|
|
unsigned int a = hash[0]; |
|
|
|
@ -3052,23 +3054,22 @@ unsigned int *ComputeSHA256(unsigned char *data, int dataSize) |
|
|
|
unsigned int h = hash[7]; |
|
|
|
|
|
|
|
unsigned char *block = buffer + (blockN*64); |
|
|
|
unsigned int w[64]; |
|
|
|
unsigned int w[64] = { 0 }; |
|
|
|
for (int i = 0; i < 16; i++) |
|
|
|
{ |
|
|
|
w[i] = |
|
|
|
((unsigned int)block[i*4 + 0] << 24) | |
|
|
|
((unsigned int)block[i*4 + 1] << 16) | |
|
|
|
((unsigned int)block[i*4 + 2] << 8) | |
|
|
|
((unsigned int)block[i*4 + 3]); |
|
|
|
w[i] = ((unsigned int)block[i*4 + 0] << 24) | |
|
|
|
((unsigned int)block[i*4 + 1] << 16) | |
|
|
|
((unsigned int)block[i*4 + 2] << 8) | |
|
|
|
((unsigned int)block[i*4 + 3]); |
|
|
|
} |
|
|
|
for (int t = 16; t < 64; t++) w[t] = SHA256_A1(w[t - 2]) + w[t - 7] + SHA256_A0(w[t - 15]) + w[t - 16]; |
|
|
|
|
|
|
|
for (unsigned long long int t = 0; t < 64; t++) |
|
|
|
{ |
|
|
|
unsigned int e1 = (ROTATE_RIGHT(e, 6) ^ ROTATE_RIGHT(e, 11) ^ ROTATE_RIGHT(e, 25)); |
|
|
|
unsigned int e1 = (SHA256_ROTATE_RIGHT(e, 6) ^ SHA256_ROTATE_RIGHT(e, 11) ^ SHA256_ROTATE_RIGHT(e, 25)); |
|
|
|
unsigned int ch = ((e & f) ^ (~e & g)); |
|
|
|
unsigned int t1 = (h + e1 + ch + k[t] + w[t]); |
|
|
|
unsigned int e0 = (ROTATE_RIGHT(a, 2) ^ ROTATE_RIGHT(a, 13) ^ ROTATE_RIGHT(a, 22)); |
|
|
|
unsigned int e0 = (SHA256_ROTATE_RIGHT(a, 2) ^ SHA256_ROTATE_RIGHT(a, 13) ^ SHA256_ROTATE_RIGHT(a, 22)); |
|
|
|
unsigned int maj = ((a & b) ^ (a & c) ^ (b & c)); |
|
|
|
unsigned int t2 = e0 + maj; |
|
|
|
|
|
|
|
@ -3091,7 +3092,9 @@ unsigned int *ComputeSHA256(unsigned char *data, int dataSize) |
|
|
|
hash[6] += g; |
|
|
|
hash[7] += h; |
|
|
|
} |
|
|
|
|
|
|
|
RL_FREE(buffer); |
|
|
|
|
|
|
|
return hash; |
|
|
|
} |
|
|
|
|
|
|
|
|