Nektar++
sha1.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: sha1.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35/*
36 Copyright (c) 2011, Micael Hildenborg
37 All rights reserved.
38
39 Redistribution and use in source and binary forms, with or without
40 modification, are permitted provided that the following conditions are met:
41 * Redistributions of source code must retain the above copyright
42 notice, this list of conditions and the following disclaimer.
43 * Redistributions in binary form must reproduce the above copyright
44 notice, this list of conditions and the following disclaimer in the
45 documentation and/or other materials provided with the distribution.
46 * Neither the name of Micael Hildenborg nor the
47 names of its contributors may be used to endorse or promote products
48 derived from this software without specific prior written permission.
49
50 THIS SOFTWARE IS PROVIDED BY Micael Hildenborg ''AS IS'' AND ANY
51 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 DISCLAIMED. IN NO EVENT SHALL Micael Hildenborg BE LIABLE FOR ANY
54 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 */
61
62/*
63 Contributors:
64 Gustav
65 Several members in the gamedev.se forum.
66 Gregory Petrosyan
67 */
68
69#include "sha1.h"
70
71namespace sha1
72{
73namespace // local
74{
75// Rotate an integer value to left.
76inline unsigned int rol(const unsigned int value, const unsigned int steps)
77{
78 return ((value << steps) | (value >> (32 - steps)));
79}
80
81// Sets the first 16 integers in the buffert to zero.
82// Used for clearing the W buffert.
83inline void clearWBuffert(unsigned int *buffert)
84{
85 for (int pos = 16; --pos >= 0;)
86 {
87 buffert[pos] = 0;
88 }
89}
90
91void innerHash(unsigned int *result, unsigned int *w)
92{
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];
98
99 int round = 0;
100
101#define sha1macro(func, val) \
102 { \
103 const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \
104 e = d; \
105 d = c; \
106 c = rol(b, 30); \
107 b = a; \
108 a = t; \
109 }
110
111 while (round < 16)
112 {
113 sha1macro((b & c) | (~b & d), 0x5a827999)++ round;
114 }
115 while (round < 20)
116 {
117 w[round] = rol(
118 (w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
119 sha1macro((b & c) | (~b & d), 0x5a827999)++ round;
120 }
121 while (round < 40)
122 {
123 w[round] = rol(
124 (w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
125 sha1macro(b ^ c ^ d, 0x6ed9eba1)++ round;
126 }
127 while (round < 60)
128 {
129 w[round] = rol(
130 (w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
131 sha1macro((b & c) | (b & d) | (c & d), 0x8f1bbcdc)++ round;
132 }
133 while (round < 80)
134 {
135 w[round] = rol(
136 (w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
137 sha1macro(b ^ c ^ d, 0xca62c1d6)++ round;
138 }
139
140#undef sha1macro
141
142 result[0] += a;
143 result[1] += b;
144 result[2] += c;
145 result[3] += d;
146 result[4] += e;
147}
148} // namespace
149
150void calc(const void *src, const int bytelength, unsigned char *hash)
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}
212
213void toHexString(const unsigned char *hash, char *hexstring)
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}
224} // namespace sha1
std::vector< double > w(NPUPPER)
std::vector< double > d(NPUPPER *NPUPPER)
Definition: sha1.cpp:72
void toHexString(const unsigned char *hash, char *hexstring)
Calculate a string which represents the SHA1 hash as a hexadecimal number.
Definition: sha1.cpp:213
void calc(const void *src, const int bytelength, unsigned char *hash)
Calculate the SHA1 hash of some data set.
Definition: sha1.cpp:150
#define sha1macro(func, val)