sha1.hpp
Go to the documentation of this file.
1 /*
2  From: https://github.com/vog/sha1/blob/3f8a4aa032d144309d00dbfe972906a49b3631b9/sha1.hpp
3  sha1.hpp - source code of
4 
5  ============
6  SHA-1 in C++
7  ============
8 
9  100% Public Domain.
10 
11  Original C Code
12  -- Steve Reid <steve@edmweb.com>
13  Small changes to fit into bglibs
14  -- Bruce Guenter <bruce@untroubled.org>
15  Translation to simpler C++ Code
16  -- Volker Diels-Grabsch <v@njh.eu>
17  Safety fixes
18  -- Eugene Hopkinson <slowriot at voxelstorm dot com>
19  Header-only library
20  -- Zlatko Michailov <zlatko@michailov.org>
21 */
22 
23 #ifndef SHA1_HPP
24 #define SHA1_HPP
25 
26 
27 #include <cstdint>
28 #include <fstream>
29 #include <iomanip>
30 #include <iostream>
31 #include <sstream>
32 #include <string>
33 
34 
35 class SHA1
36 {
37 public:
38  SHA1();
39  void update(const std::string &s);
40  void update(std::istream &is);
41  std::string final();
42  static std::string from_file(const std::string &filename);
43 
44 private:
45  uint32_t digest[5];
46  std::string buffer;
47  uint64_t transforms;
48 };
49 
50 
51 static const size_t BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */
52 static const size_t BLOCK_BYTES = BLOCK_INTS * 4;
53 
54 
55 inline static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
56 {
57  /* SHA1 initialization constants */
58  digest[0] = 0x67452301;
59  digest[1] = 0xefcdab89;
60  digest[2] = 0x98badcfe;
61  digest[3] = 0x10325476;
62  digest[4] = 0xc3d2e1f0;
63 
64  /* Reset counters */
65  buffer = "";
66  transforms = 0;
67 }
68 
69 
70 inline static uint32_t rol(const uint32_t value, const size_t bits)
71 {
72  return (value << bits) | (value >> (32 - bits));
73 }
74 
75 
76 inline static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i)
77 {
78  return rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1);
79 }
80 
81 
82 /*
83  * (R0+R1), R2, R3, R4 are the different operations used in SHA1
84  */
85 
86 inline static void R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
87 {
88  z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5);
89  w = rol(w, 30);
90 }
91 
92 
93 inline static void R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
94 {
95  block[i] = blk(block, i);
96  z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5);
97  w = rol(w, 30);
98 }
99 
100 
101 inline static void R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
102 {
103  block[i] = blk(block, i);
104  z += (w^x^y) + block[i] + 0x6ed9eba1 + rol(v, 5);
105  w = rol(w, 30);
106 }
107 
108 
109 inline static void R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
110 {
111  block[i] = blk(block, i);
112  z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rol(v, 5);
113  w = rol(w, 30);
114 }
115 
116 
117 inline static void R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
118 {
119  block[i] = blk(block, i);
120  z += (w^x^y) + block[i] + 0xca62c1d6 + rol(v, 5);
121  w = rol(w, 30);
122 }
123 
124 
125 /*
126  * Hash a single 512-bit block. This is the core of the algorithm.
127  */
128 
129 inline static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms)
130 {
131  /* Copy digest[] to working vars */
132  uint32_t a = digest[0];
133  uint32_t b = digest[1];
134  uint32_t c = digest[2];
135  uint32_t d = digest[3];
136  uint32_t e = digest[4];
137 
138  /* 4 rounds of 20 operations each. Loop unrolled. */
139  R0(block, a, b, c, d, e, 0);
140  R0(block, e, a, b, c, d, 1);
141  R0(block, d, e, a, b, c, 2);
142  R0(block, c, d, e, a, b, 3);
143  R0(block, b, c, d, e, a, 4);
144  R0(block, a, b, c, d, e, 5);
145  R0(block, e, a, b, c, d, 6);
146  R0(block, d, e, a, b, c, 7);
147  R0(block, c, d, e, a, b, 8);
148  R0(block, b, c, d, e, a, 9);
149  R0(block, a, b, c, d, e, 10);
150  R0(block, e, a, b, c, d, 11);
151  R0(block, d, e, a, b, c, 12);
152  R0(block, c, d, e, a, b, 13);
153  R0(block, b, c, d, e, a, 14);
154  R0(block, a, b, c, d, e, 15);
155  R1(block, e, a, b, c, d, 0);
156  R1(block, d, e, a, b, c, 1);
157  R1(block, c, d, e, a, b, 2);
158  R1(block, b, c, d, e, a, 3);
159  R2(block, a, b, c, d, e, 4);
160  R2(block, e, a, b, c, d, 5);
161  R2(block, d, e, a, b, c, 6);
162  R2(block, c, d, e, a, b, 7);
163  R2(block, b, c, d, e, a, 8);
164  R2(block, a, b, c, d, e, 9);
165  R2(block, e, a, b, c, d, 10);
166  R2(block, d, e, a, b, c, 11);
167  R2(block, c, d, e, a, b, 12);
168  R2(block, b, c, d, e, a, 13);
169  R2(block, a, b, c, d, e, 14);
170  R2(block, e, a, b, c, d, 15);
171  R2(block, d, e, a, b, c, 0);
172  R2(block, c, d, e, a, b, 1);
173  R2(block, b, c, d, e, a, 2);
174  R2(block, a, b, c, d, e, 3);
175  R2(block, e, a, b, c, d, 4);
176  R2(block, d, e, a, b, c, 5);
177  R2(block, c, d, e, a, b, 6);
178  R2(block, b, c, d, e, a, 7);
179  R3(block, a, b, c, d, e, 8);
180  R3(block, e, a, b, c, d, 9);
181  R3(block, d, e, a, b, c, 10);
182  R3(block, c, d, e, a, b, 11);
183  R3(block, b, c, d, e, a, 12);
184  R3(block, a, b, c, d, e, 13);
185  R3(block, e, a, b, c, d, 14);
186  R3(block, d, e, a, b, c, 15);
187  R3(block, c, d, e, a, b, 0);
188  R3(block, b, c, d, e, a, 1);
189  R3(block, a, b, c, d, e, 2);
190  R3(block, e, a, b, c, d, 3);
191  R3(block, d, e, a, b, c, 4);
192  R3(block, c, d, e, a, b, 5);
193  R3(block, b, c, d, e, a, 6);
194  R3(block, a, b, c, d, e, 7);
195  R3(block, e, a, b, c, d, 8);
196  R3(block, d, e, a, b, c, 9);
197  R3(block, c, d, e, a, b, 10);
198  R3(block, b, c, d, e, a, 11);
199  R4(block, a, b, c, d, e, 12);
200  R4(block, e, a, b, c, d, 13);
201  R4(block, d, e, a, b, c, 14);
202  R4(block, c, d, e, a, b, 15);
203  R4(block, b, c, d, e, a, 0);
204  R4(block, a, b, c, d, e, 1);
205  R4(block, e, a, b, c, d, 2);
206  R4(block, d, e, a, b, c, 3);
207  R4(block, c, d, e, a, b, 4);
208  R4(block, b, c, d, e, a, 5);
209  R4(block, a, b, c, d, e, 6);
210  R4(block, e, a, b, c, d, 7);
211  R4(block, d, e, a, b, c, 8);
212  R4(block, c, d, e, a, b, 9);
213  R4(block, b, c, d, e, a, 10);
214  R4(block, a, b, c, d, e, 11);
215  R4(block, e, a, b, c, d, 12);
216  R4(block, d, e, a, b, c, 13);
217  R4(block, c, d, e, a, b, 14);
218  R4(block, b, c, d, e, a, 15);
219 
220  /* Add the working vars back into digest[] */
221  digest[0] += a;
222  digest[1] += b;
223  digest[2] += c;
224  digest[3] += d;
225  digest[4] += e;
226 
227  /* Count the number of transformations */
228  transforms++;
229 }
230 
231 
232 inline static void buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS])
233 {
234  /* Convert the std::string (byte buffer) to a uint32_t array (MSB) */
235  for (size_t i = 0; i < BLOCK_INTS; i++)
236  {
237  block[i] = (buffer[4*i+3] & 0xff)
238  | (buffer[4*i+2] & 0xff)<<8
239  | (buffer[4*i+1] & 0xff)<<16
240  | (buffer[4*i+0] & 0xff)<<24;
241  }
242 }
243 
244 
245 inline SHA1::SHA1()
246 {
248 }
249 
250 
251 inline void SHA1::update(const std::string &s)
252 {
253  std::istringstream is(s);
254  update(is);
255 }
256 
257 
258 inline void SHA1::update(std::istream &is)
259 {
260  while (true)
261  {
262  char sbuf[BLOCK_BYTES];
263  is.read(sbuf, BLOCK_BYTES - buffer.size());
264  buffer.append(sbuf, (std::size_t)is.gcount());
265  if (buffer.size() != BLOCK_BYTES)
266  {
267  return;
268  }
269  uint32_t block[BLOCK_INTS];
270  buffer_to_block(buffer, block);
271  transform(digest, block, transforms);
272  buffer.clear();
273  }
274 }
275 
276 
277 /*
278  * Add padding and return the message digest.
279  */
280 
281 inline std::string SHA1::final()
282 {
283  /* Total number of hashed bits */
284  uint64_t total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8;
285 
286  /* Padding */
287  buffer += (char)0x80;
288  size_t orig_size = buffer.size();
289  while (buffer.size() < BLOCK_BYTES)
290  {
291  buffer += (char)0x00;
292  }
293 
294  uint32_t block[BLOCK_INTS];
295  buffer_to_block(buffer, block);
296 
297  if (orig_size > BLOCK_BYTES - 8)
298  {
299  transform(digest, block, transforms);
300  for (size_t i = 0; i < BLOCK_INTS - 2; i++)
301  {
302  block[i] = 0;
303  }
304  }
305 
306  /* Append total_bits, split this uint64_t into two uint32_t */
307  block[BLOCK_INTS - 1] = (uint32_t)total_bits;
308  block[BLOCK_INTS - 2] = (uint32_t)(total_bits >> 32);
309  transform(digest, block, transforms);
310 
311  /* Hex std::string */
312  std::ostringstream result;
313  for (size_t i = 0; i < sizeof(digest) / sizeof(digest[0]); i++)
314  {
315  result << std::hex << std::setfill('0') << std::setw(8);
316  result << digest[i];
317  }
318 
319  /* Reset for next run */
321 
322  return result.str();
323 }
324 
325 
326 inline std::string SHA1::from_file(const std::string &filename)
327 {
328  std::ifstream stream(filename.c_str(), std::ios::binary);
329  SHA1 checksum;
330  checksum.update(stream);
331  return checksum.final();
332 }
333 
334 
335 #endif /* SHA1_HPP */
SHA1::buffer
std::string buffer
Definition: sha1.hpp:46
buffer_to_block
static void buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS])
Definition: sha1.hpp:232
SHA1::SHA1
SHA1()
Definition: sha1.hpp:245
SHA1::update
void update(const std::string &s)
Definition: sha1.hpp:251
rol
static uint32_t rol(const uint32_t value, const size_t bits)
Definition: sha1.hpp:70
SHA1::from_file
static std::string from_file(const std::string &filename)
Definition: sha1.hpp:326
dai::utility::checksum
std::uint32_t checksum(const void *buffer, std::size_t size, uint32_t prevChecksum)
Definition: Checksum.cpp:6
nanorpc::core::type::buffer
std::vector< std::uint8_t > buffer
Definition: type.h:28
BLOCK_BYTES
static const size_t BLOCK_BYTES
Definition: sha1.hpp:52
SHA1::digest
uint32_t digest[5]
Definition: sha1.hpp:45
R3
static void R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
Definition: sha1.hpp:109
R1
static void R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
Definition: sha1.hpp:93
BLOCK_INTS
static const size_t BLOCK_INTS
Definition: sha1.hpp:51
SHA1::transforms
uint64_t transforms
Definition: sha1.hpp:47
SHA1::final
std::string final()
Definition: sha1.hpp:281
transform
static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms)
Definition: sha1.hpp:129
R2
static void R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
Definition: sha1.hpp:101
reset
static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
Definition: sha1.hpp:55
R4
static void R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
Definition: sha1.hpp:117
blk
static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i)
Definition: sha1.hpp:76
R0
static void R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
Definition: sha1.hpp:86
SHA1
Definition: sha1.hpp:35


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19