diff --git a/src/base/tools/Cvt.cpp b/src/base/tools/Cvt.cpp index be46bc0c..18d5e627 100644 --- a/src/base/tools/Cvt.cpp +++ b/src/base/tools/Cvt.cpp @@ -1,7 +1,7 @@ /* XMRig * Copyright (c) 2013-2020 Frank Denis - * Copyright (c) 2018-2020 SChernykh - * Copyright (c) 2016-2020 XMRig , + * Copyright (c) 2018-2021 SChernykh + * Copyright (c) 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,11 +34,6 @@ namespace xmrig { -#ifndef XMRIG_SODIUM -static std::random_device randomDevice; -static std::mt19937 randomEngine(randomDevice()); - - static char *cvt_bin2hex(char *const hex, const size_t hex_maxlen, const unsigned char *const bin, const size_t bin_len) { size_t i = 0U; @@ -69,6 +64,11 @@ static char *cvt_bin2hex(char *const hex, const size_t hex_maxlen, const unsigne } +#ifndef XMRIG_SODIUM +static std::random_device randomDevice; +static std::mt19937 randomEngine(randomDevice()); + + static int cvt_hex2bin(unsigned char *const bin, const size_t bin_maxlen, const char *const hex, const size_t hex_len, const char *const ignore, size_t *const bin_len, const char **const hex_end) { size_t bin_pos = 0U; @@ -275,3 +275,17 @@ xmrig::String xmrig::Cvt::toHex(const uint8_t *in, size_t size) return buf; } + + +void xmrig::Cvt::randomBytes(void *buf, size_t size) +{ +# ifndef XMRIG_SODIUM + std::uniform_int_distribution<> dis(0, 255); + + for (size_t i = 0; i < size; ++i) { + static_cast(buf)[i] = static_cast(dis(randomEngine)); + } +# else + randombytes_buf(buf, size); +# endif +} diff --git a/src/base/tools/Cvt.h b/src/base/tools/Cvt.h index 8b67b146..e7507aaf 100644 --- a/src/base/tools/Cvt.h +++ b/src/base/tools/Cvt.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2020 SChernykh - * Copyright (c) 2016-2020 XMRig , + * Copyright (c) 2018-2021 SChernykh + * Copyright (c) 2016-2021 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -52,6 +52,7 @@ public: static rapidjson::Value toHex(const std::string &data, rapidjson::Document &doc); static rapidjson::Value toHex(const uint8_t *in, size_t size, rapidjson::Document &doc); static String toHex(const uint8_t *in, size_t size); + static void randomBytes(void *buf, size_t size); };