jeudi 31 mai 2018

Random bytes generation in c++11

I've written a funtion to prouduce random bytes in a uint8_t type array, unless I seed this with a random_device object it works fine but when I seed there are some compiler errors.

code:

#include <algorithm>
#include <random>
#include <functional>
#include <stdint>

void generateInitVector(uint8_t IV_buff[16])
{
using bytes_randomizer = std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>;
std::random_device rd;
bytes_randomizer bytes(rd);

std::generate(std::begin(IV_buff), std::end(IV_buff), std::ref(bytes));
}

compiler errors:

  1. error: no matching function for call to 'begin(uint8_t*&)'|

  2. error: request for member 'begin' in '__cont', which is of non-class type 'unsigned char*'|

  3. error: request for member 'begin' in '__cont', which is of non-class type 'unsigned char* const'|

  4. error: no matching function for call to 'end(uint8_t*&)'|

  5. error: request for member 'begin' in '__cont', which is of non-class type 'unsigned char*'|

  6. error: request for member 'end' in '__cont', which is of non-class type 'unsigned char* const'|

  7. error: 'class std::random_device' has no member named 'generate'|

There's catch that if I define a uint8_t type array within th function

uint8_t data[16];
std::generate(std::begin(data), std::end(data), std::ref(bytes));  //and pass this array in `generate()`.

then only error 7 remains.

I'm using codeblocks 16.01

Any solutions to this.

Thanks.




Aucun commentaire:

Enregistrer un commentaire