FixedSizeData::CreatePseudoRandom() is threadsafe
This commit is contained in:
parent
c27dddf429
commit
8b99d277a2
@ -7,6 +7,7 @@
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include "../assert/assert.h"
|
||||
#include "ThreadsafePseudoRandomPool.h"
|
||||
|
||||
namespace cpputils {
|
||||
|
||||
@ -34,7 +35,7 @@ public:
|
||||
|
||||
private:
|
||||
FixedSizeData() {}
|
||||
static CryptoPP::AutoSeededRandomPool &PseudoRandomPool();
|
||||
static ThreadsafePseudoRandomPool &PseudoRandomPool();
|
||||
|
||||
unsigned char _data[BINARY_LENGTH];
|
||||
};
|
||||
@ -48,9 +49,8 @@ template<unsigned int SIZE> constexpr unsigned int FixedSizeData<SIZE>::BINARY_L
|
||||
template<unsigned int SIZE> constexpr unsigned int FixedSizeData<SIZE>::STRING_LENGTH;
|
||||
|
||||
template<unsigned int SIZE>
|
||||
CryptoPP::AutoSeededRandomPool &FixedSizeData<SIZE>::PseudoRandomPool() {
|
||||
//TODO Make seeding use blocking=true (aka /dev/random instead of /dev/urandom) or offer a configuration option?
|
||||
static CryptoPP::AutoSeededRandomPool singleton;
|
||||
ThreadsafePseudoRandomPool &FixedSizeData<SIZE>::PseudoRandomPool() {
|
||||
static ThreadsafePseudoRandomPool singleton;
|
||||
return singleton;
|
||||
}
|
||||
|
||||
|
34
data/ThreadsafePseudoRandomPool.h
Normal file
34
data/ThreadsafePseudoRandomPool.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#ifndef MESSMER_CPPUTILS_DATA_THREADSAFEPSEUDORANDOMPOOL_H
|
||||
#define MESSMER_CPPUTILS_DATA_THREADSAFEPSEUDORANDOMPOOL_H
|
||||
|
||||
#include "../macros.h"
|
||||
|
||||
namespace cpputils {
|
||||
|
||||
//TODO Store a static CryptoPP::AutoSeededRandomPool (or multiple ones) and make constructor of
|
||||
// ThreadsafeRandomPool() be lightweight (i.e. not do seeding), so it can be called on each callsite.
|
||||
// Alternatively, use a singleton factory.
|
||||
|
||||
//TODO Test
|
||||
class ThreadsafePseudoRandomPool {
|
||||
public:
|
||||
ThreadsafePseudoRandomPool() { }
|
||||
|
||||
void GenerateBlock(byte *data, size_t size) {
|
||||
// TODO Provide multiple randomPools for parallelity instead of locking the only available one
|
||||
std::unique_lock <std::mutex> lock(_mutex);
|
||||
_pool.GenerateBlock(data, size);
|
||||
}
|
||||
|
||||
private:
|
||||
//TODO Make seeding use blocking=true (aka /dev/random instead of /dev/urandom) or offer a configuration option?
|
||||
CryptoPP::AutoSeededRandomPool _pool;
|
||||
std::mutex _mutex;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ThreadsafePseudoRandomPool);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user