1 #ifndef _library__sha256__hpp__included__
2 #define _library__sha256__hpp__included__
4 #include "bintohex.hpp"
11 * This class implements interface to SHA-256.
17 * Creates new SHA-256 context, initially containing empty data.
19 sha256() throw(std::bad_alloc
)
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()
43 real_write(data
, datalen
);
47 * Reads the hash of data written. Can be called multiple times, but after the first call, data can't be appended
50 * Parameter hashout: 32-byte buffer to store the hash to.
52 void read(uint8_t* hashout
) throw()
55 real_finish(finalhash
);
58 memcpy(hashout
, finalhash
, 32);
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);
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
);
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
)
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()
107 i
.write(data
, datalen
);
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
)
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
)
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
)
172 hash(hashout
, reinterpret_cast<const uint8_t*>(&data
[0]), data
.size());
173 return tostring(hashout
);
176 sha256(const sha256
& x
) throw();
177 sha256
& operator=(const sha256
& x
) throw();
179 uint32_t datablock
[16];
183 uint8_t finalhash
[32];
186 void real_finish(uint8_t* hash
);
187 void real_write(const uint8_t* data
, size_t datalen
);