1#!/usr/bin/env perl
2
3##
4## Author......: See docs/credits.txt
5## License.....: MIT
6##
7
8use strict;
9use warnings;
10
11use Digest::MD5 qw (md5_hex);
12
13sub module_constraints { [[0, 256], [16, 16], [-1, -1], [-1, -1], [-1, -1]] }
14
15sub module_generate_hash
16{
17  my $word = shift;
18  my $salt = shift;
19  my $iter = shift // 1000;
20
21  my $data;
22
23  $data .= $salt;
24  $data .= $word x $iter;
25  $data .= $word;
26
27  my $digest = md5_hex ($data);
28
29  my $hash;
30
31  if ($iter == 1000)
32  {
33    $hash = sprintf ("\@m\@%s\@%s", $digest, $salt);
34  }
35  else
36  {
37    $hash = sprintf ("\@m,%u\@%s\@%s", $iter, $digest, $salt);
38  }
39
40  return $hash;
41}
42
43sub module_verify_hash
44{
45  my $line = shift;
46
47  my ($hash, $word) = split (':', $line);
48
49  return unless defined $hash;
50  return unless defined $word;
51
52  my (undef, $tag, $digest, $salt) = split (/\@/, $hash);
53
54  my ($type, $iter) = split (/\,/, $tag);
55
56  return unless ($type eq "m");
57
58  my $word_packed = pack_if_HEX_notation ($word);
59
60  my $new_hash = module_generate_hash ($word_packed, $salt, $iter);
61
62  return ($new_hash, $word);
63}
64
651;
66