Nektar++
Functions
sha1 Namespace Reference

Functions

void calc (const void *src, const int bytelength, unsigned char *hash)
 Calculate the SHA1 hash of some data set. More...
 
void toHexString (const unsigned char *hash, char *hexstring)
 Calculate a string which represents the SHA1 hash as a hexadecimal number. More...
 

Function Documentation

◆ calc()

void sha1::calc ( const void *  src,
const int  bytelength,
unsigned char *  hash 
)

Calculate the SHA1 hash of some data set.

Parameters
srcPoints to any kind of data to be hashed.
bytelengthThe number of bytes to hash from the src pointer.
hashPoints to a buffer of at least 20 bytes of size for storing the sha1 result in.

Definition at line 150 of file sha1.cpp.

151{
152 // Init the result array.
153 unsigned int result[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476,
154 0xc3d2e1f0};
155
156 // Cast the void src pointer to be the byte array we can work with.
157 const unsigned char *sarray = (const unsigned char *)src;
158
159 // The reusable round buffer
160 unsigned int w[80];
161
162 // Loop through all complete 64byte blocks.
163 const int endOfFullBlocks = bytelength - 64;
164 int endCurrentBlock;
165 int currentBlock = 0;
166
167 while (currentBlock <= endOfFullBlocks)
168 {
169 endCurrentBlock = currentBlock + 64;
170
171 // Init the round buffer with the 64 byte block data.
172 for (int roundPos = 0; currentBlock < endCurrentBlock;
173 currentBlock += 4)
174 {
175 // This line will swap endian on big endian and keep endian on
176 // little endian.
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);
181 }
182 innerHash(result, w);
183 }
184
185 // Handle the last and not full 64 byte block if existing.
186 endCurrentBlock = bytelength - currentBlock;
187 clearWBuffert(w);
188 int lastBlockBytes = 0;
189 for (; lastBlockBytes < endCurrentBlock; ++lastBlockBytes)
190 {
191 w[lastBlockBytes >> 2] |=
192 (unsigned int)sarray[lastBlockBytes + currentBlock]
193 << ((3 - (lastBlockBytes & 3)) << 3);
194 }
195 w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
196 if (endCurrentBlock >= 56)
197 {
198 innerHash(result, w);
199 clearWBuffert(w);
200 }
201 w[15] = bytelength << 3;
202 innerHash(result, w);
203
204 // Store hash in result pointer, and make sure we get in in the correct
205 // order on both endian models.
206 for (int hashByte = 20; --hashByte >= 0;)
207 {
208 hash[hashByte] =
209 (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
210 }
211}
std::vector< double > w(NPUPPER)

References Nektar::UnitTests::w().

Referenced by Nektar::MetricFile::CalculateHash().

◆ toHexString()

void sha1::toHexString ( const unsigned char *  hash,
char *  hexstring 
)

Calculate a string which represents the SHA1 hash as a hexadecimal number.

Parameters
hashis 20 bytes of sha1 hash. This is the same data that is the result from the calc function.
hexstringshould point to a buffer of at least 41 bytes of size for storing the hexadecimal representation of the hash. A zero will be written at position 40, so the buffer will be a valid zero ended string.

Definition at line 213 of file sha1.cpp.

214{
215 const char hexDigits[] = {"0123456789abcdef"};
216
217 for (int hashByte = 20; --hashByte >= 0;)
218 {
219 hexstring[hashByte << 1] = hexDigits[(hash[hashByte] >> 4) & 0xf];
220 hexstring[(hashByte << 1) + 1] = hexDigits[hash[hashByte] & 0xf];
221 }
222 hexstring[40] = 0;
223}

Referenced by Nektar::MetricFile::CalculateHash().