Random: secure_uint{32,64}

GitOrigin-RevId: 5cc15552bc0d103cbe93935d3c75256845e3a39b
This commit is contained in:
Arseny Smirnov 2019-07-04 12:55:17 +02:00
parent d4cb9d2d52
commit b938ca635c
2 changed files with 14 additions and 0 deletions

View File

@ -81,6 +81,18 @@ int64 Random::secure_int64() {
return res; return res;
} }
uint32 Random::secure_uint32() {
uint32 res = 0;
secure_bytes(reinterpret_cast<unsigned char *>(&res), sizeof(uint32));
return res;
}
uint64 Random::secure_uint64() {
uint64 res = 0;
secure_bytes(reinterpret_cast<unsigned char *>(&res), sizeof(uint64));
return res;
}
void Random::add_seed(Slice bytes, double entropy) { void Random::add_seed(Slice bytes, double entropy) {
RAND_add(bytes.data(), static_cast<int>(bytes.size()), entropy); RAND_add(bytes.data(), static_cast<int>(bytes.size()), entropy);
random_seed_generation++; random_seed_generation++;

View File

@ -18,6 +18,8 @@ class Random {
static void secure_bytes(unsigned char *ptr, size_t size); static void secure_bytes(unsigned char *ptr, size_t size);
static int32 secure_int32(); static int32 secure_int32();
static int64 secure_int64(); static int64 secure_int64();
static uint32 secure_uint32();
static uint64 secure_uint64();
// works only for current thread // works only for current thread
static void add_seed(Slice bytes, double entropy = 0); static void add_seed(Slice bytes, double entropy = 0);