How to implement sha-512 with Crypt::PBKDF2 in Perl?

917 views Asked by At

So I'm looking to use SHA-512 with PBKDF2 to implement Bitcoin BIP-039. I have managed to work out that SHA-512 falls under SHA2 but when I specify that as the hashing function, even with 64 byte output, it still reports as using SHA-256. Am I missing something? I tried adding +512 to the hash_class but that didn't work.

#!/usr/bin/perl
#
use Crypt::PBKDF2;
my $sentence="Hellothere";
my $salt="mnemonic";
my $pbkdf2 = Crypt::PBKDF2->new(
  hash_class => 'HMACSHA2', # 
  iterations => 2048,      # 
  output_len => 64,        # 
);
my $hash = $pbkdf2->generate($sentence,$salt);
print "$hash\n";

Gives

{X-PBKDF2}HMACSHA2+256:AAAIAA:bW5lbW9uaWM=:NLw67sZbhQYsPhrEYm9e5ruslS6/ivK1vDfICtCN07rb7RuBkQxAoZIyTG7sTmsob30JwoP64Fvzpjx6Cqc+KQ==
2

There are 2 answers

0
Richard Thomas On

Passing this to the new() call works.

  hash_args=>{sha_size => 512}
{X-PBKDF2}HMACSHA2+512:AAAIAA:bW5lbW9uaWM=:WG00S/OSlPeYJ/HWeIPkVdQHpSXnpzG0Ixb+j70pbgDgdCAemPBLbjYBbcUtnfSS2dzMJng73eAlGSSnDi+dDQ==
0
karel-m On
use Crypt::KeyDerivation 'pbkdf2';
my $data = pbkdf2("Hellothere", "mnemonic", 2048, 'SHA512', 64);
print unpack("H*", $data), "\n";