From 9f84c2ba374a07fcf12fdf41c0d6f1fc3f315655 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 17 May 2011 14:42:25 +0200 Subject: Move create_salt to App::Raps2::Password, make $pw->salt() return current salt --- lib/App/Raps2.pm | 36 ++++++++++++------------------------ lib/App/Raps2/Password.pm | 19 +++++++++++++++++++ t/20-app-raps2-password.t | 26 ++------------------------ t/29-app-raps2.t | 4 +--- 4 files changed, 34 insertions(+), 51 deletions(-) diff --git a/lib/App/Raps2.pm b/lib/App/Raps2.pm index 9d43364..3b97fa9 100644 --- a/lib/App/Raps2.pm +++ b/lib/App/Raps2.pm @@ -75,23 +75,6 @@ sub new { return bless($ref, $obj); } -=item $raps2->create_salt() - -Returns a 16-character random salt for App::Raps2::Password(3pm). - -=cut - -sub create_salt { - my ($self) = @_; - my $salt = q{}; - - for (1 .. 16) { - $salt .= chr(0x21 + int(rand(90))); - } - - return $salt; -} - =item $raps2->file_to_hash(I<$file>) Reads $file (lines with key/value separated by whitespace) and returns a hash @@ -145,7 +128,7 @@ Asks the user for the master passphrase. sub get_master_password { my ($self) = @_; - my $pass = $self->ui()->read_pw('Master Password', 0); + my $pass = $self->ui->read_pw('Master Password', 0); $self->{pass} = App::Raps2::Password->new( cost => $self->{default}->{cost}, @@ -165,15 +148,14 @@ Creates a default config and asks the user to set a master password. sub create_config { my ($self) = @_; my $cost = 12; - my $salt = $self->create_salt(); - my $pass = $self->ui()->read_pw('Master Password', 1); + my $pass = $self->ui->read_pw('Master Password', 1); $self->{pass} = App::Raps2::Password->new( cost => $cost, - salt => $salt, passphrase => $pass, ); - my $hash = $self->{pass}->crypt(); + my $hash = $self->pw->crypt(); + my $salt = $self->pw->salt(); write_file( $self->{xdg_conf} . '/password', @@ -205,7 +187,13 @@ Returns the App::Raps2::Password(3pm) object. sub pw { my ($self) = @_; - return $self->{pass}; + + if (defined $self->{pass}) { + return $self->{pass}; + } + else { + confess('No App::Raps2::Password object, did you call get_master_password?'); + } } =item $raps2->ui() @@ -235,7 +223,7 @@ sub cmd_add { $self->get_master_password(); - my $salt = $self->create_salt(); + my $salt = $self->pw->create_salt(); my $url = $self->ui->read_line('URL'); my $login = $self->ui->read_line('Login'); my $pass = $self->ui->read_pw('Password', 1); diff --git a/lib/App/Raps2/Password.pm b/lib/App/Raps2/Password.pm index 10431ed..b236b7b 100644 --- a/lib/App/Raps2/Password.pm +++ b/lib/App/Raps2/Password.pm @@ -17,6 +17,10 @@ sub new { $conf{cost} //= 12; + if (not defined $conf{salt}) { + $conf{salt} = create_salt(); + } + if (not (defined $conf{salt} and length($conf{salt}) == 16)) { confess('incorrect salt length'); } @@ -30,9 +34,24 @@ sub new { return bless($ref, $obj); } +sub create_salt { + my ($self) = @_; + my $salt = q{}; + + for (1 .. 16) { + $salt .= chr(0x21 + int(rand(90))); + } + + return $salt; +} + sub salt { my ($self, $salt) = @_; + if (not defined $salt) { + return $self->{salt}; + } + if (not (defined $salt and length($salt) == 16)) { confess('incorrect salt length'); } diff --git a/t/20-app-raps2-password.t b/t/20-app-raps2-password.t index ae4f267..5faa7e9 100644 --- a/t/20-app-raps2-password.t +++ b/t/20-app-raps2-password.t @@ -3,7 +3,7 @@ use strict; use warnings; use 5.010; -use Test::More tests => 19; +use Test::More tests => 17; use Test::Fatal; my $pw; @@ -16,14 +16,6 @@ like( exception { App::Raps2::Password->new(); }, - qr{incorrect salt length}, - 'new() missing salt and passphrase' -); - -like( - exception { - App::Raps2::Password->new(salt => $salt); - }, qr{no passphrase given}, 'new() missing passphrase' ); @@ -39,14 +31,6 @@ like( 'new() missing passphrase' ); -like( - exception { - App::Raps2::Password->new(passphrase => $pass); - }, - qr{incorrect salt length}, - 'new() missing salt' -); - like( exception { App::Raps2::Password->new( @@ -100,13 +84,7 @@ like( ok($pw->verify($pw->crypt('truth')), 'crypt->verify okay'); -like( - exception { - $pw->salt(); - }, - qr{incorrect salt length}, - 'salt: No argument', -); +is($pw->salt(), $salt, 'salt() returns current salt'); like( exception { diff --git a/t/29-app-raps2.t b/t/29-app-raps2.t index ceaaf51..8034791 100644 --- a/t/29-app-raps2.t +++ b/t/29-app-raps2.t @@ -3,7 +3,7 @@ use strict; use warnings; use 5.010; -use Test::More tests => 4; +use Test::More tests => 3; use Test::Fatal; use_ok('App::Raps2'); @@ -11,8 +11,6 @@ use_ok('App::Raps2'); my $r2 = App::Raps2->new(); isa_ok($r2, 'App::Raps2'); -is(length($r2->create_salt()), 16, 'create_salt: correct length'); - is_deeply( { $r2->file_to_hash('t/in/hash') }, { key => 'value', otherkey => 'othervalue' }, -- cgit v1.2.3