openMSX
sha1.hh
Go to the documentation of this file.
1 #ifndef SHA1_HH
2 #define SHA1_HH
3 
4 #include <ostream>
5 #include <string>
6 #include <string_view>
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(std::string_view hex);
29 
34  void parse40(const char* str);
35  [[nodiscard]] std::string toString() const;
36 
37  // Test or set 'null' value.
38  [[nodiscard]] bool empty() const;
39  void clear();
40 
41  [[nodiscard]] 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  [[nodiscard]] bool operator!=(const Sha1Sum& other) const { return !(*this == other); }
48  [[nodiscard]] 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  [[nodiscard]] bool operator<=(const Sha1Sum& other) const { return !(other < *this); }
56  [[nodiscard]] bool operator> (const Sha1Sum& other) const { return (other < *this); }
57  [[nodiscard]] 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  [[nodiscard]] Sha1Sum digest();
89 
91  [[nodiscard]] 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
openmsx::Sha1Sum::operator>
bool operator>(const Sha1Sum &other) const
Definition: sha1.hh:56
openmsx::SHA1
Helper class to perform a sha1 calculation.
Definition: sha1.hh:79
openmsx::Sha1Sum::Sha1Sum
Sha1Sum()
Definition: utils/sha1.cc:98
openmsx::Sha1Sum::operator>=
bool operator>=(const Sha1Sum &other) const
Definition: sha1.hh:57
openmsx::Sha1Sum::toString
std::string toString() const
Definition: utils/sha1.cc:232
openmsx::Sha1Sum::clear
void clear()
Definition: utils/sha1.cc:248
openmsx::Sha1Sum::UninitializedTag
Definition: sha1.hh:22
openmsx::Sha1Sum::operator==
bool operator==(const Sha1Sum &other) const
Definition: sha1.hh:41
openmsx::SHA1::digest
Sha1Sum digest()
Get the final hash.
Definition: utils/sha1.cc:355
openmsx::SHA1::SHA1
SHA1()
Definition: utils/sha1.cc:256
openmsx::Sha1Sum::operator!=
bool operator!=(const Sha1Sum &other) const
Definition: sha1.hh:47
openmsx::Sha1Sum::Sha1Sum
Sha1Sum(UninitializedTag)
Definition: sha1.hh:23
openmsx::Sha1Sum::operator<
bool operator<(const Sha1Sum &other) const
Definition: sha1.hh:48
openmsx::Sha1Sum
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:20
openmsx::Sha1Sum::operator<<
friend std::ostream & operator<<(std::ostream &os, const Sha1Sum &sum)
Definition: sha1.hh:59
openmsx::SHA1::calc
static Sha1Sum calc(const uint8_t *data, size_t len)
Easier to use interface, if you can pass all data in one go.
Definition: utils/sha1.cc:361
openmsx::Sha1Sum::operator<=
bool operator<=(const Sha1Sum &other) const
Definition: sha1.hh:55
openmsx::Sha1Sum::empty
bool empty() const
Definition: utils/sha1.cc:244
openmsx::Sha1Sum::parse40
void parse40(const char *str)
Parse from a 40-character long buffer.
Definition: utils/sha1.cc:141
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
sum
auto sum(InputRange &&range)
Definition: stl.hh:288
openmsx::SHA1::update
void update(const uint8_t *data, size_t len)
Incrementally calculate the hash value.
Definition: utils/sha1.cc:318