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
|
|
|
ircd::string_view
|
2020-10-07 00:46:36 +02:00
|
|
|
ircd::rand::string(const mutable_buffer &out,
|
|
|
|
const std::string &dict)
|
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
|
|
|
};
|
|
|
|
|
2018-02-15 21:53:00 +01:00
|
|
|
std::generate(data(out), data(out) + size(out), [&dict, &dist]
|
2017-09-30 01:35:55 +02:00
|
|
|
{
|
2020-10-07 00:46:36 +02:00
|
|
|
return char(dict.at(dist(mt)));
|
2017-09-30 01:35:55 +02:00
|
|
|
});
|
|
|
|
|
2018-02-15 21:53:00 +01:00
|
|
|
return out;
|
2017-09-23 01:29:06 +02:00
|
|
|
}
|