Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pack 128- or 256-bit numbers

Tags:

perl

pack

Is it possible to pack 128- or 256-bit numbers (AES keys/ivs 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.

like image 892
Ya. Perelman Avatar asked Dec 21 '22 15:12

Ya. Perelman


1 Answers

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
like image 79
ikegami Avatar answered Jan 06 '23 19:01

ikegami