Wrap OpenSSL's random number generator in fc clothing
This commit is contained in:
parent
2e5fdf952c
commit
3d02e3bc00
2 changed files with 33 additions and 0 deletions
8
include/fc/crypto/rand.hpp
Normal file
8
include/fc/crypto/rand.hpp
Normal 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
25
src/crypto/rand.cpp
Normal 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
|
||||
Loading…
Reference in a new issue