Allow on_input to veto/force system controls (including reset line)
[lsnes.git] / include / library / sha256.hpp
blob62505cba47e138e70d7e2d77fb5c07157c433f21
1 #ifndef _library__sha256__hpp__included__
2 #define _library__sha256__hpp__included__
4 #include "bintohex.hpp"
5 #include <cstdint>
6 #include <cstring>
7 #include <vector>
8 #include <string>
10 /**
11 * This class implements interface to SHA-256.
13 class sha256
15 public:
16 /**
17 * Creates new SHA-256 context, initially containing empty data.
19 sha256() throw(std::bad_alloc)
21 real_init();
22 finished = false;
25 /**
26 * Destructor
28 ~sha256() throw()
30 real_destroy();
33 /**
34 * This function appends specified data to be hashed. Don't call after calling read().
36 * Parameter data: The data to write.
37 * Parameter datalen: The length of data written.
39 void write(const uint8_t* data, size_t datalen) throw()
41 if(finished)
42 return;
43 real_write(data, datalen);
46 /**
47 * Reads the hash of data written. Can be called multiple times, but after the first call, data can't be appended
48 * anymore.
50 * Parameter hashout: 32-byte buffer to store the hash to.
52 void read(uint8_t* hashout) throw()
54 if(!finished) {
55 real_finish(finalhash);
56 finished = true;
58 memcpy(hashout, finalhash, 32);
61 /**
62 * Reads 32-byte binary hash from hashout and returns 64-hex hexadecimal hash.
64 * Parameter hashout: The binary hash
65 * Returns: Hexadecimal hash
66 * Throws std::bad_alloc: Not enough memory.
68 static std::string tostring(const uint8_t* hashout) throw(std::bad_alloc)
70 return binary_to_hex(hashout, 32);
73 /**
74 * This function appends specified data to be hashed. Don't call after calling read().
76 * Parameter data: The data to write.
77 * Parameter datalen: The length of data written.
79 void write(const char* data, size_t datalen) throw()
81 write(reinterpret_cast<const uint8_t*>(data), datalen);
84 /**
85 * Similar to read(uint8_t*) but instead returns the hash as hexadecimal string.
87 * Returns: The hash in hex form.
88 * Throws std::bad_alloc: Not enough memory.
90 std::string read() throw(std::bad_alloc)
92 uint8_t x[32];
93 read(x);
94 return tostring(x);
97 /**
98 * Hashes block of data.
100 * Parameter hashout: 32-byte buffer to write the hash to.
101 * Parameter data: The data to hash.
102 * Parameter datalen: The length of data hashed.
104 static void hash(uint8_t* hashout, const uint8_t* data, size_t datalen) throw()
106 sha256 i;
107 i.write(data, datalen);
108 i.read(hashout);
112 * Hashes block of data.
114 * Parameter hashout: 32-byte buffer to write the hash to.
115 * Parameter data: The data to hash.
117 static void hash(uint8_t* hashout, const std::vector<uint8_t>& data) throw()
119 hash(hashout, &data[0], data.size());
123 * Hashes block of data.
125 * Parameter hashout: 32-byte buffer to write the hash to.
126 * Parameter data: The data to hash.
128 static void hash(uint8_t* hashout, const std::vector<char>& data) throw()
130 hash(hashout, reinterpret_cast<const uint8_t*>(&data[0]), data.size());
134 * Hashes block of data.
136 * Parameter data: The data to hash.
137 * Parameter datalen: The length of data hashed.
138 * Returns: Hexadecimal hash of the data.
139 * Throws std::bad_alloc: Not enough memory.
141 static std::string hash(const uint8_t* data, size_t datalen) throw(std::bad_alloc)
143 uint8_t hashout[32];
144 hash(hashout, data, datalen);
145 return tostring(hashout);
149 * Hashes block of data.
151 * Parameter data: The data to hash.
152 * Returns: Hexadecimal hash of the data.
153 * Throws std::bad_alloc: Not enough memory.
155 static std::string hash(const std::vector<uint8_t>& data) throw(std::bad_alloc)
157 uint8_t hashout[32];
158 hash(hashout, &data[0], data.size());
159 return tostring(hashout);
163 * Hashes block of data.
165 * Parameter data: The data to hash.
166 * Returns: Hexadecimal hash of the data.
167 * Throws std::bad_alloc: Not enough memory.
169 static std::string hash(const std::vector<char>& data) throw(std::bad_alloc)
171 uint8_t hashout[32];
172 hash(hashout, reinterpret_cast<const uint8_t*>(&data[0]), data.size());
173 return tostring(hashout);
175 private:
176 sha256(const sha256& x) throw();
177 sha256& operator=(const sha256& x) throw();
178 uint32_t state[8];
179 uint32_t datablock[16];
180 unsigned blockbytes;
181 uint64_t totalbytes;
182 bool finished;
183 uint8_t finalhash[32];
184 void real_init();
185 void real_destroy();
186 void real_finish(uint8_t* hash);
187 void real_write(const uint8_t* data, size_t datalen);
190 #endif