76inline unsigned int rol(
const unsigned int value,
const unsigned int steps)
78 return ((value << steps) | (value >> (32 - steps)));
83inline void clearWBuffert(
unsigned int *buffert)
85 for (
int pos = 16; --pos >= 0;)
91void innerHash(
unsigned int *result,
unsigned int *
w)
93 unsigned int a = result[0];
94 unsigned int b = result[1];
95 unsigned int c = result[2];
96 unsigned int d = result[3];
97 unsigned int e = result[4];
101#define sha1macro(func, val) \
103 const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \
113 sha1macro((b & c) | (~b &
d), 0x5a827999)++ round;
118 (
w[round - 3] ^
w[round - 8] ^
w[round - 14] ^
w[round - 16]), 1);
119 sha1macro((b & c) | (~b &
d), 0x5a827999)++ round;
124 (
w[round - 3] ^
w[round - 8] ^
w[round - 14] ^
w[round - 16]), 1);
130 (
w[round - 3] ^
w[round - 8] ^
w[round - 14] ^
w[round - 16]), 1);
131 sha1macro((b & c) | (b &
d) | (c &
d), 0x8f1bbcdc)++ round;
136 (
w[round - 3] ^
w[round - 8] ^
w[round - 14] ^
w[round - 16]), 1);
150void calc(
const void *src,
const int bytelength,
unsigned char *hash)
153 unsigned int result[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476,
157 const unsigned char *sarray = (
const unsigned char *)src;
163 const int endOfFullBlocks = bytelength - 64;
165 int currentBlock = 0;
167 while (currentBlock <= endOfFullBlocks)
169 endCurrentBlock = currentBlock + 64;
172 for (
int roundPos = 0; currentBlock < endCurrentBlock;
177 w[roundPos++] = (
unsigned int)sarray[currentBlock + 3] |
178 (((
unsigned int)sarray[currentBlock + 2]) << 8) |
179 (((
unsigned int)sarray[currentBlock + 1]) << 16) |
180 (((
unsigned int)sarray[currentBlock]) << 24);
182 innerHash(result,
w);
186 endCurrentBlock = bytelength - currentBlock;
188 int lastBlockBytes = 0;
189 for (; lastBlockBytes < endCurrentBlock; ++lastBlockBytes)
191 w[lastBlockBytes >> 2] |=
192 (
unsigned int)sarray[lastBlockBytes + currentBlock]
193 << ((3 - (lastBlockBytes & 3)) << 3);
195 w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
196 if (endCurrentBlock >= 56)
198 innerHash(result,
w);
201 w[15] = bytelength << 3;
202 innerHash(result,
w);
206 for (
int hashByte = 20; --hashByte >= 0;)
209 (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
215 const char hexDigits[] = {
"0123456789abcdef"};
217 for (
int hashByte = 20; --hashByte >= 0;)
219 hexstring[hashByte << 1] = hexDigits[(hash[hashByte] >> 4) & 0xf];
220 hexstring[(hashByte << 1) + 1] = hexDigits[hash[hashByte] & 0xf];
std::vector< double > w(NPUPPER)
std::vector< double > d(NPUPPER *NPUPPER)
void toHexString(const unsigned char *hash, char *hexstring)
Calculate a string which represents the SHA1 hash as a hexadecimal number.
void calc(const void *src, const int bytelength, unsigned char *hash)
Calculate the SHA1 hash of some data set.
#define sha1macro(func, val)