55 lines
1.2 KiB
C++
55 lines
1.2 KiB
C++
#include "common/Crypto.h"
|
|
|
|
#include <drogon/utils/Utilities.h>
|
|
#include <iomanip>
|
|
#include <random>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
namespace visione::crypto
|
|
{
|
|
namespace
|
|
{
|
|
std::string toHex(const unsigned char *data, size_t size)
|
|
{
|
|
std::ostringstream stream;
|
|
stream << std::hex << std::uppercase << std::setfill('0');
|
|
for (size_t i = 0; i < size; ++i)
|
|
{
|
|
stream << std::setw(2) << static_cast<int>(data[i]);
|
|
}
|
|
return stream.str();
|
|
}
|
|
} // namespace
|
|
|
|
std::string sha256(const std::string &value)
|
|
{
|
|
return drogon::utils::getSha256(value);
|
|
}
|
|
|
|
std::string randomNumericCode(int digits)
|
|
{
|
|
std::random_device device;
|
|
std::mt19937 generator(device());
|
|
std::uniform_int_distribution<int> distribution(0, 9);
|
|
|
|
std::string code;
|
|
code.reserve(static_cast<size_t>(digits));
|
|
for (int i = 0; i < digits; ++i)
|
|
{
|
|
code.push_back(static_cast<char>('0' + distribution(generator)));
|
|
}
|
|
return code;
|
|
}
|
|
|
|
std::string randomToken()
|
|
{
|
|
return drogon::utils::getUuid() + drogon::utils::getUuid();
|
|
}
|
|
|
|
std::string hashSecret(const std::string &secret)
|
|
{
|
|
return sha256("visione:" + secret);
|
|
}
|
|
} // namespace visione::crypto
|