openMSX
sha1.hh
Go to the documentation of this file.
1 #ifndef SHA1_HH
2 #define SHA1_HH
3 
4 #include "xrange.hh"
5 #include <cstdint>
6 #include <ostream>
7 #include <span>
8 #include <string>
9 #include <string_view>
10 
11 namespace openmsx {
12 
21 class Sha1Sum
22 {
23 public:
24  struct UninitializedTag {};
26 
27  // note: default copy and assign are ok
28  Sha1Sum();
30  explicit Sha1Sum(std::string_view hex);
31 
36  void parse40(const char* str);
37  [[nodiscard]] std::string toString() const;
38 
39  // Test or set 'null' value.
40  [[nodiscard]] bool empty() const;
41  void clear();
42 
43  // gcc-10.2 miscompiles this (fixed in gcc-11),
44  // so still manually implement operator==.
45  //[[nodiscard]] constexpr bool operator==(const Sha1Sum&) const = default;
46  [[nodiscard]] bool operator==(const Sha1Sum& other) const {
47  for (int i : xrange(5)) {
48  if (a[i] != other.a[i]) return false;
49  }
50  return true;
51  }
52  [[nodiscard]] constexpr auto operator<=>(const Sha1Sum& other) const {
53  for (int i : xrange(5 - 1)) {
54  if (auto cmp = a[i] <=> other.a[i]; cmp != 0) return cmp;
55  }
56  return a[5 - 1] <=> other.a[5 - 1];
57  }
58 
59  friend std::ostream& operator<<(std::ostream& os, const Sha1Sum& sum) {
60  os << sum.toString();
61  return os;
62  }
63 
64 private:
65  uint32_t a[5];
66  friend class SHA1;
67 };
68 
69 
78 class SHA1
79 {
80 public:
81  SHA1();
82 
84  void update(std::span<const uint8_t> data);
85 
88  [[nodiscard]] Sha1Sum digest();
89 
91  [[nodiscard]] static Sha1Sum calc(std::span<const uint8_t> data);
92 
93 private:
94  void transform(const uint8_t buffer[64]);
95  void finalize();
96 
97 private:
98  uint64_t m_count; // in bytes (sha1 reference implementation counts in bits)
99  Sha1Sum m_state;
100  uint8_t m_buffer[64];
101  bool m_finalized;
102 };
103 
104 } // namespace openmsx
105 
106 #endif
Helper class to perform a sha1 calculation.
Definition: sha1.hh:79
Sha1Sum digest()
Get the final hash.
Definition: utils/sha1.cc:357
void update(std::span< const uint8_t > data)
Incrementally calculate the hash value.
Definition: utils/sha1.cc:314
static Sha1Sum calc(std::span< const uint8_t > data)
Easier to use interface, if you can pass all data in one go.
Definition: utils/sha1.cc:363
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:22
friend std::ostream & operator<<(std::ostream &os, const Sha1Sum &sum)
Definition: sha1.hh:59
bool operator==(const Sha1Sum &other) const
Definition: sha1.hh:46
Sha1Sum(UninitializedTag)
Definition: sha1.hh:25
bool empty() const
Definition: utils/sha1.cc:241
constexpr auto operator<=>(const Sha1Sum &other) const
Definition: sha1.hh:52
void parse40(const char *str)
Parse from a 40-character long buffer.
Definition: utils/sha1.cc:138
std::string toString() const
Definition: utils/sha1.cc:229
This file implemented 3 utility functions:
Definition: Autofire.cc:9
auto sum(InputRange &&range, Proj proj={})
Definition: stl.hh:236
constexpr auto xrange(T e)
Definition: xrange.hh:133