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::SHA  qw (sha256);
12use MIME::Base64 qw (encode_base64);
13
14sub module_constraints { [[0, 256], [-1, -1], [0, 55], [-1, -1], [-1, -1]] }
15
16my $CISCO_BASE64_MAPPING =
17{
18  'A', '.', 'B', '/', 'C', '0', 'D', '1', 'E', '2', 'F', '3', 'G', '4', 'H',
19  '5', 'I', '6', 'J', '7', 'K', '8', 'L', '9', 'M', 'A', 'N', 'B', 'O', 'C',
20  'P', 'D', 'Q', 'E', 'R', 'F', 'S', 'G', 'T', 'H', 'U', 'I', 'V', 'J', 'W',
21  'K', 'X', 'L', 'Y', 'M', 'Z', 'N', 'a', 'O', 'b', 'P', 'c', 'Q', 'd', 'R',
22  'e', 'S', 'f', 'T', 'g', 'U', 'h', 'V', 'i', 'W', 'j', 'X', 'k', 'Y', 'l',
23  'Z', 'm', 'a', 'n', 'b', 'o', 'c', 'p', 'd', 'q', 'e', 'r', 'f', 's', 'g',
24  't', 'h', 'u', 'i', 'v', 'j', 'w', 'k', 'x', 'l', 'y', 'm', 'z', 'n', '0',
25  'o', '1', 'p', '2', 'q', '3', 'r', '4', 's', '5', 't', '6', 'u', '7', 'v',
26  '8', 'w', '9', 'x', '+', 'y', '/', 'z'
27};
28
29sub module_generate_hash
30{
31  my $word = shift;
32
33  my $digest = sha256 ($word);
34
35  my $base64_buf = encode_base64 ($digest, "");
36
37  my $hash = "";
38
39  for (my $i = 0; $i < 43; $i++)
40  {
41    $hash .= $CISCO_BASE64_MAPPING->{substr ($base64_buf, $i, 1)};
42  }
43
44  return $hash;
45}
46
47sub module_verify_hash
48{
49  my $line = shift;
50
51  my ($hash, $word) = split (':', $line);
52
53  return unless defined $hash;
54  return unless defined $word;
55
56  my $word_packed = pack_if_HEX_notation ($word);
57
58  my $new_hash = module_generate_hash ($word_packed);
59
60  return ($new_hash, $word);
61}
62
631;
64