From 3d02e3bc00831363fcca9f0a19a3daaf05e878ba Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Thu, 27 Mar 2014 14:13:02 -0400 Subject: [PATCH] Wrap OpenSSL's random number generator in fc clothing --- include/fc/crypto/rand.hpp | 8 ++++++++ src/crypto/rand.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 include/fc/crypto/rand.hpp create mode 100644 src/crypto/rand.cpp 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