2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::device) ircd::rand::device
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
// on linux: uses RDRND or /dev/urandom
|
|
|
|
// on windows: TODO: verify construction source
|
2017-09-23 01:29:06 +02:00
|
|
|
};
|
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::mt) ircd::rand::mt
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
device()
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::alnum) ircd::rand::dict::alnum
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::alpha) ircd::rand::dict::alpha
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::upper) ircd::rand::dict::upper
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::lower) ircd::rand::dict::lower
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
decltype(ircd::rand::dict::numeric) ircd::rand::dict::numeric
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
"0123456789"
|
|
|
|
};
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +02:00
|
|
|
std::string
|
|
|
|
ircd::rand::string(const std::string &dict,
|
|
|
|
const size_t &len)
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +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
|
|
|
}
|
|
|
|
|
2017-09-30 01:35:55 +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
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
if(unlikely(!max))
|
|
|
|
return { buf, max };
|
2017-09-23 01:29:06 +02:00
|
|
|
|
2017-09-30 01:35:55 +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
|
|
|
}
|
|
|
|
|
2017-09-30 01:35:55 +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
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
std::uniform_int_distribution<size_t> dist
|
2017-09-23 01:29:06 +02:00
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
0, dict.size() - 1
|
2017-09-23 01:29:06 +02:00
|
|
|
};
|
|
|
|
|
2017-09-30 01:35:55 +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
|
|
|
{
|
2017-09-30 01:35:55 +02:00
|
|
|
reinterpret_cast<const char *>(buf), len
|
2017-09-23 01:29:06 +02:00
|
|
|
};
|
|
|
|
}
|