diff options
Diffstat (limited to 'lib/App/Raps2')
-rw-r--r-- | lib/App/Raps2/Password.pm | 98 | ||||
-rw-r--r-- | lib/App/Raps2/UI.pm | 94 |
2 files changed, 192 insertions, 0 deletions
diff --git a/lib/App/Raps2/Password.pm b/lib/App/Raps2/Password.pm new file mode 100644 index 0000000..2ac1a51 --- /dev/null +++ b/lib/App/Raps2/Password.pm @@ -0,0 +1,98 @@ +package App::Raps2::Password; + + + + +use strict; +use warnings; +use autodie; +use 5.010; + +use base 'Exporter'; + +use Crypt::CBC; +use Crypt::Eksblowfish; +use Crypt::Eksblowfish::Bcrypt qw(bcrypt_hash en_base64 de_base64); + +our @EXPORT_OK = (); +our $VERSION = '0.1'; + +sub new { + my ($obj, %conf) = @_; + + $conf{'cost'} //= 12; + + if (not (defined $conf{'salt'} and length($conf{'salt'}) == 16)) { + return undef; + } + + if (not (defined $conf{'passphrase'} and length $conf{'passphrase'})) { + return undef; + } + + my $ref = \%conf; + + return bless($ref, $obj); +} + +sub salt { + my ($self, $salt) = @_; + + if (not (defined $salt and length($salt) == 16)) { + return undef; + } + + $self->{'salt'} = $salt; +} + +sub encrypt { + my ($self, $in) = @_; + + my $eksblowfish = Crypt::Eksblowfish->new( + $self->{'cost'}, + $self->{'salt'}, + $self->{'passphrase'}, + ); + my $cbc = Crypt::CBC->new(-cipher => $eksblowfish); + + return $cbc->encrypt_hex($in); +} + +sub decrypt { + my ($self, $in) = @_; + + my $eksblowfish = Crypt::Eksblowfish->new( + $self->{'cost'}, + $self->{'salt'}, + $self->{'passphrase'}, + ); + my $cbc = Crypt::CBC->new(-cipher => $eksblowfish); + + return $cbc->decrypt_hex($in); +} + +sub crypt { + my ($self) = @_; + + return en_base64( + bcrypt_hash({ + key_nul => 1, + cost => $self->{'cost'}, + salt => $self->{'salt'}, + }, + $self->{'passphrase'}, + )); +} + +sub verify { + my ($self, $testhash) = @_; + + my $myhash = $self->crypt(); + + if ($testhash eq $myhash) { + return 1; + } + return undef; +} + +1; diff --git a/lib/App/Raps2/UI.pm b/lib/App/Raps2/UI.pm new file mode 100644 index 0000000..43abf29 --- /dev/null +++ b/lib/App/Raps2/UI.pm @@ -0,0 +1,94 @@ +package App::Raps2::UI; + +use strict; +use warnings; +use autodie; +use 5.010; + +use base 'Exporter'; + +use POSIX; + +our @EXPORT_OK = (); +our $VERSION = '0.1'; + +sub new { + my ($obj) = @_; + my $ref = {}; + return bless($ref, $obj); +} + +sub read_line { + my ($self, $str) = @_; + + print "${str}: "; + my $input = readline(STDIN); + + chomp $input; + return $input; +} + +sub read_multiline { + my ($self, $str) = @_; + my $in; + + say "${str} (^D to quit)"; + + while (my $line = <STDIN>) { + $in .= $line; + } + return $in; +} + +sub read_pw { + my ($self, $str, $verify) = @_; + my ($in1, $in2); + + my $term = POSIX::Termios->new(); + $term->getattr(0); + $term->setlflag($term->getlflag() & ~POSIX::ECHO); + $term->setattr(0, POSIX::TCSANOW); + + print "${str}: "; + $in1 = readline(STDIN); + print "\n"; + + if ($verify) { + print 'Verify: '; + $in2 = readline(STDIN); + print "\n"; + } + + $term->setlflag($term->getlflag() | POSIX::ECHO); + $term->setattr(0, POSIX::TCSANOW); + + if ($verify and $in1 ne $in2) { + return undef; + } + + chomp $in1; + return $in1; +} + +sub to_clipboard { + my ($self, $str) = @_; + + open(my $clipboard, '|-', 'xclip -l 1'); + print $clipboard $str; + close($clipboard); + return; +} + +sub output { + my ($self, @out) = @_; + + for my $pair (@out) { + printf( + "%-8s : %s\n", + @{$pair}, + ); + } + return; +} + +1; |