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);
12
13sub module_constraints { [[0, 232], [0, 232], [0, 32], [0, 32], [0, 32]] }
14
15sub module_generate_hash
16{
17  my $word = shift;
18  my $salt = shift;
19
20  # we need to reduce the maximum password and salt buffer size by 23 since we
21  # add it here statically
22
23  my $final = sprintf ("%s:Administration Tools:%s", $salt, $word);
24
25  my $hash_buf = md5 ($final);
26
27  my $res = "";
28
29  for (my $pos = 0; $pos < 16; $pos += 2)
30  {
31    my $octet1 = ord (substr ($hash_buf, $pos + 0, 1));
32    my $octet2 = ord (substr ($hash_buf, $pos + 1, 1));
33
34    my $num = (($octet1 << 8) & 0xff00)
35            | (($octet2 << 0) & 0x00ff);
36
37    my $idx1 = $num >> 12 & 0x0f;
38    my $idx2 = $num >>  6 & 0x3f;
39    my $idx3 = $num       & 0x3f;
40
41    my $itoa64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
42
43    $res = $res . substr ($itoa64, $idx1, 1) . substr ($itoa64, $idx2, 1) . substr ($itoa64, $idx3, 1);
44  }
45
46  my $obfuscate_str = "nrcstn";
47  my @obfuscate_pos = (0, 6, 12, 17, 23, 29);
48
49  foreach my $pos (keys @obfuscate_pos)
50  {
51    my $idx = $obfuscate_pos[$pos];
52
53    my $before = substr ($res, 0, $idx);
54    my $char   = substr ($obfuscate_str, $pos, 1);
55    my $after  = substr ($res, $idx);
56
57    $res = sprintf ("%s%s%s", $before, $char, $after);
58  }
59
60  my $hash = sprintf ("%s:%s", $res, $salt);
61
62  return $hash;
63}
64
65sub module_verify_hash
66{
67  my $line = shift;
68
69  my ($hash, $salt, $word) = split (':', $line);
70
71  return unless defined $hash;
72  return unless defined $salt;
73  return unless defined $word;
74
75  $word = pack_if_HEX_notation ($word);
76
77  my $new_hash = module_generate_hash ($word, $salt);
78
79  return ($new_hash, $word);
80}
81
821;
83