Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
websocketpp::sha1 Namespace Reference

Functions

void calc (void const *src, size_t bytelength, unsigned char *hash)
 Calculate a SHA1 hash.
 

Function Documentation

◆ calc()

void websocketpp::sha1::calc ( void const * src,
size_t bytelength,
unsigned char * hash )
inline
Parameters
srcpoints to any kind of data to be hashed.
bytelengththe number of bytes to hash from the src pointer.
hashshould point to a buffer of at least 20 bytes of size for storing the sha1 result in.

Definition at line 127 of file sha1.hpp.

127 {
128 // Init the result array.
129 unsigned int result[5] = { 0x67452301, 0xefcdab89, 0x98badcfe,
130 0x10325476, 0xc3d2e1f0 };
131
132 // Cast the void src pointer to be the byte array we can work with.
133 unsigned char const * sarray = (unsigned char const *) src;
134
135 // The reusable round buffer
136 unsigned int w[80];
137
138 // Loop through all complete 64byte blocks.
139
140 size_t endCurrentBlock;
141 size_t currentBlock = 0;
142
143 if (bytelength >= 64) {
144 size_t const endOfFullBlocks = bytelength - 64;
145
146 while (currentBlock <= endOfFullBlocks) {
147 endCurrentBlock = currentBlock + 64;
148
149 // Init the round buffer with the 64 byte block data.
150 for (int roundPos = 0; currentBlock < endCurrentBlock; currentBlock += 4)
151 {
152 // This line will swap endian on big endian and keep endian on
153 // little endian.
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);
158 }
159 innerHash(result, w);
160 }
161 }
162
163 // Handle the last and not full 64 byte block if existing.
164 endCurrentBlock = bytelength - currentBlock;
165 clearWBuffert(w);
166 size_t lastBlockBytes = 0;
167 for (;lastBlockBytes < endCurrentBlock; ++lastBlockBytes) {
168 w[lastBlockBytes >> 2] |= (unsigned int) sarray[lastBlockBytes + currentBlock] << ((3 - (lastBlockBytes & 3)) << 3);
169 }
170
171 w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
172 if (endCurrentBlock >= 56) {
173 innerHash(result, w);
174 clearWBuffert(w);
175 }
176 w[15] = bytelength << 3;
177 innerHash(result, w);
178
179 // Store hash in result pointer, and make sure we get in in the correct
180 // order on both endian models.
181 for (int hashByte = 20; --hashByte >= 0;) {
182 hash[hashByte] = (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
183 }
184}
Here is the caller graph for this function: