| e8555c89 Andy McCall |
1 |
#ifndef SHA1_HH |
| 2 |
#define SHA1_HH |
| 3 |
|
| 4 |
#include "serialize.hh" |
| 5 |
#include "strCat.hh" |
| 6 |
|
| 7 |
#include <array> |
| 8 |
#include <cstdint> |
| 9 |
#include <ostream> |
| 10 |
#include <span> |
| 11 |
#include <string> |
| 12 |
#include <string_view> |
| 13 |
|
| 14 |
namespace openmsx { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class Sha1Sum |
| 25 |
{ |
| 26 |
public: |
| 27 |
struct UninitializedTag {}; |
| 28 |
explicit Sha1Sum(UninitializedTag) {} |
| 29 |
|
| 30 |
|
| 31 |
Sha1Sum(); |
| 32 |
|
| 33 |
explicit Sha1Sum(std::string_view hex); |
| 34 |
void parse(std::string_view hex); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
void parse40(std::span<const char, 40> str); |
| 41 |
|
| 42 |
void toBuffer(std::span<char, 40> buf) const; |
| 43 |
[[nodiscard]] std::string_view toString(std::span<char, 40> buf) const { |
| 44 |
toBuffer(buf); |
| 45 |
return {buf.data(), 40}; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
[[nodiscard]] bool empty() const; |
| 50 |
void clear(); |
| 51 |
|
| 52 |
[[nodiscard]] constexpr bool operator==(const Sha1Sum&) const = default; |
| 53 |
[[nodiscard]] constexpr auto operator<=>(const Sha1Sum&) const = default; |
| 54 |
|
| 55 |
friend std::ostream& operator<<(std::ostream& os, const Sha1Sum& sum) { |
| 56 |
std::array<char, 40> buf; |
| 57 |
sum.toBuffer(buf); |
| 58 |
os.write(buf.data(), 40); |
| 59 |
return os; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
void serialize(MemInputArchive& ar, unsigned ) { |
| 66 |
ar.serialize("a", a); |
| 67 |
} |
| 68 |
void serialize(MemOutputArchive& ar, unsigned ) const { |
| 69 |
ar.serialize("a", a); |
| 70 |
} |
| 71 |
void serialize(XmlInputArchive& ar, unsigned ) { |
| 72 |
std::string s; |
| 73 |
ar.load(s); |
| 74 |
if (s.empty()) { |
| 75 |
clear(); |
| 76 |
} else { |
| 77 |
parse(s); |
| 78 |
} |
| 79 |
} |
| 80 |
void serialize(XmlOutputArchive& ar, unsigned ) const { |
| 81 |
if (empty()) return; |
| 82 |
std::array<char, 40> buf; |
| 83 |
toBuffer(buf); |
| 84 |
std::string_view s(buf.data(), 40); |
| 85 |
ar.save(s); |
| 86 |
} |
| 87 |
|
| 88 |
private: |
| 89 |
std::array<uint32_t, 5> a; |
| 90 |
friend class SHA1; |
| 91 |
}; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
class SHA1 |
| 103 |
{ |
| 104 |
public: |
| 105 |
SHA1(); |
| 106 |
|
| 107 |
|
| 108 |
void update(std::span<const uint8_t> data); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
[[nodiscard]] Sha1Sum digest(); |
| 113 |
|
| 114 |
|
| 115 |
[[nodiscard]] static Sha1Sum calc(std::span<const uint8_t> data); |
| 116 |
|
| 117 |
private: |
| 118 |
void transform(std::span<const uint8_t, 64> buffer); |
| 119 |
void finalize(); |
| 120 |
|
| 121 |
private: |
| 122 |
uint64_t m_count = 0; |
| 123 |
Sha1Sum m_state; |
| 124 |
std::array<uint8_t, 64> m_buffer; |
| 125 |
bool m_finalized = false; |
| 126 |
}; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
template<> struct strCatImpl::ConcatUnit<openmsx::Sha1Sum> |
| 132 |
{ |
| 133 |
explicit ConcatUnit(openmsx::Sha1Sum sum_) |
| 134 |
: sum(sum_) {} |
| 135 |
|
| 136 |
[[nodiscard]] size_t size() const { return 40; } |
| 137 |
|
| 138 |
[[nodiscard]] char* copy(char* dst) const { |
| 139 |
sum.toBuffer(std::span<char, 40>(dst, 40)); |
| 140 |
return dst + 40; |
| 141 |
} |
| 142 |
|
| 143 |
private: |
| 144 |
openmsx::Sha1Sum sum; |
| 145 |
}; |
| 146 |
|
| 147 |
#endif |