GPG module writed
This commit is contained in:
parent
dcb70474e3
commit
58eea8d76c
24
Database.pm
24
Database.pm
@ -9,7 +9,7 @@ sub new {
|
||||
# Get home dir
|
||||
my $home = $ENV->{HOME};
|
||||
|
||||
my $gpg = GPG->new();
|
||||
my $gpg = GPG->new($home);
|
||||
|
||||
my $self = {
|
||||
_home => $home,
|
||||
@ -21,24 +21,23 @@ sub new {
|
||||
}
|
||||
|
||||
sub connect {
|
||||
my ($self) = @_;
|
||||
my $home = $self->{_home};
|
||||
my $db_file = $home . "/.PM/db.sqlite";
|
||||
|
||||
my ( $self, $db_file ) = @_;
|
||||
my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_file", "", "" );
|
||||
|
||||
return $dbh;
|
||||
}
|
||||
|
||||
sub mdo {
|
||||
my ( $self, $query, $type ) = @_;
|
||||
my $dbh = $self->{_dbh};
|
||||
my ( $self, $query, $type, $file ) = @_;
|
||||
my $dbh = Database->connect();
|
||||
}
|
||||
|
||||
# Create config dirrectory and DB if not exist
|
||||
sub create_base {
|
||||
my ($self) = @_;
|
||||
my $home = $self->{_home};
|
||||
my $pm_dir = $home . "/.PM/";
|
||||
my $gpg = $self->{_gpg};
|
||||
|
||||
# Check dir
|
||||
if ( !( -d $pm_dir ) ) {
|
||||
@ -47,20 +46,21 @@ sub create_base {
|
||||
my @mkdir_cmd = ( "mkdir", "$pm_dir" );
|
||||
system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n";
|
||||
|
||||
my $first_sqlite = '/tmp/db.sqlite';
|
||||
|
||||
# Create DB file
|
||||
my @createdb_cmd = ( "touch", "$pm_dir/db.sqlite" );
|
||||
my @createdb_cmd = ( "touch", "$first_sqlite" );
|
||||
system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n";
|
||||
|
||||
# Create table. TODO: write this
|
||||
my $dbh
|
||||
= DBI->connect( "dbi:SQLite:dbname=$pm_dir/db.sqlite", "", "" );
|
||||
# Create table.
|
||||
my $dbh = DBI->connect( "dbi:SQLite:dbname=$first_sqlite", "", "" );
|
||||
print "Create database schema\n";
|
||||
my $q_table
|
||||
= "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)";
|
||||
$dbh->do($q_table);
|
||||
|
||||
# Encrypt db
|
||||
# TODO: write this
|
||||
$gpg->encrypt_db($first_sqlite);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
52
GPG.pm
52
GPG.pm
@ -1,15 +1,59 @@
|
||||
# GPG layer for encrypt/decrypt passwords database
|
||||
package GPG;
|
||||
|
||||
# GPG abstraction layer for encrypt/decrypt password database
|
||||
our $gpg = '/usr/bin/gpg';
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my $class = shift;
|
||||
|
||||
my $self = {
|
||||
};
|
||||
my $home = shift;
|
||||
my $db = $home . "/.PM/db.sqlite";
|
||||
|
||||
# Get default private key
|
||||
|
||||
my $self = { _db => $db, };
|
||||
|
||||
bless $self, $class;
|
||||
return $self;
|
||||
}
|
||||
|
||||
# Encrypt sqlite database with default key
|
||||
# and save it in config dir
|
||||
sub encrypt_db {
|
||||
my ( $self, $file ) = @_;
|
||||
my $db = $self->{_db};
|
||||
|
||||
# gpg --output test.gpg --encrypt -a --default-recipient-self test
|
||||
@enc_cmd = (
|
||||
"$gpg", "--output",
|
||||
"$db", "--encrypt",
|
||||
"-a", "--default-recipient-self",
|
||||
"$file"
|
||||
);
|
||||
system(@enc_cmd) == 0 or die "Cannot encrypt! $!\n";
|
||||
|
||||
# Remove unencrypted file
|
||||
@rm_cmd = ( "rm", "$file" );
|
||||
system(@rm_cmd) == 0 or die "Cannot remove file $file: $!\n";
|
||||
}
|
||||
|
||||
# Decrypt database, save it in new place
|
||||
# and return path to file
|
||||
sub decrypt_db {
|
||||
my ($self) = @_;
|
||||
my $db = $self->{_db};
|
||||
|
||||
# Generate random file name
|
||||
my @chars = ( "A" .. "Z", "a" .. "z" );
|
||||
my $string;
|
||||
$string .= $chars[ rand @chars ] for 1 .. 10;
|
||||
my $file = '/tmp/' . 'pm.' . $string;
|
||||
|
||||
# gpg --output /tmp/decryptfile --decrypt $db
|
||||
@dec_cmd = ( "$gpg", "--decrypt", "$db", "--output", "$file" );
|
||||
system(@sys_dec_cmd) == 0 or die "Cannot decrypt $db: $!\n";
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
1;
|
Loading…
Reference in New Issue
Block a user