openMSX
sha1.hh
Go to the documentation of this file.
1 #ifndef SHA1_HH
2 #define SHA1_HH
3 
4 #include "string_ref.hh"
5 #include <ostream>
6 #include <string>
7 #include <cstdint>
8 
9 namespace openmsx {
10 
19 class Sha1Sum
20 {
21 public:
22  struct UninitializedTag {};
24 
25  // note: default copy and assign are ok
26  Sha1Sum();
28  explicit Sha1Sum(string_ref hex);
29 
30  void parse40(const char* str);
31  std::string toString() const;
32 
33  // Test or set 'null' value.
34  bool empty() const;
35  void clear();
36 
37  bool operator==(const Sha1Sum& other) const {
38  for (int i = 0; i < 5; ++i) {
39  if (a[i] != other.a[i]) return false;
40  }
41  return true;
42  }
43  bool operator!=(const Sha1Sum& other) const { return !(*this == other); }
44  bool operator< (const Sha1Sum& other) const {
45  for (int i = 0; i < 5-1; ++i) {
46  if (a[i] != other.a[i]) return a[i] < other.a[i];
47  }
48  return a[5-1] < other.a[5-1];
49  }
50 
51  bool operator<=(const Sha1Sum& other) const { return !(other < *this); }
52  bool operator> (const Sha1Sum& other) const { return (other < *this); }
53  bool operator>=(const Sha1Sum& other) const { return !(*this < other); }
54 
55  friend std::ostream& operator<<(std::ostream& os, const Sha1Sum& sum) {
56  os << sum.toString();
57  return os;
58  }
59 
60 private:
61  uint32_t a[5];
62  friend class SHA1;
63 };
64 
65 
74 class SHA1
75 {
76 public:
77  SHA1();
78 
80  void update(const uint8_t* data, size_t len);
81 
84  Sha1Sum digest();
85 
87  static Sha1Sum calc(const uint8_t* data, size_t len);
88 
89 private:
90  void transform(const uint8_t buffer[64]);
91  void finalize();
92 
93  uint64_t m_count;
94  Sha1Sum m_state;
95  uint8_t m_buffer[64];
96  bool m_finalized;
97 };
98 
99 } // namespace openmsx
100 
101 #endif
bool operator==(const Sha1Sum &other) const
Definition: sha1.hh:37
Sha1Sum(UninitializedTag)
Definition: sha1.hh:23
friend class SHA1
Definition: sha1.hh:62
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
bool operator<(const Sha1Sum &other) const
Definition: sha1.hh:44
T sum(const vecN< N, T > &x)
Definition: gl_vec.hh:300
friend std::ostream & operator<<(std::ostream &os, const Sha1Sum &sum)
Definition: sha1.hh:55
bool operator<=(const Sha1Sum &other) const
Definition: sha1.hh:51
bool empty() const
Definition: sha1.cc:241
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:19
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
Helper class to perform a sha1 calculation.
Definition: sha1.hh:74
bool operator>=(const Sha1Sum &other) const
Definition: sha1.hh:53
std::string toString() const
Definition: sha1.cc:229
void parse40(const char *str)
Definition: sha1.cc:138
uint8_t * data()
void clear()
Definition: sha1.cc:248
bool operator!=(const Sha1Sum &other) const
Definition: sha1.hh:43
bool operator>(const Sha1Sum &other) const
Definition: sha1.hh:52