Perl 中 Crypt::PBKDF2 中 HMACSHA2 散列的大小是多少?

What's the size of a HMACSHA2 hash in Crypt::PBKDF2 in Perl?

我想将密码哈希存储在数据库中。将使用

生成哈希
my $PBKDF2 = Crypt::PBKDF2->new(
    hash_class => 'HMACSHA2',
    hash_args => {
        sha_size => 512,
    },
    iterations => 10000,
    salt_len => 10,
);

在 Crypt::PBKDF2 的 Pod 中,我发现:

The default size (in bytes, not bits) of the output hash. If a value isn't provided, the output size depends on the hash_class / hasher selected, and will equal the output size of the backend hash (e.g. 20 bytes for HMACSHA1).

但默认输出大小实际上是多少?

32 字节

您可以在 source code of Crypt::PBKDF2::Hash::HMACSHA2 中找到此信息。定义默认大小的代码是:

has 'sha_size' => (
  is => 'ro',
  isa => Type::Tiny->new(
    name => 'SHASize',
    parent => Enum[qw( 224 256 384 512 )],
    display_name => 'valid number of bits for SHA-2',
  ),
  default => 256,
);

用于return大小的函数将sha_size除以8:

sub hash_len {
  my $self = shift;
  return $self->sha_size() / 8;
}

因此 return默认为 256/8 = 32。