forked from eteran/cpp-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHA1.h
112 lines (94 loc) · 2.67 KB
/
SHA1.h
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Evan Teran
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef SHA1_20151007_H_
#define SHA1_20151007_H_
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
namespace hash {
class SHA1 {
public:
class State {
friend class SHA1;
public:
State() : length_(0), index_(0) {
}
static constexpr int BlockSize = 64;
private:
uint8_t block_[BlockSize]; // 512-bit message blocks
uint64_t length_; // message length in bits
std::size_t index_; // index into message block array
};
class Digest {
friend class SHA1;
public:
Digest() {
h_[0] = 0x67452301;
h_[1] = 0xefcdab89;
h_[2] = 0x98badcfe;
h_[3] = 0x10325476;
h_[4] = 0xc3d2e1f0;
}
public:
std::string to_string() const;
std::array<uint8_t, 20> bytes() const;
bool operator==(const Digest &rhs) const;
bool operator!=(const Digest &rhs) const;
private:
uint32_t h_[5];
};
public:
template <class In>
SHA1(In first, In last) {
append(first, last);
}
SHA1(const std::string &s);
SHA1() = default;
SHA1(const SHA1 &other) = default;
SHA1 &operator=(const SHA1 &rhs) = default;
public:
template <class In>
SHA1 &append(In first, In last) {
while(first != last) {
append(*first++);
}
return *this;
}
SHA1 &append(uint8_t byte);
SHA1 &append(const std::string &s);
SHA1 &operator<<(uint8_t byte);
public:
void swap(SHA1 &other);
void clear();
Digest finalize() const;
private:
static void processMessageBlock(State *state, Digest *digest);
private:
State state_;
Digest digest_;
};
}
#endif