1#!/usr/bin/env perl
2
3##
4## Author......: See docs/credits.txt
5## License.....: MIT
6##
7
8use strict;
9use warnings;
10
11use MIME::Base64 qw (encode_base64 decode_base64);
12use Crypt::PBKDF2;
13
14sub module_constraints { [[0, 256], [16, 16], [-1, -1], [-1, -1], [-1, -1]] }
15
16sub module_generate_hash
17{
18  my $word  = shift;
19  my $salt  = shift;
20
21  my $pbkdf2 = Crypt::PBKDF2->new
22  (
23    hasher     => Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA1'),
24    iterations => 10000,
25    output_len => 32
26  );
27
28  my $base64 = encode_base64 ($salt . $pbkdf2->PBKDF2 ($salt, $word), "");
29
30  my $hash = sprintf ("{PKCS5S2}%s", $base64);
31
32  return $hash;
33}
34
35sub module_verify_hash
36{
37  my $line = shift;
38
39  my ($digest, $word) = split ":", $line;
40
41  return unless defined $digest;
42  return unless defined $word;
43
44  my $signature = substr ($digest, 0, 9);
45
46  return unless ($signature eq '{PKCS5S2}');
47
48  my $hash = substr ($digest, 9);
49
50  # base64 buf
51
52  my $base64_decoded = decode_base64 ($hash);
53
54  return if (length ($base64_decoded) != (16 + 32));
55
56  my $salt = substr ($base64_decoded, 0, 16);
57
58  my $word_packed = pack_if_HEX_notation ($word);
59
60  my $new_hash = module_generate_hash ($word_packed, $salt);
61
62  return ($new_hash, $word);
63}
64
651;
66