openMSX
sha1.hh
Go to the documentation of this file.
1 #ifndef SHA1_HH
2 #define SHA1_HH
3 
4 #include "string_view.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_view hex);
29 
34  void parse40(const char* str);
35  std::string toString() const;
36 
37  // Test or set 'null' value.
38  bool empty() const;
39  void clear();
40 
41  bool operator==(const Sha1Sum& other) const {
42  for (int i = 0; i < 5; ++i) {
43  if (a[i] != other.a[i]) return false;
44  }
45  return true;
46  }
47  bool operator!=(const Sha1Sum& other) const { return !(*this == other); }
48  bool operator< (const Sha1Sum& other) const {
49  for (int i = 0; i < 5-1; ++i) {
50  if (a[i] != other.a[i]) return a[i] < other.a[i];
51  }
52  return a[5-1] < other.a[5-1];
53  }
54 
55  bool operator<=(const Sha1Sum& other) const { return !(other < *this); }
56  bool operator> (const Sha1Sum& other) const { return (other < *this); }
57  bool operator>=(const Sha1Sum& other) const { return !(*this < other); }
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(const uint8_t* data, size_t len);
85 
88  Sha1Sum digest();
89 
91  static Sha1Sum calc(const uint8_t* data, size_t len);
92 
93 private:
94  void transform(const uint8_t buffer[64]);
95  void finalize();
96 
97  uint64_t m_count;
98  Sha1Sum m_state;
99  uint8_t m_buffer[64];
100  bool m_finalized;
101 };
102 
103 } // namespace openmsx
104 
105 #endif
bool operator==(const Sha1Sum &other) const
Definition: sha1.hh:41
Sha1Sum(UninitializedTag)
Definition: sha1.hh:23
friend class SHA1
Definition: sha1.hh:66
bool operator<(const Sha1Sum &other) const
Definition: sha1.hh:48
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:59
bool operator<=(const Sha1Sum &other) const
Definition: sha1.hh:55
bool empty() const
Definition: utils/sha1.cc:243
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:78
bool operator>=(const Sha1Sum &other) const
Definition: sha1.hh:57
std::string toString() const
Definition: utils/sha1.cc:231
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:15
void parse40(const char *str)
Parse from a 40-character long buffer.
Definition: utils/sha1.cc:140
uint8_t * data()
bool operator!=(const Sha1Sum &other) const
Definition: sha1.hh:47
bool operator>(const Sha1Sum &other) const
Definition: sha1.hh:56