openMSX
sha1.hh
Go to the documentation of this file.
1#ifndef SHA1_HH
2#define SHA1_HH
3
4#include "xrange.hh"
5
6#include <array>
7#include <cstdint>
8#include <ostream>
9#include <span>
10#include <string>
11#include <string_view>
12
13namespace openmsx {
14
24{
25public:
28
29 // note: default copy and assign are ok
30 Sha1Sum();
32 explicit Sha1Sum(std::string_view hex);
33
38 void parse40(std::span<const char, 40> str);
39 [[nodiscard]] std::string toString() const;
40
41 // Test or set 'null' value.
42 [[nodiscard]] bool empty() const;
43 void clear();
44
45 [[nodiscard]] constexpr auto operator<=>(const Sha1Sum&) const = default;
46
47 friend std::ostream& operator<<(std::ostream& os, const Sha1Sum& sum) {
48 os << sum.toString();
49 return os;
50 }
51
52private:
53 std::array<uint32_t, 5> a;
54 friend class SHA1;
55};
56
57
66class SHA1
67{
68public:
69 SHA1();
70
72 void update(std::span<const uint8_t> data);
73
76 [[nodiscard]] Sha1Sum digest();
77
79 [[nodiscard]] static Sha1Sum calc(std::span<const uint8_t> data);
80
81private:
82 void transform(std::span<const uint8_t, 64> buffer);
83 void finalize();
84
85private:
86 uint64_t m_count = 0; // in bytes (sha1 reference implementation counts in bits)
87 Sha1Sum m_state;
88 std::array<uint8_t, 64> m_buffer;
89 bool m_finalized = false;
90};
91
92} // namespace openmsx
93
94#endif
Helper class to perform a sha1 calculation.
Definition sha1.hh:67
Sha1Sum digest()
Get the final hash.
void update(std::span< const uint8_t > data)
Incrementally calculate the hash value.
static Sha1Sum calc(std::span< const uint8_t > data)
Easier to use interface, if you can pass all data in one go.
This class represents the result of a sha1 calculation (a 160-bit value).
Definition sha1.hh:24
Sha1Sum(UninitializedTag)
Definition sha1.hh:27
constexpr auto operator<=>(const Sha1Sum &) const =default
bool empty() const
void parse40(std::span< const char, 40 > str)
Parse from a 40-character long buffer.
friend std::ostream & operator<<(std::ostream &os, const Sha1Sum &sum)
Definition sha1.hh:47
std::string toString() const
This file implemented 3 utility functions:
Definition Autofire.cc:11
constexpr auto sum(InputRange &&range, Proj proj={})
Definition stl.hh:245