diff --git a/include/fc/crypto/rand.hpp b/include/fc/crypto/rand.hpp new file mode 100644 index 0000000..749547f --- /dev/null +++ b/include/fc/crypto/rand.hpp @@ -0,0 +1,8 @@ +#pragma once + +namespace fc { + + /* provides access to the OpenSSL random number generator */ + void rand_bytes(char* buf, int count); + void rand_pseudo_bytes(char* buf, int count); +} // namespace fc diff --git a/src/crypto/rand.cpp b/src/crypto/rand.cpp new file mode 100644 index 0000000..313ba75 --- /dev/null +++ b/src/crypto/rand.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include + + +namespace fc { + +static int init = init_openssl(); + +void rand_bytes(char* buf, int count) +{ + int result = RAND_bytes((unsigned char*)buf, count); + if (result != 1) + FC_THROW("Error calling OpenSSL's RAND_bytes(): ${code}", ("code", (uint32_t)ERR_get_error())); +} + +void rand_pseudo_bytes(char* buf, int count) +{ + int result = RAND_pseudo_bytes((unsigned char*)buf, count); + if (result == -1) + FC_THROW("Error calling OpenSSL's RAND_pseudo_bytes(): ${code}", ("code", (uint32_t)ERR_get_error())); +} + +} // namespace fc