From 88010542443bf1f83906a0bbf7e3003d045471f6 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 7 Jun 2011 15:50:37 +0200 Subject: Raps2/Password: Accept salt for encrypt/decrypt, makes Raps2.pm less messy --- lib/App/Raps2.pm | 21 +++++++-------------- lib/App/Raps2/Password.pm | 26 ++++++++++++++++++-------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/App/Raps2.pm b/lib/App/Raps2.pm index 133b898..7de4b1a 100644 --- a/lib/App/Raps2.pm +++ b/lib/App/Raps2.pm @@ -126,12 +126,10 @@ sub ui { sub pw_add { my ( $self, %data ) = @_; - $self->pw->salt( $data{salt} ); - - my $pass_hash = $self->pw->encrypt( $data{password} ); + my $pass_hash = $self->pw->encrypt( $data{password}, $data{salt} ); my $extra_hash = ( $data{extra} - ? $self->pw->encrypt( $data{extra} ) + ? $self->pw->encrypt( $data{extra}, $data{salt} ) : q{} ); @@ -181,15 +179,13 @@ sub pw_get { my %key = $self->file_to_hash( $data{file} ); - $self->pw->salt( $key{salt} ); - return { url => $key{url}, login => $key{login}, - password => $self->pw->decrypt( $key{hash} ), + password => $self->pw->decrypt( $key{hash}, $key{salt} ), extra => ( $key{extra} - ? $self->pw->decrypt( $key{extra} ) + ? $self->pw->decrypt( $key{extra}, $key{salt} ) : undef ), }; @@ -233,7 +229,6 @@ sub cmd_edit { my %key = $self->file_to_hash($pwfile); $self->get_master_password(); - $self->pw->salt( $key{salt} ); my $salt = $key{salt}; my $url = $self->ui->read_line( 'URL', $key{url} ); @@ -242,7 +237,7 @@ sub cmd_edit { my $extra = $key{extra} // q{}; if ( length($pass) ) { - $pass_hash = $self->pw->encrypt($pass); + $pass_hash = $self->pw->encrypt( $pass, $salt ); } else { $pass_hash = $key{hash}; @@ -272,12 +267,10 @@ sub cmd_get { $self->get_master_password(); - $self->pw->salt( $key{salt} ); - - $self->ui()->to_clipboard( $self->pw->decrypt( $key{hash} ) ); + $self->ui()->to_clipboard( $self->pw->decrypt( $key{hash}, $key{salt} ) ); if ( $key{extra} ) { - print $self->pw->decrypt( $key{extra} ); + print $self->pw->decrypt( $key{extra}, $key{salt} ); } return; diff --git a/lib/App/Raps2/Password.pm b/lib/App/Raps2/Password.pm index 1a3ab63..3662c29 100644 --- a/lib/App/Raps2/Password.pm +++ b/lib/App/Raps2/Password.pm @@ -59,20 +59,24 @@ sub salt { } sub encrypt { - my ( $self, $in ) = @_; + my ( $self, $in, $salt ) = @_; - my $eksblowfish = Crypt::Eksblowfish->new( $self->{cost}, $self->{salt}, - $self->{passphrase}, ); + $salt //= $self->{salt}; + + my $eksblowfish + = Crypt::Eksblowfish->new( $self->{cost}, $salt, $self->{passphrase}, ); my $cbc = Crypt::CBC->new( -cipher => $eksblowfish ); return $cbc->encrypt_hex($in); } sub decrypt { - my ( $self, $in ) = @_; + my ( $self, $in, $salt ) = @_; + + $salt //= $self->{salt}; - my $eksblowfish = Crypt::Eksblowfish->new( $self->{cost}, $self->{salt}, - $self->{passphrase}, ); + my $eksblowfish + = Crypt::Eksblowfish->new( $self->{cost}, $salt, $self->{passphrase}, ); my $cbc = Crypt::CBC->new( -cipher => $eksblowfish ); return $cbc->decrypt_hex($in); @@ -169,15 +173,21 @@ Returns a new 16-byte salt. Contains only printable characters. Returns the currently used salt and optionally changes it to I. -=item $pass->encrypt(I) +=item $pass->encrypt(I, [I]) Encrypts I with the passphrase saved in the object, returns the corresponding hexadecimal hash (as string). -=item $pass->decrypt(I) +By default, the salt set in B or B will be used. You can override +it by specifying I. + +=item $pass->decrypt(I, [I]) Decrypts I (as created by B), returns its original content. +By default, the salt set in B or B will be used. You can override +it by specifying I. + =item $pass->bcrypt() Return a base64 bcrypt hash of the password, salted with the salt. -- cgit v1.2.3