What is a solution to generate random long long
with cryptographic strength С++? (boost is allowed)

- 64,318
- 19
- 100
- 158

- 69
- 4
- 10
-
2Why not Boost? -it helps us to understand what your limits are – mmmmmm Dec 04 '12 at 17:55
-
@Mark i need a solution without boost because it must work under linux without pre-installed boost libraries – Roman Dec 04 '12 at 17:57
-
1On linux you can read `sizeof(long long)` bytes from `/dev/random`. – zch Dec 04 '12 at 17:58
-
1You can just copy the boost headers for random – mmmmmm Dec 04 '12 at 18:00
-
2You don't need `boost` on the deployment platform, only on the build machine. – Paul R Dec 04 '12 at 18:01
-
Thanks, i will create new thread – Roman Dec 04 '12 at 18:35
-
@Roman: Why? They are effectively the same quesiton, just combine them. (The answers already do.) – GManNickG Dec 04 '12 at 18:47
-
@GManNickG as i know it is not recommended to change question. I my case from "no boost" to "using boost" – Roman Dec 04 '12 at 18:52
-
1@Roman: That is a small change in the question that would fit the answers, it's okay. What's not okay is radically changing the question so nothing else (comments/answers) makes sense. – GManNickG Dec 04 '12 at 18:56
-
@GManNickG, ok, thanks, i've fixed thread question – Roman Dec 04 '12 at 19:02
2 Answers
The <random>
header provides portable access to random number facilities including, potentially, a cryptographic pRNG.
#include <random> // random_device, uniform_int_distribution
#include <algorithm> // generate_n
#include <iterator> // ostream_iterator
#include <iostream> // cout
#include <functional> // bind, ref
int main() {
std::random_device r;
std::uniform_int_distribution<long long> dist;
std::generate_n(std::ostream_iterator<long long>(std::cout, "\n"), 10,
std::bind(dist,std::ref(r)));
}
std::random_device
may not be a cryptographic pRNG on all implementations, so you'll have to check your implementation documentation. In particular VC++ before 2012 does not provide a non-deterministic implementation. VC++ 2012 and later implements this using Windows cryptography services.
Implementations on other operating systems such as Linux or Mac OS X can commonly use "/dev/urandom" or "/dev/random" or any other random device exposed via the filesystem. E.g. libc++ uses "/dev/urandom" by default, which on OS X uses the Yarrow algorithm.
I know you ruled out boost, but boost::random_device
has an implementation for Windows that uses that platform's cryptography services.

- 86,085
- 15
- 179
- 244
-
1+1 for using std but also pointing to boost again (I don't understand why people don't have boost setup by default) – gvd Dec 04 '12 at 18:11
-
@gvd: Many companies do not like putting open source code onto the build machine. – Mooing Duck Dec 04 '12 at 19:16
Under linux you can read from /dev/random
or /dev/urandom
They both provide cryptographic entropy.
Difference between them being: /dev/random
blocks if entropy is exhausted, therefore it could be slower than /dev/urandom
but is "stronger"
So, using streams it will look like this
long long n;
std::ifstream rstream ("/dev/random");
rstream >> n;

- 79,187
- 7
- 161
- 281