openMSX
sha1.hh
Go to the documentation of this file.
1 #ifndef SHA1_HH
2 #define SHA1_HH
3 
4 #include "span.hh"
5 #include "xrange.hh"
6 #include <ostream>
7 #include <string>
8 #include <string_view>
9 #include <cstdint>
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  [[nodiscard]] bool operator==(const Sha1Sum& other) const {
44  for (int i : xrange(5)) {
45  if (a[i] != other.a[i]) return false;
46  }
47  return true;
48  }
49  [[nodiscard]] bool operator!=(const Sha1Sum& other) const { return !(*this == other); }
50  [[nodiscard]] bool operator< (const Sha1Sum& other) const {
51  for (int i : xrange(5 - 1)) {
52  if (a[i] != other.a[i]) return a[i] < other.a[i];
53  }
54  return a[5 - 1] < other.a[5 - 1];
55  }
56 
57  [[nodiscard]] bool operator<=(const Sha1Sum& other) const { return !(other < *this); }
58  [[nodiscard]] bool operator> (const Sha1Sum& other) const { return (other < *this); }
59  [[nodiscard]] bool operator>=(const Sha1Sum& other) const { return !(*this < other); }
60 
61  friend std::ostream& operator<<(std::ostream& os, const Sha1Sum& sum) {
62  os << sum.toString();
63  return os;
64  }
65 
66 private:
67  uint32_t a[5];
68  friend class SHA1;
69 };
70 
71 
80 class SHA1
81 {
82 public:
83  SHA1();
84 
86  void update(span<const uint8_t> data);
87 
90  [[nodiscard]] Sha1Sum digest();
91 
93  [[nodiscard]] static Sha1Sum calc(span<const uint8_t> data);
94 
95 private:
96  void transform(const uint8_t buffer[64]);
97  void finalize();
98 
99 private:
100  uint64_t m_count; // in bytes (sha1 reference implementation counts in bits)
101  Sha1Sum m_state;
102  uint8_t m_buffer[64];
103  bool m_finalized;
104 };
105 
106 } // namespace openmsx
107 
108 #endif
Helper class to perform a sha1 calculation.
Definition: sha1.hh:81
Sha1Sum digest()
Get the final hash.
Definition: utils/sha1.cc:359
void update(span< const uint8_t > data)
Incrementally calculate the hash value.
Definition: utils/sha1.cc:316
static Sha1Sum calc(span< const uint8_t > data)
Easier to use interface, if you can pass all data in one go.
Definition: utils/sha1.cc:365
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:61
bool operator==(const Sha1Sum &other) const
Definition: sha1.hh:43
bool operator<=(const Sha1Sum &other) const
Definition: sha1.hh:57
Sha1Sum(UninitializedTag)
Definition: sha1.hh:25
bool empty() const
Definition: utils/sha1.cc:242
bool operator<(const Sha1Sum &other) const
Definition: sha1.hh:50
bool operator!=(const Sha1Sum &other) const
Definition: sha1.hh:49
bool operator>(const Sha1Sum &other) const
Definition: sha1.hh:58
void parse40(const char *str)
Parse from a 40-character long buffer.
Definition: utils/sha1.cc:139
bool operator>=(const Sha1Sum &other) const
Definition: sha1.hh:59
std::string toString() const
Definition: utils/sha1.cc:230
This file implemented 3 utility functions:
Definition: Autofire.cc:9
auto sum(InputRange &&range, Proj proj={})
Definition: stl.hh:245
constexpr auto xrange(T e)
Definition: xrange.hh:155