【问题标题】:Seeding Math::Random::ISAAC from /dev/urandom : How to read/pass 128 bits?从 /dev/urandom 播种 Math::Random::ISAAC:如何读取/传递 128 位?
【发布时间】:2013-01-29 05:43:34
【问题描述】:

在 Perl 中有很多方法可以创建安全的随机数。大多数这些方法都涉及一个或多个 Perl 模块。其中许多 Perl 模块需要更多的 Perl 模块作为依赖项。

由于我想保持轻量级并减少所需的依赖,我决定使用Math::Random::ISAAC,并自己从/dev/urandom 播种。

那么,在不使用其他模块的情况下,在 Perl 中读取来自 /dev/urandom 的 128 位(我想这是一个很好的数量)数据的推荐方法是什么,然后将其传递给 ISAAC ?

我不知道正确的阅读和格式化方式是什么,这里是the sub I will be passing the seed to:的副本

sub new {
  my ($class, @seed) = @_;

  my $seedsize = scalar(@seed);

  my @mm;
  $#mm = $#seed = 255; # predeclare arrays with 256 slots

  # Zero-fill our seed data
  for ($seedsize .. 255) {
    $seed[$_] = 0;
  }

  my $self = {
    randrsl   => \@seed,
    randcnt   => 0,
    randmem   => \@mm,

    randa     => 0,
    randb     => 0,
    randc     => 0,
  };

  bless($self, $class);

  $self->_randinit();

  return $self;
}

我不是数组、标量、字节字符串等方面的专家;所以我真的不知道如何将urandom 数据适当地格式化为@seed。我确实认为 128 位有很多随机性,但我不知道如何确保所有这些熵都变成ISAAC,并防止它被截断为 64 或 32 位。

【问题讨论】:

    标签: perl file unix random binary


    【解决方案1】:

    通过查看使用种子的 ISAAC 代码,它似乎需要 32 位值。所以:

    use autodie;
    open(my $fh,'<','/dev/urandom');
    my $buffer;
    read($fh, $buffer, 16) == 16 or die "not enough read";
    my @seed = unpack 'L4', $buffer;
    my $isaac = Math::Random::ISAAC->new(@seed);
    

    【讨论】:

      猜你喜欢
      • 2010-11-18
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-11-21
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多