Hashing.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_UTIL__HASHING
18 #define COM_BORA_SOFTWARE__BALAU_UTIL__HASHING
19 
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>
27 #include <Balau/Resource/File.hpp>
28 #include <Balau/Util/Files.hpp>
29 
30 namespace Balau::Util {
31 
35 struct Hashing final {
42  static std::string sha256(const Resource::File & file) {
43  auto fileStream = getFileStream(file);
44  HashLibrary::SHA256 d;
45  return computeHash(*fileStream, d);
46  }
47 
51  static std::string sha256(std::istream & stream) {
52  HashLibrary::SHA256 d;
53  return computeHash(stream, d);
54  }
55 
59  static std::string sha256(std::string & text) {
60  HashLibrary::SHA256 d;
61  return computeHash(text, d);
62  }
63 
70  static std::string sha3(const Resource::File & file) {
71  auto fileStream = getFileStream(file);
72  HashLibrary::SHA3 d(HashLibrary::SHA3::Bits256);
73  return computeHash(*fileStream, d);
74  }
75 
79  static std::string sha3(std::istream & stream) {
80  HashLibrary::SHA3 d(HashLibrary::SHA3::Bits256);
81  return computeHash(stream, d);
82  }
83 
87  static std::string sha3(std::string & text) {
88  HashLibrary::SHA3 d;
89  return computeHash(text, d);
90  }
91 
98  static std::string sha1(const Resource::File & file) {
99  auto fileStream = getFileStream(file);
100  HashLibrary::SHA1 d;
101  return computeHash(*fileStream, d);
102  }
103 
107  static std::string sha1(std::istream & stream) {
108  HashLibrary::SHA1 d;
109  return computeHash(stream, d);
110  }
111 
115  static std::string sha1(std::string & text) {
116  HashLibrary::SHA1 d;
117  return computeHash(text, d);
118  }
119 
126  static std::string keccak(const Resource::File & file) {
127  auto fileStream = getFileStream(file);
128  HashLibrary::Keccak d(HashLibrary::Keccak::Keccak256);
129  return computeHash(*fileStream, d);
130  }
131 
135  static std::string keccak(std::istream & stream) {
136  HashLibrary::Keccak d(HashLibrary::Keccak::Keccak256);
137  return computeHash(stream, d);
138  }
139 
143  static std::string keccak(std::string & text) {
144  HashLibrary::Keccak d(HashLibrary::Keccak::Keccak256);
145  return computeHash(text, d);
146  }
147 
154  static std::string md5(const Resource::File & file) {
155  auto fileStream = getFileStream(file);
156  HashLibrary::MD5 d;
157  return computeHash(*fileStream, d);
158  }
159 
163  static std::string md5(std::istream & stream) {
164  HashLibrary::MD5 d;
165  return computeHash(stream, d);
166  }
167 
171  static std::string md5(std::string & text) {
172  HashLibrary::MD5 d;
173  return computeHash(text, d);
174  }
175 
182  static std::string crc32(const Resource::File & file) {
183  auto fileStream = getFileStream(file);
184  HashLibrary::CRC32 d;
185  return computeHash(*fileStream, d);
186  }
187 
191  static std::string crc32(std::istream & stream) {
192  HashLibrary::CRC32 d;
193  return computeHash(stream, d);
194  }
195 
199  static std::string crc32(std::string & text) {
200  HashLibrary::CRC32 d;
201  return computeHash(text, d);
202  }
203 
205 
206  Hashing() = delete;
207  Hashing(const Hashing &) = delete;
208  Hashing & operator = (const Hashing &) = delete;
209 
211 
212  private: static std::unique_ptr<boost::filesystem::ifstream> getFileStream(const Resource::File & file) {
213  if (!file.exists()) {
215  }
216 
217  auto stream = std::make_unique<boost::filesystem::ifstream>(file.getEntry(), std::ios::in | std::ios::binary);
218 
219  if (!stream.get()) {
221  }
222 
223  return stream;
224  }
225 
226  private: template <typename DigestT> static std::string computeHash(std::istream & input, DigestT & d) {
227  std::array<char, 144 * 7 * 1024> buffer {};
228 
229  while (input) {
230  input.read(buffer.data(), buffer.size());
231  auto numBytesRead = size_t(input.gcount());
232  d.add(buffer.data(), numBytesRead);
233  }
234 
235  return d.getHash();
236  }
237 
238  private: template <typename DigestT> static std::string computeHash(std::string & string, DigestT & d) {
239  std::istringstream input(string);
240  return computeHash(input, d);
241  }
242 };
243 
244 } // namespace Balau::Util
245 
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
Utilities for files.
Utility functions.
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