Implemented initialization vector

This commit is contained in:
Leonetienne 2022-02-06 18:38:09 +01:00
parent 4064483cad
commit 40a6b0c4ea
4 changed files with 49 additions and 2 deletions

View File

@ -2,6 +2,8 @@
#include <iostream>
#include <GhettoCryptWrapper.h>
#include <SecureBitset.h>
#include <Util.h>
#include <InitializationVector.h>
using namespace GhettoCipher;
@ -39,7 +41,7 @@ void ExampleFiles()
int main()
{
ExampleString();
//ExampleString();
//ExampleFiles();
return 0;

View File

@ -1,5 +1,5 @@
#pragma once
#include <cstdint>
#include <cstddef>
namespace GhettoCipher
{

View File

@ -0,0 +1,29 @@
#include "InitializationVector.h"
#include <random>
#include <sstream>
using namespace GhettoCipher;
InitializationVector::InitializationVector(const Block& seed)
{
// Since an initialization vector does not have to be a secret,
// we should be fine just using a mersenne twister seeded with
// for example the key to fill it up to fit BLOCK_SIZE.
// Loosely seed mersenne twister with seed
// Here is nothing copied. Both Block::Get, and Hash<>::operator() take refs.
std::mt19937 mt = std::mt19937(std::hash<std::bitset<BLOCK_SIZE>>()(seed.Get()));
// Now generate BLOCK_SIZE urandom bits
std::stringstream ss;
for (std::size_t i = 0; i < BLOCK_SIZE; i++)
ss << (mt() % 2 ? '1' : '0');
// And create a bitset
iv = Block(ss.str());
}
InitializationVector::operator GhettoCipher::Block() const
{
return iv;
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "Config.h"
#include "Block.h"
/** Will create a sudo-random Block based on a seed
*/
class InitializationVector
{
public:
InitializationVector(const GhettoCipher::Block& seed);
operator GhettoCipher::Block() const;
private:
GhettoCipher::Block iv;
};