[Server] Add script functions for getting SHA256 hashes & random strings

pull/541/head
David Cernat 5 years ago
parent c15b3377da
commit 3903ac6526

@ -4,9 +4,38 @@
#include <apps/openmw-mp/Networking.hpp>
#include <extern/PicoSHA2/picosha2.h>
#include <iostream>
#include <random>
using namespace std;
const char *MiscellaneousFunctions::GenerateRandomString(unsigned int length) noexcept
{
const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
std::random_device randomDevice;
std::mt19937 generator(randomDevice());
std::uniform_int_distribution<> distribution(0, characters.size() - 1);
std::string randomString;
for (std::size_t i = 0; i < length; ++i)
{
randomString += characters[distribution(generator)];
}
return randomString.c_str();
}
const char *MiscellaneousFunctions::GetSHA256Hash(const char* inputString) noexcept
{
std::string hashString = picosha2::hash256_hex_string(std::string{inputString});
return hashString.c_str();
}
unsigned int MiscellaneousFunctions::GetLastPlayerId() noexcept
{
return Players::getLastPlayerId();

@ -4,6 +4,10 @@
#include "../Types.hpp"
#define MISCELLANEOUSAPI \
{"GenerateRandomString", MiscellaneousFunctions::GenerateRandomString},\
\
{"GetSHA256Hash", MiscellaneousFunctions::GetSHA256Hash},\
\
{"GetLastPlayerId", MiscellaneousFunctions::GetLastPlayerId},\
\
{"GetCurrentMpNum", MiscellaneousFunctions::GetCurrentMpNum},\
@ -13,6 +17,23 @@ class MiscellaneousFunctions
{
public:
/**
* \brief Generate a random string of a particular length that only contains
* letters and numbers.
*
* \param length The length of the generated string.
* \return The generated string.
*/
static const char *GenerateRandomString(unsigned int length) noexcept;
/**
* \brief Get the SHA256 hash corresponding to an input string.
*
* \param inputString The input string.
* \return The SHA256 hash.
*/
static const char *GetSHA256Hash(const char* inputString) noexcept;
/**
* \brief Get the last player ID currently connected to the server.
*

Loading…
Cancel
Save