【问题标题】:Coding a cryptography algorithm that accepts random bits from an unspecified source编码接受来自未指定来源的随机位的加密算法
【发布时间】:2020-08-23 08:30:37
【问题描述】:

我目前正在编写一个我想在 GitHub 上分享的加密算法。该算法接受随机位作为输入。我知道有很多可能的伪随机位来源,其中最奇怪的是你可以买到它们。因为伪随机数生成器的质量和来源差异很大,我希望用户能够生成自己的伪随机位作为我算法的输入。我不知道如何以一般方式编写我的算法,因为我不知道专业密码学或统计项目中的伪随机数来自什么数据结构,以及我应该如何编写模板函数来最一般地访问这些伪随机数方式。

我的函数 my_distribution 将访问伪随机数生成器。我假设 PRNG 将返回 0 到 1 之间的双精度,或者返回我可以转换为 0 到 1 之间的双精度的数据类型。

double my_distribution([pseudorandom number generator]) {
    double random_number_from_my_distribution;
    // compute random_number_from_my_distribution using the PRNG
    return random_number_from_my_distribution;
}

我能想到的伪随机位有几个可能的来源。

  1. 伪随机位可以存储在运行时打开的文件中
  2. 伪随机位可以由(非 io)流提供。
  3. 伪随机位可以是函数的返回值。
  4. 伪随机位可以存储在其他数据结构中。

我应该如何在 my_distribution 中接受伪随机位?为什么?

【问题讨论】:

标签: c++ templates random


【解决方案1】:

您可以在 C++ 中将函数作为参数传递。将随机生成器函数作为算法的参数。这不仅允许插入任何生成器,还使单元测试变得微不足道:

#include <functional>

// The first generator
int generate_random() {
  return rand();
}

// The second generator good for tests
int generate_predictable_random() {
  return 17;
}

// Cryptography algorithm that uses the passed random generator.
void work_with_random(std::function<int()> random_generator) {
  int random_data = random_generator();
  printf("My random: %d\n", random_data);
}

int main() {
  work_with_random(generate_random);
  work_with_random(generate_predictable_random);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2018-01-05
    • 2012-05-19
    • 2015-03-03
    • 1970-01-01
    • 2021-04-11
    • 2017-09-05
    相关资源
    最近更新 更多