trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::utils Namespace Reference

Trantor helper functions. More...

Classes

struct  Hash128
struct  Hash160
struct  Hash256

Functions

TRANTOR_EXPORT std::string toUtf8 (const std::wstring &wstr)
 Convert a wide string to a UTF-8.
TRANTOR_EXPORT std::wstring fromUtf8 (const std::string &str)
 Convert a UTF-8 string to a wide string.
TRANTOR_EXPORT std::string fromWidePath (const std::wstring &strPath)
TRANTOR_EXPORT std::wstring toWidePath (const std::string &strUtf8Path)
const std::string & toNativePath (const std::string &strPath)
std::string toNativePath (const std::wstring &strPath)
const std::string & fromNativePath (const std::string &strPath)
std::string fromNativePath (const std::wstring &strPath)
TRANTOR_EXPORT std::string tlsBackend ()
 Returns the TLS backend used by trantor. Could be "None", "OpenSSL" or "Botan".
TRANTOR_EXPORT Hash128 md5 (const void *data, size_t len)
 Compute the MD5 hash of the given data.
Hash128 md5 (const std::string &str)
TRANTOR_EXPORT Hash160 sha1 (const void *data, size_t len)
 Compute the SHA1 hash of the given data.
Hash160 sha1 (const std::string &str)
TRANTOR_EXPORT Hash256 sha256 (const void *data, size_t len)
 Compute the SHA256 hash of the given data.
Hash256 sha256 (const std::string &str)
TRANTOR_EXPORT Hash256 sha3 (const void *data, size_t len)
 Compute the SHA3 hash of the given data.
Hash256 sha3 (const std::string &str)
TRANTOR_EXPORT Hash256 blake2b (const void *data, size_t len)
 Compute the BLAKE2b hash of the given data.
Hash256 blake2b (const std::string &str)
TRANTOR_EXPORT std::string toHexString (const void *data, size_t len)
 hex encode the given data
std::string toHexString (const Hash128 &hash)
std::string toHexString (const Hash160 &hash)
std::string toHexString (const Hash256 &hash)
TRANTOR_EXPORT bool secureRandomBytes (void *ptr, size_t size)
 Generates cryptographically secure random bytes.

Detailed Description

Trantor helper functions.

Function Documentation

◆ blake2b()

TRANTOR_EXPORT Hash256 trantor::utils::blake2b ( const void * data,
size_t len )

Compute the BLAKE2b hash of the given data.

Note
When in doubt, use SHA3 or BLAKE2b. Both are safe and SHA3 is faster if you are using OpenSSL and it has SHA3 in hardware mode. Otherwise BLAKE2b is faster in software.

◆ fromNativePath() [1/2]

const std::string & trantor::utils::fromNativePath ( const std::string & strPath)
inline
Note
NoOP on all OSes

◆ fromNativePath() [2/2]

std::string trantor::utils::fromNativePath ( const std::wstring & strPath)
inline
Note
fromWidePath() on all OSes

◆ fromUtf8()

TRANTOR_EXPORT std::wstring trantor::utils::fromUtf8 ( const std::string & str)

Convert a UTF-8 string to a wide string.

UCS2 on Windows, UTF-32 on Linux & Mac

Parameters
strString to convert
Returns
converted string.

◆ fromWidePath()

TRANTOR_EXPORT std::string trantor::utils::fromWidePath ( const std::wstring & strPath)

Convert a wide string path with arbitrary directory separators to a UTF-8 portable path for use with trantor.

This is a helper, mainly for Windows and multi-platform projects.

Note
On Windows, backslash directory separators are converted to slash to keep portable paths.
Remarks
On other OSes, backslashes are not converted to slash, since they are valid characters for directory/file names.
Parameters
strPathWide string path.
Returns
std::string UTF-8 path, with slash directory separator.

◆ md5()

TRANTOR_EXPORT Hash128 trantor::utils::md5 ( const void * data,
size_t len )

Compute the MD5 hash of the given data.

Note
don't use MD5 for new applications. It's here only for compatibility

◆ secureRandomBytes()

TRANTOR_EXPORT bool trantor::utils::secureRandomBytes ( void * ptr,
size_t size )

Generates cryptographically secure random bytes.

Parameters
ptrPointer to the buffer to fill
sizeSize of the buffer
Returns
true if successful, false otherwise
Note
This function really shouldn't fail, but it's possible that
  • OpenSSL can't access /dev/urandom
  • Compiled with glibc that supports getentropy() but the kernel doesn't

When using Botan or on *BSD/macOS, this function will always succeed.

◆ toHexString()

TRANTOR_EXPORT std::string trantor::utils::toHexString ( const void * data,
size_t len )

hex encode the given data

Note
When in doubt, use SHA3 or BLAKE2b. Both are safe and SHA3 is faster if you are using OpenSSL and it has SHA3 in hardware mode. Otherwise BLAKE2b is faster in software.

◆ toNativePath() [1/2]

const std::string & trantor::utils::toNativePath ( const std::string & strPath)
inline

Convert an UTF-8 path to a native path.

This is a helper for non-Windows OSes for multi-platform projects.

Does nothing.

Parameters
strPathUTF-8 path.
Returns
strPath.

◆ toNativePath() [2/2]

std::string trantor::utils::toNativePath ( const std::wstring & strPath)
inline

Convert an wide string path to a UTF-8 path.

This is a helper, mainly for Windows and multi-platform projects.

Note
On Windows, backslash directory separators are converted to slash to keep portable paths.
Warning
On other OSes, backslashes are not converted to slash, since they are valid characters for directory/file names.
Parameters
strPathwide string path.
Returns
Generic path, with slash directory separators

◆ toUtf8()

TRANTOR_EXPORT std::string trantor::utils::toUtf8 ( const std::wstring & wstr)

Convert a wide string to a UTF-8.

UCS2 on Windows, UTF-32 on Linux & Mac

Parameters
wstrString to convert
Returns
converted string.

◆ toWidePath()

TRANTOR_EXPORT std::wstring trantor::utils::toWidePath ( const std::string & strUtf8Path)

Convert a UTF-8 path with arbitrary directory separator to a wide string path.

This is a helper, mainly for Windows and multi-platform projects.

Note
On Windows, slash directory separators are converted to backslash. Although it accepts both slash and backslash as directory separator in its API, it is better to stick to its standard.
Remarks
On other OSes, slashes are not converted to backslashes, since they are not interpreted as directory separators and are valid characters for directory/file names.
Parameters
strUtf8PathAscii path considered as being UTF-8.
Returns
std::wstring path with, on windows, standard backslash directory separator to stick to its standard.