trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
Utilities.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <trantor/exports.h>
18#include <string>
19
20namespace trantor
21{
26namespace utils
27{
36TRANTOR_EXPORT std::string toUtf8(const std::wstring &wstr);
45TRANTOR_EXPORT std::wstring fromUtf8(const std::string &str);
46
63TRANTOR_EXPORT std::string fromWidePath(const std::wstring &strPath);
83TRANTOR_EXPORT std::wstring toWidePath(const std::string &strUtf8Path);
84
85// Helpers for multi-platform development
86// OS dependent
87#if defined(_WIN32) && !defined(__MINGW32__)
103inline std::wstring toNativePath(const std::string &strPath)
104{
105 return toWidePath(strPath);
106}
118inline const std::wstring &toNativePath(const std::wstring &strPath)
119{
120 return strPath;
121}
122#else // __WIN32
134inline const std::string &toNativePath(const std::string &strPath)
135{
136 return strPath;
137}
138
153inline std::string toNativePath(const std::wstring &strPath)
154{
155 return fromWidePath(strPath);
156}
157#endif // _WIN32
161inline const std::string &fromNativePath(const std::string &strPath)
162{
163 return strPath;
164}
165
168// Convert on all systems
169inline std::string fromNativePath(const std::wstring &strPath)
170{
171 return fromWidePath(strPath);
172}
173
178TRANTOR_EXPORT std::string tlsBackend();
179
181{
182 unsigned char bytes[16];
183};
184
186{
187 unsigned char bytes[20];
188};
189
191{
192 unsigned char bytes[32];
193};
194
195// provide sane hash functions so users don't have to provide their own
196
201TRANTOR_EXPORT Hash128 md5(const void *data, size_t len);
202inline Hash128 md5(const std::string &str)
203{
204 return md5(str.data(), str.size());
205}
206
210TRANTOR_EXPORT Hash160 sha1(const void *data, size_t len);
211inline Hash160 sha1(const std::string &str)
212{
213 return sha1(str.data(), str.size());
214}
215
219TRANTOR_EXPORT Hash256 sha256(const void *data, size_t len);
220inline Hash256 sha256(const std::string &str)
221{
222 return sha256(str.data(), str.size());
223}
224
228TRANTOR_EXPORT Hash256 sha3(const void *data, size_t len);
229inline Hash256 sha3(const std::string &str)
230{
231 return sha3(str.data(), str.size());
232}
233
240TRANTOR_EXPORT Hash256 blake2b(const void *data, size_t len);
241inline Hash256 blake2b(const std::string &str)
242{
243 return blake2b(str.data(), str.size());
244}
245
252TRANTOR_EXPORT std::string toHexString(const void *data, size_t len);
253inline std::string toHexString(const Hash128 &hash)
254{
255 return toHexString(hash.bytes, sizeof(hash.bytes));
256}
257
258inline std::string toHexString(const Hash160 &hash)
259{
260 return toHexString(hash.bytes, sizeof(hash.bytes));
261}
262
263inline std::string toHexString(const Hash256 &hash)
264{
265 return toHexString(hash.bytes, sizeof(hash.bytes));
266}
267
281TRANTOR_EXPORT bool secureRandomBytes(void *ptr, size_t size);
282
283} // namespace utils
284
285} // namespace trantor
Trantor helper functions.
Definition Utilities.h:27
TRANTOR_EXPORT bool secureRandomBytes(void *ptr, size_t size)
Generates cryptographically secure random bytes.
TRANTOR_EXPORT Hash160 sha1(const void *data, size_t len)
Compute the SHA1 hash of the given data.
TRANTOR_EXPORT std::string tlsBackend()
Returns the TLS backend used by trantor. Could be "None", "OpenSSL" or "Botan".
TRANTOR_EXPORT std::string fromWidePath(const std::wstring &strPath)
TRANTOR_EXPORT Hash256 blake2b(const void *data, size_t len)
Compute the BLAKE2b hash of the given data.
TRANTOR_EXPORT std::wstring toWidePath(const std::string &strUtf8Path)
TRANTOR_EXPORT Hash128 md5(const void *data, size_t len)
Compute the MD5 hash of the given data.
TRANTOR_EXPORT std::wstring fromUtf8(const std::string &str)
Convert a UTF-8 string to a wide string.
const std::string & toNativePath(const std::string &strPath)
Definition Utilities.h:134
TRANTOR_EXPORT Hash256 sha256(const void *data, size_t len)
Compute the SHA256 hash of the given data.
TRANTOR_EXPORT std::string toHexString(const void *data, size_t len)
hex encode the given data
const std::string & fromNativePath(const std::string &strPath)
Definition Utilities.h:161
TRANTOR_EXPORT std::string toUtf8(const std::wstring &wstr)
Convert a wide string to a UTF-8.
TRANTOR_EXPORT Hash256 sha3(const void *data, size_t len)
Compute the SHA3 hash of the given data.
Definition EventLoop.h:34
Definition Utilities.h:181
Definition Utilities.h:186
Definition Utilities.h:191