45 inline unsigned int rol(
unsigned int value,
unsigned int steps) {
46 return ((value << steps) | (value >> (32 - steps)));
51 inline void clearWBuffert(
unsigned int * buffert)
53 for (
int pos = 16; --pos >= 0;)
59 inline void innerHash(
unsigned int * result,
unsigned int * w)
61 unsigned int a = result[0];
62 unsigned int b = result[1];
63 unsigned int c = result[2];
64 unsigned int d = result[3];
65 unsigned int e = result[4];
69 #define sha1macro(func,val) \ 71 const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \ 86 w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
92 w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
98 w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
99 sha1macro((b & c) | (b & d) | (c & d), 0x8f1bbcdc)
104 w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
127 inline void calc(
void const * src,
size_t bytelength,
unsigned char * hash) {
129 unsigned int result[5] = { 0x67452301, 0xefcdab89, 0x98badcfe,
130 0x10325476, 0xc3d2e1f0 };
133 unsigned char const * sarray = (
unsigned char const *) src;
140 size_t endCurrentBlock;
141 size_t currentBlock = 0;
143 if (bytelength >= 64) {
144 size_t const endOfFullBlocks = bytelength - 64;
146 while (currentBlock <= endOfFullBlocks) {
147 endCurrentBlock = currentBlock + 64;
150 for (
int roundPos = 0; currentBlock < endCurrentBlock; currentBlock += 4)
154 w[roundPos++] = (
unsigned int) sarray[currentBlock + 3]
155 | (((
unsigned int) sarray[currentBlock + 2]) << 8)
156 | (((
unsigned int) sarray[currentBlock + 1]) << 16)
157 | (((
unsigned int) sarray[currentBlock]) << 24);
159 innerHash(result, w);
164 endCurrentBlock = bytelength - currentBlock;
166 size_t lastBlockBytes = 0;
167 for (;lastBlockBytes < endCurrentBlock; ++lastBlockBytes) {
168 w[lastBlockBytes >> 2] |= (
unsigned int) sarray[lastBlockBytes + currentBlock] << ((3 - (lastBlockBytes & 3)) << 3);
171 w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
172 if (endCurrentBlock >= 56) {
173 innerHash(result, w);
176 w[15] = bytelength << 3;
177 innerHash(result, w);
181 for (
int hashByte = 20; --hashByte >= 0;) {
182 hash[hashByte] = (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
189 #endif // SHA1_DEFINED #define sha1macro(func, val)
Namespace for the WebSocket++ project.
void calc(void const *src, size_t bytelength, unsigned char *hash)
Calculate a SHA1 hash.