1#!/usr/bin/env perl
2
3##
4## Author......: See docs/credits.txt
5## License.....: MIT
6##
7
8use strict;
9use warnings;
10
11use Crypt::ScryptKDF qw (scrypt_hash);
12use MIME::Base64 qw (decode_base64);
13
14sub module_constraints { [[0, 256], [1, 15], [-1, -1], [-1, -1], [-1, -1]] }
15
16sub module_generate_hash
17{
18  my $word = shift;
19  my $salt = shift;
20  my $N    = shift // 16384;
21  my $r    = shift // 8;
22  my $p    = shift // 1;
23
24  my $hash_buf = scrypt_hash ($word, $salt, $N, $r, $p, 32);
25
26  my $hash = sprintf ('%s', $hash_buf);
27
28  return $hash;
29}
30
31sub module_verify_hash
32{
33  my $line = shift;
34
35  # scrypt
36  return unless (substr ($line, 0, 7) eq 'SCRYPT:');
37
38  # get hash
39  my $index1 = index ($line, ":", 7);
40
41  return if $index1 < 1;
42
43  # N
44  my $N = substr ($line, 7, $index1 - 7);
45
46  my $index2 = index ($line, ":", $index1 + 1);
47
48  return if $index2 < 1;
49
50  # r
51  my $r = substr ($line, $index1 + 1, $index2 - $index1 - 1);
52
53  $index1 = index ($line, ":", $index2 + 1);
54
55  return if $index1 < 1;
56
57  # p
58  my $p = substr ($line, $index2 + 1, $index1 - $index2 - 1);
59
60  $index2 = index ($line, ":", $index1 + 1);
61
62  return if $index2 < 1;
63
64  # salt
65  my $salt = substr ($line, $index1 + 1, $index2 - $index1 - 1);
66
67  $salt = decode_base64 ($salt);
68
69  $index1 = index ($line, ":", $index2 + 1);
70
71  return if $index1 < 1;
72
73  # digest
74
75  my $word = substr ($line, $index1 + 1);
76
77  return unless defined $salt;
78  return unless defined $word;
79  return unless defined $N;
80  return unless defined $r;
81  return unless defined $p;
82
83  $word = pack_if_HEX_notation ($word);
84
85  my $new_hash = module_generate_hash ($word, $salt, $N, $r, $p);
86
87  return ($new_hash, $word);
88}
89
901;
91