diff options
author | Daniel Friesel <derf@finalrewind.org> | 2011-06-07 15:50:37 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2011-06-07 15:50:37 +0200 |
commit | 88010542443bf1f83906a0bbf7e3003d045471f6 (patch) | |
tree | bd13f1ce0f9a91b8f1853ea4b4c7c470a0e7af65 /lib/App | |
parent | f4d7e354367d78f36024255ca598431e3508005e (diff) |
Raps2/Password: Accept salt for encrypt/decrypt, makes Raps2.pm less messy
Diffstat (limited to 'lib/App')
-rw-r--r-- | lib/App/Raps2.pm | 21 | ||||
-rw-r--r-- | 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<salt>. -=item $pass->encrypt(I<data>) +=item $pass->encrypt(I<data>, [I<salt>]) Encrypts I<data> with the passphrase saved in the object, returns the corresponding hexadecimal hash (as string). -=item $pass->decrypt(I<hexstr>) +By default, the salt set in B<salt> or B<new> will be used. You can override +it by specifying I<salt>. + +=item $pass->decrypt(I<hexstr>, [I<salt>]) Decrypts I<hexstr> (as created by B<encrypt>), returns its original content. +By default, the salt set in B<salt> or B<new> will be used. You can override +it by specifying I<salt>. + =item $pass->bcrypt() Return a base64 bcrypt hash of the password, salted with the salt. |