prg.cpp
Go to the documentation of this file.
1 
11 #include <cstdint>
12 #include <cstdlib>
13 #include <iostream>
14 
15 #include "../sha/shax.hpp"
16 #include "prg.hpp"
17 
18 
19 using namespace cryptoTools;
20 
21 
22 PRG::PRG(SHAx * hash)
23 {
24  hashfunction = hash;
25  seedLen = hash->getHashLength();
26 }
27 
28 
30 {
31  std::vector<uint8_t> toHash (seed);
32  toHash.push_back( (counter>>24) % 0x100);
33  toHash.push_back( (counter>>16) % 0x100);
34  toHash.push_back( (counter>> 8) % 0x100);
35  toHash.push_back( counter % 0x100);
36  hashfunction->hash(toHash);
38  counter++;
39 }
40 
41 
42 void PRG::initialize(std::vector<uint8_t> newSeed)
43 {
44  if (newSeed.size()*8 != seedLen)
45  {
46  std::cout<<"ERROR: wrong seed length for the PRG ("
47  <<newSeed.size()*8<<" instead of "<<seedLen
48  <<")"<<std::endl;
49  exit(1);
50  }
51  seed = newSeed;
52  counter = 0;
53  index = 0;
54  updateDigest();
55 }
56 
57 
59 {
60  uint8_t out = digest[index];
61  index++;
62  if (index == hashfunction->getHashLength()/8)
63  {
64  updateDigest();
65  index = 0;
66  }
67  return out;
68 }