0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-27 07:54:05 +01:00
construct/ircd/rand.cc

75 lines
1.6 KiB
C++
Raw Normal View History

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