17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__HASHING 18 #define COM_BORA_SOFTWARE__BALAU_UTIL__HASHING 20 #include <Balau/ThirdParty/HashLibrary/crc32.h> 21 #include <Balau/ThirdParty/HashLibrary/md5.h> 22 #include <Balau/ThirdParty/HashLibrary/sha1.h> 23 #include <Balau/ThirdParty/HashLibrary/sha256.h> 24 #include <Balau/ThirdParty/HashLibrary/sha3.h> 25 #include <Balau/ThirdParty/HashLibrary/keccak.h> 43 auto fileStream = getFileStream(file);
44 HashLibrary::SHA256 d;
45 return computeHash(*fileStream, d);
51 static std::string
sha256(std::istream & stream) {
52 HashLibrary::SHA256 d;
53 return computeHash(stream, d);
59 static std::string
sha256(std::string & text) {
60 HashLibrary::SHA256 d;
61 return computeHash(text, d);
71 auto fileStream = getFileStream(file);
72 HashLibrary::SHA3 d(HashLibrary::SHA3::Bits256);
73 return computeHash(*fileStream, d);
79 static std::string
sha3(std::istream & stream) {
80 HashLibrary::SHA3 d(HashLibrary::SHA3::Bits256);
81 return computeHash(stream, d);
87 static std::string
sha3(std::string & text) {
89 return computeHash(text, d);
99 auto fileStream = getFileStream(file);
101 return computeHash(*fileStream, d);
107 static std::string
sha1(std::istream & stream) {
109 return computeHash(stream, d);
115 static std::string
sha1(std::string & text) {
117 return computeHash(text, d);
127 auto fileStream = getFileStream(file);
128 HashLibrary::Keccak d(HashLibrary::Keccak::Keccak256);
129 return computeHash(*fileStream, d);
135 static std::string
keccak(std::istream & stream) {
136 HashLibrary::Keccak d(HashLibrary::Keccak::Keccak256);
137 return computeHash(stream, d);
143 static std::string
keccak(std::string & text) {
144 HashLibrary::Keccak d(HashLibrary::Keccak::Keccak256);
145 return computeHash(text, d);
155 auto fileStream = getFileStream(file);
157 return computeHash(*fileStream, d);
163 static std::string
md5(std::istream & stream) {
165 return computeHash(stream, d);
171 static std::string
md5(std::string & text) {
173 return computeHash(text, d);
183 auto fileStream = getFileStream(file);
184 HashLibrary::CRC32 d;
185 return computeHash(*fileStream, d);
191 static std::string
crc32(std::istream & stream) {
192 HashLibrary::CRC32 d;
193 return computeHash(stream, d);
199 static std::string
crc32(std::string & text) {
200 HashLibrary::CRC32 d;
201 return computeHash(text, d);
212 private:
static std::unique_ptr<boost::filesystem::ifstream> getFileStream(
const Resource::File & file) {
217 auto stream = std::make_unique<boost::filesystem::ifstream>(file.
getEntry(), std::ios::in | std::ios::binary);
226 private:
template <
typename DigestT>
static std::string computeHash(std::istream & input, DigestT & d) {
227 std::array<char, 144 * 7 * 1024> buffer {};
230 input.read(buffer.data(), buffer.size());
231 auto numBytesRead = size_t(input.gcount());
232 d.add(buffer.data(), numBytesRead);
238 private:
template <
typename DigestT>
static std::string computeHash(std::string &
string, DigestT & d) {
239 std::istringstream input(
string);
240 return computeHash(input, d);
246 #endif // COM_BORA_SOFTWARE__BALAU_UTIL__HASHING static std::string md5(const Resource::File &file)
Calculate the MD5 hash of the specified file.
Definition: Hashing.hpp:154
static std::string sha256(const Resource::File &file)
Calculate the SHA-256 hash of the specified file.
Definition: Hashing.hpp:42
static std::string crc32(std::string &text)
Calculate the CRC32 hash of the supplied string.
Definition: Hashing.hpp:199
static std::string sha1(std::istream &stream)
Calculate the SHA-1 hash of the data supplied by the stream.
Definition: Hashing.hpp:107
A file on the local file system.
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
static std::string sha1(std::string &text)
Calculate the SHA-1 hash of the supplied string.
Definition: Hashing.hpp:115
Hashing utilities.
Definition: Hashing.hpp:35
static std::string keccak(std::string &text)
Calculate the Keccak hash of the supplied string.
Definition: Hashing.hpp:143
static std::string sha256(std::istream &stream)
Calculate the SHA-256 hash of the data supplied by the stream.
Definition: Hashing.hpp:51
static std::string sha256(std::string &text)
Calculate the SHA-256 hash of the supplied string.
Definition: Hashing.hpp:59
boost::filesystem::directory_entry getEntry() const
Get the underlying directory entry for this file URI.
Definition: File.hpp:400
static std::string keccak(std::istream &stream)
Calculate the Keccak hash of the data supplied by the stream.
Definition: Hashing.hpp:135
Thrown when a resource could not be opened.
Definition: ResourceExceptions.hpp:78
static std::string crc32(const Resource::File &file)
Calculate the CRC32 hash of the specified file.
Definition: Hashing.hpp:182
A file on the local file system.
Definition: File.hpp:35
static std::string md5(std::string &text)
Calculate the MD5 hash of the supplied string.
Definition: Hashing.hpp:171
Balau exceptions for resources.
Thrown when a resource is not found.
Definition: ResourceExceptions.hpp:63
static std::string crc32(std::istream &stream)
Calculate the CRC32 hash of the data supplied by the stream.
Definition: Hashing.hpp:191
static std::string sha3(const Resource::File &file)
Calculate the SHA-3 hash of the specified file.
Definition: Hashing.hpp:70
static std::string md5(std::istream &stream)
Calculate the MD5 hash of the data supplied by the stream.
Definition: Hashing.hpp:163
static std::string sha3(std::string &text)
Calculate the SHA-3 hash of the supplied string.
Definition: Hashing.hpp:87
static std::string sha1(const Resource::File &file)
Calculate the SHA-1 hash of the specified file.
Definition: Hashing.hpp:98
static std::string sha3(std::istream &stream)
Calculate the SHA-3 hash of the data supplied by the stream.
Definition: Hashing.hpp:79
static std::string keccak(const Resource::File &file)
Calculate the Keccak hash of the specified file.
Definition: Hashing.hpp:126
bool exists() const
Returns true if an item exits in the file system for the file URI.
Definition: File.hpp:449