Authen-Passphrase-Scrypt

 view release on metacpan or  search on metacpan

lib/Authen/Passphrase/Scrypt.pm  view on Meta::CPAN

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        my $sha = sha256 shift;
        substr $sha, 0, 16
}
 
sub truncate_hash {
        substr shift, 32
}
 
sub new {
        my ($class, @args) = @_;
        my $self = $class->SUPER::new(@args);
 
        $self->logN(14) unless defined $self->logN;
        $self->r(16) unless defined $self->r;
        $self->p(1) unless defined $self->p;
        croak "passphrase not set" unless defined $self->passphrase;
        $self->salt(rand_bits 256) unless $self->salt;
 
        my $data = "scrypt\x00" . pack 'CNNa32',
          $self->logN, $self->r, $self->p, $self->salt;
        $data .= truncated_sha256 $data;

lib/Authen/Passphrase/Scrypt.pm  view on Meta::CPAN

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
}
 
sub from_rfc2307 {
        my ($class, $rfc2307) = @_;
        croak "Invalid Scrypt RFC2307" unless $rfc2307 =~ m,^{SCRYPT}([A-Za-z0-9+/]{128})$,;
        my $data = decode_base64 $1;
        my ($scrypt, $logN, $r, $p, $salt, $cksum, $hmac) =
          unpack 'Z7CNNa32a16a32', $data;
        croak 'Invalid Scrypt hash: should start with "scrypt"' unless $scrypt eq 'scrypt';
        croak 'Invalid Scrypt hash: bad checksum', unless $cksum eq truncated_sha256 (substr $data, 0, 48);
        $class->SUPER::new({data => (substr $data, 0, 64), logN => $logN, r => $r, p => $p, salt => $salt, hmac => $hmac});
}
 
sub match {
        my ($self, $passphrase) = @_;
        my $correct_hmac = hmac_sha256 $self->data, truncate_hash $self->compute_hash($passphrase);
        $self->hmac eq $correct_hmac
}
 
sub as_rfc2307 {
        my ($self) = @_;



( run in 0.274 second using v1.01-cache-2.11-cpan-eab888a1d7d )