How do I pack 128- or 256-bit numbers
Is it possible to pack 128- or 256-bit numbers (AES keys/iv开发者_运维技巧s generated with Crypt::Random::makerandom) using the perl built-in pack? If yes, what should my template X in
pack('X', ($256_bit_number));
be?
Thank you.
Perl can't hold numbers that large, so it can't possibly pack them.
So let's look at what makerandom
actually returns.
$ perl -MData::Dumper -MCrypt::Random=makerandom \
-e'print(Dumper(makerandom(Size => 256, Strength => 1)));'
$VAR1 = bless( do{\(my $o = 148076988)}, 'Math::Pari' );
Ah, a Math::Pari object. Looking at the docs, there doesn't appear to be a straightforward means of pack those. But it looks like we don't have to. Crypt::Random provides makerandom_octet
that returns the "packed" number.
$ perl -MCrypt::Random=makerandom_octet \
-e'print(unpack("H*", makerandom_octet(Size => 256, Strength => 1)));'
1432698ef28c63d9cb0bba474c1644b4a6f9736616bd070102a612785332e94bb4
精彩评论