Implemented GPrng pnrg
This commit is contained in:
@@ -26,6 +26,8 @@ namespace Leonetienne::GCrypt {
|
||||
//! Will decipher a data block via the set seed-key
|
||||
Block Decipher(const Block& data);
|
||||
|
||||
void operator=(const Feistel& other);
|
||||
|
||||
private:
|
||||
//! Will run the feistel rounds, with either regular key
|
||||
//! order or reversed key order
|
||||
|
@@ -23,8 +23,10 @@ namespace Leonetienne::GCrypt {
|
||||
//! Will digest a data block, and return it
|
||||
Block Digest(const Block& input);
|
||||
|
||||
void operator=(const GCipher& other);
|
||||
|
||||
private:
|
||||
const DIRECTION direction;
|
||||
DIRECTION direction;
|
||||
|
||||
//! The feistel instance to be used
|
||||
Feistel feistel;
|
||||
|
@@ -24,6 +24,8 @@ namespace Leonetienne::GCrypt {
|
||||
//! Will calculate a hashsum for `data`.
|
||||
static Block CalculateHashsum(const Flexblock& data);
|
||||
|
||||
void operator=(const GHash& other);
|
||||
|
||||
private:
|
||||
//! The cipher to use
|
||||
GCipher cipher;
|
||||
|
63
GCryptLib/include/GCrypt/GPrng.h
Normal file
63
GCryptLib/include/GCrypt/GPrng.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef GCRYPT_GPRNG_H
|
||||
#define GCRYPT_GPRNG_H
|
||||
|
||||
#include "GCrypt/GHash.h"
|
||||
#include "GCrypt/Util.h"
|
||||
#include <string.h>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Leonetienne::GCrypt {
|
||||
/** This class implements a pseudo random number generator, based on the GCrypt hash function
|
||||
*/
|
||||
class GPrng {
|
||||
public:
|
||||
//! Will instanciate the prng with a seed. Seed could also be a GCrypt::Key.
|
||||
GPrng(const Block& seed);
|
||||
|
||||
//! Will instanciate the GPrng with no seed. You should really seed it later.
|
||||
GPrng();
|
||||
|
||||
//! Will reset and seed the prng. Seed could also be a GCrypt::Key.
|
||||
void Seed(const Block& seed);
|
||||
|
||||
//! Will return a random bit.
|
||||
bool GetBit();
|
||||
|
||||
//! Will return a randomized instance of any primitive.
|
||||
template <typename T>
|
||||
T GetRandom() {
|
||||
static_assert(std::is_fundamental<T>::value, "Leonetienne::GCrypt::GPrng::GetRandom() may only be used with primitive types!");
|
||||
|
||||
// Pull the required amount of bits
|
||||
std::stringstream ss;
|
||||
for (std::size_t i = 0; i < sizeof(T)*8; i++) {
|
||||
ss << GetBit() ? '1' : '0';
|
||||
}
|
||||
|
||||
// Transform to bytes
|
||||
const std::string bytes = BitsToBytes(ss.str());
|
||||
|
||||
// Cram bytes into type
|
||||
T t;
|
||||
memcpy(&t, bytes.data(), sizeof(T));
|
||||
|
||||
// Return our randomized primitive
|
||||
return t;
|
||||
}
|
||||
|
||||
//! Will return a random block
|
||||
Block GetBlock();
|
||||
|
||||
private:
|
||||
//! Will generate the next block of random bits
|
||||
void AdvanceBlock();
|
||||
|
||||
GHash hasher;
|
||||
Block seed;
|
||||
std::size_t nextBit = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user