如何在Perl 6中实现Python的os.urandom方法?

时间:2019-01-02 13:09:58

标签: perl6

我正在将用Python编写的模块转换为Perl6。在模块中,有一个名为 create_key 的方法,该方法使用os.urandom进行加密:

def create_key(size):
    return binascii.hexlify(os.urandom(size))[:16]

文档将os.urandom描述为:

  

返回一个适合加密用途的大小为随机字节的字符串。

在Perl 6中,有一个名为Buf的类,但是没有用于它的随机方法。那么如何使用Perl 6实现os.urandom(size)

3 个答案:

答案 0 :(得分:9)

使用方法或子roll不会为您提供适合加密用途的随机字节。他们只是使用perl6提供的内置伪随机数生成器,对于moarvm上的rakudo来说,这是一个梅森扭曲器。

您将想要的是一个类似Crypt::Random的库,它类似于arc4random,并被描述为在unices上使用/dev/urandom在Windows上使用CryptGenRandomYou can find it on github或直接通过zef install Crypt::Random安装。

使用该模块中的crypt_random_buf子项将为您提供所需大小的buf,然后您可以将其转换为十六进制数字字符串,就像在其他答案中一样,使用.list.fmt("%x","")

答案 1 :(得分:9)

您总是可以只使用Python的urandom

sub py-urandom( UInt $size ){
  use Inline::Python;

  state $py = Inline::Python.new; # reuse the same instance
  once $py.import('os');          # load the Python os library only once

  $py.call('os','urandom', $size);
}

say py-urandom(10)».fmt('%02X').join;  # 1473A7D5455F15D3726B

要使以上各项正常工作,必须安装python-dev操作系统软件包。然后使用Inline::Python安装zef


您也可以使用binascii.hexlify

sub create-key ( UInt $size ) {
  use Inline::Python;

  state $py = Inline::Python.new;
  once $py.import('os');
  once $py.import('binascii');

  $py.call('binascii','hexlify', $py.call('os','urandom',$size)).decode('ascii');
}

我敢肯定有一种更好的方法可以完成上述操作,但这是我第一次使用Inline::Python。 (这很明显,因为我必须安装python-dev才能回答此问题)


从长远来看,另一种更好的方法是仅调用getrandomgetentropyCryptGenRandom,具体取决于它是否在Linux,OpenBSD或Windows上运行。基本上复制os.urandom的实现。

下面是一个快速编写的示例。

sub urandom ( UInt $size ){
  use NativeCall;

  my constant is-win = $*DISTRO.is-win;
  my constant is-openbsd = $*DISTRO.name eq 'openbsd';

  if is-win {
    fail "urandom doesn't handle Windows yet";
    # It is more involved on Windows, and I don't use Windows

  } elsif is-openbsd {
    # note that this is untested as I don't use OpenBSD
    if $size > 256 {
      fail "urandom doesn't handle more than 256 on OpenBSD"
      # note that this could be changed to load it in 256 byte chunks
    }

    sub getentropy( Buf \buf, size_t \buflen --> int32 ) is native {}

    my Buf $buf .= allocate($size);
    my $result = getentropy( $buf, $size );

    fail if $result !== 0;
    $buf

  } else { # presumably Linux or other UNIX-like

    sub getrandom (Buf \buf, size_t \buflen, uint32 \flags --> ssize_t) is native {}

    my Buf $buf .= allocate($size);
    my $total = getrandom( $buf, $size, 0 );

    fail unless $total == $size; # could be changed to call it for the rest
    $buf;
  }
}
say urandom(10)».fmt('%02X').join; # 0EF9EDB3EBC724C0E9CE

如果您使用的是/dev/urandom的系统,则可以直接从中读取。

sub urandom ( UInt $size ){
  my $urandom will leave {.close}
    = '/dev/urandom'.IO.open(:bin,:ro);
  $urandom.read( $size )
}

say urandom(10)».fmt('%02X').join; # 01B6C41AD0A77732C328

最好的方法是使用已经完成上述操作的模块,例如Crypt::Random
它实现了Windows所没有的代码,但是在* NIX系统上使用了/dev/urandom文件。

# alias &Crypt::Random::crypt_random_buf as &urandom
my &urandom = do {
  use Crypt::Random;
  &crypt_random_buf
}

say urandom(10)».fmt('%02X').join; # 841720513678B1811E2D

答案 2 :(得分:6)

sub urandom(Int:D \size) { Buf.new: (^256).roll(size) }
say urandom(16);  # Buf:0x<98 43 10 A7 5A FD 62 4B AB 1E 42 6D 24 70 E6 89>

或者,作为字符串:

say urandom(16).list.fmt("%x","");  # bfa1c6fef9784ba31b17cdb135ce6622

或将其放在urandom子中:

sub urandom(Int:D \size) { Buf.new((^256).roll(size)).list.fmt("%x","") }
say urandom(16);  # bfa1c6fef9784ba31b17cdb135ce6622