-
Notifications
You must be signed in to change notification settings - Fork 6
/
sha1.hpp
182 lines (138 loc) · 4.33 KB
/
sha1.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @file sha1.hpp
* @brief
* @date 2019-8-11
* @author Peter Nee, Steve Reid
* @copyright
*
*
* This file refers the source code written by Steve Reid, of which
* you can find the original c-style source file from redis5.0/sha1.
* To use elegantly, the algorithm was transferred to C++-style and then
* optimized by Peter Nee of [ThinkSpirit Laboratory](http://thinkspirit.org/)
* of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/).
*
* Here is the copyright of the original file. Though Steve didn't have any other argument
* about copyright in these files but I sincerely hope that you can retain the
* copyright notice in redistributions of source code. Thank you!
================ sha1.c ================
SHA-1 in C
By Steve Reid <[email protected]>
100% Public Domain
Test Vectors (from FIPS PUB 180-1)
"abc"
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
A million repetitions of "a"
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
================ sha1.h ================
SHA-1 in C
By Steve Reid <[email protected]>
100% Public Domain
*/
#ifndef KERBAL_HASH_SHA1_HPP
#define KERBAL_HASH_SHA1_HPP
#include <kerbal/hash/sha1/sha1_result.hpp>
#include <kerbal/compatibility/constexpr.hpp>
#include <kerbal/compatibility/fixed_width_integer.hpp>
#include <kerbal/compatibility/noexcept.hpp>
#include <cstddef>
namespace kerbal
{
namespace hash
{
using kerbal::compatibility::uint8_t;
using kerbal::compatibility::uint32_t;
class SHA1_context_base
{
public:
typedef SHA1_result result;
protected:
uint32_t state[5];
uint32_t count[2];
uint8_t buffer[64];
# if __cplusplus >= 201103L
constexpr
SHA1_context_base() noexcept;
# else
SHA1_context_base() KERBAL_NOEXCEPT;
# endif
protected:
KERBAL_CONSTEXPR14
static void R1(const uint32_t * w, uint32_t a, uint32_t & b, uint32_t c, uint32_t y, uint32_t & d, std::size_t i) KERBAL_NOEXCEPT;
KERBAL_CONSTEXPR14
static void R2(const uint32_t * w, uint32_t a, uint32_t & b, uint32_t c, uint32_t y, uint32_t & d, std::size_t i) KERBAL_NOEXCEPT;
KERBAL_CONSTEXPR14
static void R3(const uint32_t * w, uint32_t a, uint32_t & b, uint32_t c, uint32_t y, uint32_t & d, std::size_t i) KERBAL_NOEXCEPT;
KERBAL_CONSTEXPR14
static void R4(const uint32_t * w, uint32_t a, uint32_t & b, uint32_t c, uint32_t y, uint32_t & d, std::size_t i) KERBAL_NOEXCEPT;
};
struct SHA1_policy
{
struct fast {};
struct size {};
};
template <typename Policy>
class SHA1_transform_overload;
template <>
class SHA1_transform_overload<SHA1_policy::size> : protected SHA1_context_base
{
protected:
# if __cplusplus >= 201103L
constexpr
SHA1_transform_overload() noexcept = default;
# endif
KERBAL_CONSTEXPR14
void transform(const uint8_t buffer[64]) KERBAL_NOEXCEPT;
};
template <>
class SHA1_transform_overload<SHA1_policy::fast> : protected SHA1_context_base
{
protected:
# if __cplusplus >= 201103L
constexpr
SHA1_transform_overload() noexcept = default;
# endif
KERBAL_CONSTEXPR14
void transform(const uint8_t buffer[64]) KERBAL_NOEXCEPT;
};
template <typename Policy>
class SHA1_context : protected SHA1_transform_overload<Policy>
{
public:
typedef SHA1_result result;
# if __cplusplus >= 201103L
constexpr
SHA1_context() noexcept = default;
# else
SHA1_context() KERBAL_NOEXCEPT
{
}
# endif
private:
template <typename RandomAccessIterator>
KERBAL_CONSTEXPR14
void update_helper(
RandomAccessIterator first, RandomAccessIterator last,
std::random_access_iterator_tag
) KERBAL_NOEXCEPT;
public:
/*
* Run your data through this.
*/
template <typename ForwardIterator> // uint8_t
KERBAL_CONSTEXPR14
void update(ForwardIterator first, ForwardIterator last) KERBAL_NOEXCEPT;
KERBAL_CONSTEXPR14
void update(const uint8_t * first, const uint8_t * last) KERBAL_NOEXCEPT;
/**
* Add padding and return the message digest.
*/
KERBAL_CONSTEXPR14
result digest() KERBAL_NOEXCEPT;
};
} // namespace hash
} // namespace kerbal
#include <kerbal/hash/sha1/sha1.impl.hpp>
#endif // KERBAL_HASH_SHA1_HPP