【问题标题】:Random bytes generation in c++11c++11中的随机字节生成
【发布时间】:2018-11-10 10:40:00
【问题描述】:

我已经编写了一个函数来在 uint8_t 类型数组中引入随机字节,除非我使用 random_device 对象作为种子它工作正常,但是当我种子时会出现一些编译器错误。

代码:

#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));
}

编译器错误:

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'|

如果我在函数中定义 uint8_t 类型数组,就会发现

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

然后只剩下错误 7。有什么解决办法吗?

【问题讨论】:

  • What is array decaying?的可能重复
  • 使用std::array。
  • @SombreroChicken 你能详细说明一下吗?
  • @UnholySheep 你能演示我如何实现你重定向我的解决方案吗?
  • 无论如何你都不能在这里使用uint8_t。仅short、int、long、long long 或它们的未签名对应项,视情况而定,被允许作为&lt;random&gt; 中的模板参数。甚至 int64_t 之类的也可能是 UB,尽管这纯粹是理论上的问题。

标签: c++ arrays c++11 random function-parameter


【解决方案1】:

这里的问题是,即使你的函数看起来像

void generateInitVector(uint8_t IV_buff[16])

由于数组衰减成指针,它的真正含义是

void generateInitVector(uint8_t * IV_buff)

所以,因为你有一个指针,你不能使用std::begin 和std::end。您需要做的是通过引用传递数组以将其保留为数组。看起来像

void generateInitVector(uint8_t (&IV_buff)[16])

现在您可以使用std::begin 和std::end 并维护数组的大小信息。您甚至可以使用类似

的模板使其尺寸通用
template<std::size_t N>
void generateInitVector(uint8_t (&IV_buff)[N])
{
    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));
}

你也有问题

bytes_randomizer bytes(rd);

std::independent_bits_engine 需要第一个模板参数类型的 PRNG。您使用了std::default_random_engine,它与std::random_device 不同。您需要更改其中一个以匹配另一个才能编译。例如:

template<std::size_t N>
void generateInitVector(uint8_t (&IV_buff)[N])
{
    using bytes_randomizer = std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>;
    std::default_random_engine rd;
    bytes_randomizer bytes(rd);

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

int main()
{
    uint8_t data[16];
    generateInitVector(data);
}

Compiles just fine

【讨论】:

    猜你喜欢
    • 2014-10-07
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    相关资源
    最近更新 更多