use warnings;
use Carp;
-use parent qw/Exporter Authen::Passphrase Class::Accessor::Fast/;
+use parent qw/Exporter Authen::Passphrase/;
our @EXPORT = qw/crypto_scrypt/;
our @EXPORT_OK = @EXPORT;
require XSLoader;
XSLoader::load('Authen::Passphrase::Scrypt', $VERSION);
-__PACKAGE__->mk_accessors(qw/data logN r p salt hmac passphrase/);
+use Object::Tiny qw/data logN r p salt hmac passphrase/;
sub compute_hash {
my ($self, $passphrase) = @_;
sub new {
my ($class, @args) = @_;
- my $self = $class->SUPER::new(@args);
+ if ('HASH' eq ref $args[0]) { # we were given a hash
+ @args = %{$args[0]}
+ }
+ unshift @args, logN => 14, r => 16, p => 1; # default values
+ my %args = @args;
+ $args{salt} = rand_bits 256 unless exists $args{salt};
+ my $self = bless \%args, $class;
- $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;
- $self->data($data);
- $self->hmac(hmac_sha256 $self->data, truncate_hash $self->compute_hash($self->passphrase));
+ $self->{data} = $data;
+ $self->{hmac} = hmac_sha256 $self->data, truncate_hash $self->compute_hash($self->passphrase);
$self
}
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});
+ bless { data => (substr $data, 0, 64), logN => $logN, r => $r, p => $p, salt => $salt, hmac => $hmac }, $class;
}
sub match {