2019-01-24 14:25:07 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
##
|
|
|
|
## Author......: See docs/credits.txt
|
|
|
|
## License.....: MIT
|
|
|
|
##
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Crypt::PBKDF2;
|
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
sub module_constraints { [[0, 255], [-1, -1], [0, 255], [-1, -1], [-1, -1]] }
|
2019-01-24 14:25:07 +00:00
|
|
|
|
|
|
|
sub module_generate_hash
|
|
|
|
{
|
2019-02-15 20:10:02 +00:00
|
|
|
my $word = shift;
|
|
|
|
my $salt = shift;
|
|
|
|
my $iter = shift // 1024;
|
|
|
|
|
|
|
|
if (length $salt == 0)
|
|
|
|
{
|
|
|
|
$salt = random_hex_string (64, 64);
|
|
|
|
}
|
2019-01-24 14:25:07 +00:00
|
|
|
|
|
|
|
my $pbkdf2 = Crypt::PBKDF2->new
|
|
|
|
(
|
|
|
|
hasher => Crypt::PBKDF2->hasher_from_algorithm ('HMACSHA2', 512),
|
|
|
|
iterations => $iter
|
|
|
|
);
|
|
|
|
|
|
|
|
my $hash_buf = unpack ("H*", $pbkdf2->PBKDF2 (pack ("H*", $salt), $word));
|
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
my $hash = sprintf ("\$ml\$%i\$%s\$%0128s", $iter, $salt, $hash_buf);
|
2019-01-24 14:25:07 +00:00
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
return $hash;
|
2019-01-24 14:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub module_verify_hash
|
|
|
|
{
|
|
|
|
my $line = shift;
|
|
|
|
|
|
|
|
my $index1 = index ($line, ":");
|
|
|
|
|
|
|
|
return if $index1 < 1;
|
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
my $hash_in = substr ($line, 0, $index1);
|
|
|
|
|
2019-01-24 14:25:07 +00:00
|
|
|
my $word = substr ($line, $index1 + 1);
|
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
my $index2 = index ($hash_in, "\$", 5);
|
2019-01-24 14:25:07 +00:00
|
|
|
|
|
|
|
return if $index2 < 1;
|
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
my $index3 = index ($hash_in, "\$", $index2 + 1);
|
2019-01-24 14:25:07 +00:00
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
my $salt = substr ($hash_in, $index2 + 1, $index3 - $index2 - 1);
|
2019-01-24 14:25:07 +00:00
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
my $iter = substr ($hash_in, 4, $index2 - 4);
|
2019-01-24 14:25:07 +00:00
|
|
|
|
|
|
|
return if (int ($iter) < 1);
|
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
return unless defined $salt;
|
|
|
|
return unless defined $word;
|
|
|
|
|
|
|
|
$word = pack_if_HEX_notation ($word);
|
2019-01-24 14:25:07 +00:00
|
|
|
|
2019-02-15 20:10:02 +00:00
|
|
|
my $new_hash = module_generate_hash ($word, $salt, $iter);
|
2019-01-24 14:25:07 +00:00
|
|
|
|
|
|
|
return ($new_hash, $word);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|