Wrap OpenSSL's random number generator in fc clothing

This commit is contained in:
Eric Frias 2014-03-27 14:13:02 -04:00
parent 2e5fdf952c
commit 3d02e3bc00
2 changed files with 33 additions and 0 deletions

View file

@ -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

25
src/crypto/rand.cpp Normal file
View file

@ -0,0 +1,25 @@
#include <openssl/rand.h>
#include <fc/crypto/openssl.hpp>
#include <fc/exception/exception.hpp>
#include <fc/fwd_impl.hpp>
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