0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-30 02:32:43 +01:00
construct/ircd/rand.cc

106 lines
2.7 KiB
C++
Raw Normal View History

2017-09-23 01:29:06 +02:00
/*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice is present in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
decltype(ircd::rand::device) ircd::rand::device
2017-09-23 01:29:06 +02:00
{
// on linux: uses RDRND or /dev/urandom
// on windows: TODO: verify construction source
2017-09-23 01:29:06 +02:00
};
decltype(ircd::rand::mt) ircd::rand::mt
2017-09-23 01:29:06 +02:00
{
device()
};
2017-09-23 01:29:06 +02:00
decltype(ircd::rand::dict::alnum) ircd::rand::dict::alnum
2017-09-23 01:29:06 +02:00
{
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
};
2017-09-23 01:29:06 +02:00
decltype(ircd::rand::dict::alpha) ircd::rand::dict::alpha
2017-09-23 01:29:06 +02:00
{
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
};
2017-09-23 01:29:06 +02:00
decltype(ircd::rand::dict::upper) ircd::rand::dict::upper
2017-09-23 01:29:06 +02:00
{
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
};
2017-09-23 01:29:06 +02:00
decltype(ircd::rand::dict::lower) ircd::rand::dict::lower
2017-09-23 01:29:06 +02:00
{
"abcdefghijklmnopqrstuvwxyz"
};
2017-09-23 01:29:06 +02:00
decltype(ircd::rand::dict::numeric) ircd::rand::dict::numeric
2017-09-23 01:29:06 +02:00
{
"0123456789"
};
2017-09-23 01:29:06 +02:00
std::string
ircd::rand::string(const std::string &dict,
const size_t &len)
2017-09-23 01:29:06 +02:00
{
std::string ret(len, char());
string(dict, len, reinterpret_cast<uint8_t *>(&ret.front()));
return ret;
2017-09-23 01:29:06 +02:00
}
ircd::string_view
ircd::rand::string(const std::string &dict,
const size_t &len,
char *const &buf,
const size_t &max)
2017-09-23 01:29:06 +02:00
{
if(unlikely(!max))
return { buf, max };
2017-09-23 01:29:06 +02:00
const auto size
{
std::min(len, max - 1)
};
buf[size] = '\0';
return string(dict, size, reinterpret_cast<uint8_t *>(buf));
2017-09-23 01:29:06 +02:00
}
ircd::string_view
ircd::rand::string(const std::string &dict,
const size_t &len,
uint8_t *const &buf)
2017-09-23 01:29:06 +02:00
{
std::uniform_int_distribution<size_t> dist
2017-09-23 01:29:06 +02:00
{
0, dict.size() - 1
2017-09-23 01:29:06 +02:00
};
std::generate(buf, buf + len, [&dict, &dist]
() -> uint8_t
{
return dict.at(dist(mt));
});
return string_view
2017-09-23 01:29:06 +02:00
{
reinterpret_cast<const char *>(buf), len
2017-09-23 01:29:06 +02:00
};
}