Ibrahim Ipek Ibrahim Ipek - 3 months ago 13
C Question

Is there a random_device equivalent in C

In C++ we can generate true random numbers (if your hardware supports) with random_device. And i am writing a implementation of One-time Pad algorithm with C. Is there a way to generate true random numbers in C? I writing with C for speed and some another reasons. If i use a PRNG, it is will be unsafe.

char *KeyGenerate(unsigned long long StringLength)
{
std::random_device TrueRandomNumberGenerator;
char *Key = (char *) malloc(StringLength + 1);

for(unsigned long long I = 0; I < StringLength; I++)
{
Key[I] = TrueRandomNumberGenerator();
}
Key[StringLength] = '\0';

return Key;
}

Answer

The C standard library is very very tiny. To get true random numbers, you will need to use OS-specific APIs.

In Linux systems, you can get a stream of cryptographically-strong random bytes from the /dev/urandom file.

Comments