2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
20#ifndef DOXYGEN_SHOULD_SKIP_THIS
22#include "utils/sha1.h"
33#define SHA1_ROL(value, bits) (((value) << (bits)) | (((value) & 0xffffffff
) >> (32
- (bits))))
34#define SHA1_BLK(i) (block[i & 15
] = SHA1_ROL(block[(i + 13
) & 15
] ^ block[(i + 8
) & 15
] ^ block[(i + 2
) & 15
] ^ block[i & 15
], 1
))
37#define SHA1_R0(v, w, x, y, z, i)
38 z += ((w & (x ^ y)) ^ y) + block[i] + 0x5a827999
+ SHA1_ROL(v, 5
);
40#define SHA1_R1(v, w, x, y, z, i)
43#define SHA1_R2(v, w, x, y, z, i)
46#define SHA1_R3(v, w, x, y, z, i)
49#define SHA1_R4(v, w, x, y, z, i)
57 void SHA1::update(
const std::string& s) {
58 std::istringstream is(s);
62 void SHA1::update(std::istream& is) {
63 std::string rest_of_buffer;
64 read(is, rest_of_buffer,
static_cast<std::size_t>(
static_cast<
int>(BLOCK_BYTES) -
static_cast<
int>(buffer.size())));
65 buffer += rest_of_buffer;
68 uint32_t block[BLOCK_INTS];
69 buffer_to_block(buffer, block);
71 read(is, buffer, BLOCK_BYTES);
76
77
79 std::string SHA1::final() {
81 const uint64_t total_bits = (transforms * BLOCK_BYTES + buffer.size()) * 8;
84 buffer +=
static_cast<std::string::value_type>(0x80);
85 const std::size_t orig_size = buffer.size();
86 while (buffer.size() < BLOCK_BYTES) {
87 buffer +=
static_cast<
char>(0x00);
90 uint32_t block[BLOCK_INTS];
91 buffer_to_block(buffer, block);
93 if (orig_size > BLOCK_BYTES - 8) {
95 for (
unsigned int i = 0; i < BLOCK_INTS - 2; i++) {
101 block[BLOCK_INTS - 1] =
static_cast<uint32_t>(total_bits & 0x00000000FFFFFFFF);
102 block[BLOCK_INTS - 2] =
static_cast<uint32_t>((total_bits >> 32) & 0x00000000FFFFFFFF);
106 std::ostringstream result;
107 for (
unsigned int i = 0; i < DIGEST_INTS; i++) {
108 result << std::hex << std::setfill(
'0') << std::setw(8);
109 result << (digest[i] & 0xffffffff);
120 digest[0] = 0x67452301;
121 digest[1] = 0xefcdab89;
122 digest[2] = 0x98badcfe;
123 digest[3] = 0x10325476;
124 digest[4] = 0xc3d2e1f0;
132
133
135 void SHA1::transform(uint32_t block[BLOCK_BYTES]) {
137 uint32_t a = digest[0];
138 uint32_t b = digest[1];
139 uint32_t c = digest[2];
140 uint32_t d = digest[3];
141 uint32_t e = digest[4];
236 void SHA1::buffer_to_block(
const std::string& buffer, uint32_t block[BLOCK_BYTES]) {
238 for (
unsigned int i = 0; i < BLOCK_INTS; i++) {
239 block[i] =
static_cast<uint32_t>((buffer[4 * i + 3] & 0xff) | (buffer[4 * i + 2] & 0xff) << 8 |
240 (buffer[4 * i + 1] & 0xff) << 16 | (buffer[4 * i + 0] & 0xff) << 24);
244 void SHA1::read(std::istream& is, std::string& s, std::size_t max) {
245 char* sbuf =
new char[max];
246 is.read(sbuf,
static_cast<std::streamsize>(max));
247 s.assign(sbuf,
static_cast<
unsigned long>(is.gcount()));
252 std::vector<
unsigned char> buf;
257 for (std::size_t i = 0, j = 0; i < string.size(); i += 2, j += 1) {
258 hex_byte[0] = string.at(i);
259 hex_byte[1] = string.at(i + 1);
260 char* end_ptr =
nullptr;
261 buf.push_back(
static_cast<
unsigned char>(strtoul(hex_byte, &end_ptr, 16)));
267 std::vector<
unsigned char> sha1(
const std::string& string) {
269 checksum.update(string);
static std::vector< unsigned char > transform_to_binary(const std::string &string)
#define SHA1_ROL(value, bits)
#define SHA1_R2(v, w, x, y, z, i)
#define SHA1_R3(v, w, x, y, z, i)
#define SHA1_R4(v, w, x, y, z, i)
#define SHA1_R1(v, w, x, y, z, i)
#define SHA1_R0(v, w, x, y, z, i)