line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# database::keystore Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Database::Keystore; |
7
|
1
|
|
|
1
|
|
742
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
88
|
|
8
|
1
|
|
|
1
|
|
8
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
6
|
use base qw(Metabrik::File::Text); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
501
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
db => [ qw(db) ], |
20
|
|
|
|
|
|
|
}, |
21
|
|
|
|
|
|
|
commands => { |
22
|
|
|
|
|
|
|
search => [ qw(pattern db|OPTIONAL) ], |
23
|
|
|
|
|
|
|
decrypt => [ qw(db|OPTIONAL) ], |
24
|
|
|
|
|
|
|
encrypt => [ qw($data) ], |
25
|
|
|
|
|
|
|
save => [ qw($data db|OPTIONAL) ], |
26
|
|
|
|
|
|
|
load => [ qw(db|OPTIONAL) ], |
27
|
|
|
|
|
|
|
add => [ qw(db|OPTIONAL) ], |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
require_modules => { |
30
|
|
|
|
|
|
|
'Metabrik::Crypto::Aes' => [ ], |
31
|
|
|
|
|
|
|
'Metabrik::File::Write' => [ ], |
32
|
|
|
|
|
|
|
'Metabrik::String::Password' => [ ], |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub search { |
38
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
39
|
0
|
|
|
|
|
|
my ($pattern, $db) = @_; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
0
|
|
|
|
$db ||= $self->db; |
42
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('search', $pattern) or return; |
43
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('search', $db) or return; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my $decrypted = $self->decrypt; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my @results = (); |
48
|
0
|
|
|
|
|
|
my @lines = split(/\n/, $decrypted); |
49
|
0
|
|
|
|
|
|
for (@lines) { |
50
|
0
|
0
|
|
|
|
|
push @results, $_ if /$pattern/i; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
return \@results; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub decrypt { |
57
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
58
|
0
|
|
|
|
|
|
my ($db) = @_; |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
0
|
|
|
|
$db ||= $self->db; |
61
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('decrypt', $db) or return; |
62
|
0
|
0
|
|
|
|
|
$self->brik_help_run_file_not_found('decrypt', $db) or return; |
63
|
|
|
|
|
|
|
|
64
|
0
|
0
|
|
|
|
|
my $read = $self->read($db) or return; |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
|
my $ca = Metabrik::Crypto::Aes->new_from_brik_init($self) or return; |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
my $decrypted = $ca->decrypt($read) or return; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
return $decrypted; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub encrypt { |
74
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
75
|
0
|
|
|
|
|
|
my ($data) = @_; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('encrypt', $data) or return; |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
my $ca = Metabrik::Crypto::Aes->new_from_brik_init($self) or return; |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
my $encrypted = $ca->encrypt($data) or return; |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
return $encrypted; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub save { |
87
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
88
|
0
|
|
|
|
|
|
my ($data, $db) = @_; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
0
|
|
|
|
$db ||= $self->db; |
91
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('save', $data) or return; |
92
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('save', $db) or return; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
$self->write($data, $db) or return; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
return $db; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub load { |
100
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
101
|
0
|
|
|
|
|
|
my ($db) = @_; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
0
|
|
|
|
$db ||= $self->db; |
104
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('load', $db) or return; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
my $decrypted = $self->decrypt; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
my @lines = split(/\n/, $decrypted); |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
return \@lines; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub add { |
114
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
115
|
0
|
|
|
|
|
|
my ($db) = @_; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
0
|
|
|
|
$db ||= $self->db; |
118
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('add', $db) or return; |
119
|
|
|
|
|
|
|
|
120
|
0
|
0
|
|
|
|
|
my $sp = Metabrik::String::Password->new_from_brik_init($self) or return; |
121
|
0
|
0
|
|
|
|
|
my $fw = Metabrik::File::Write->new_from_brik_init($self) or return; |
122
|
0
|
|
|
|
|
|
$fw->overwrite(1); |
123
|
0
|
|
|
|
|
|
$fw->append(0); |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
my $lines = $self->load($db) or return; |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
print "Enter entry name: "; |
128
|
0
|
|
|
|
|
|
chomp(my $name = <>); |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
print "Enter login or identity description: "; |
131
|
0
|
|
|
|
|
|
chomp(my $login = <>); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
my $passwd = $sp->generate->[0]; |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
push @$lines, "$name: $login $passwd"; |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
my $content = join("\n", @$lines); |
138
|
0
|
0
|
|
|
|
|
my $encrypted = $self->encrypt($content) or return; |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
|
$fw->open($db) or return; |
141
|
0
|
0
|
|
|
|
|
$fw->write($encrypted) or return; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
return $db; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
1; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
__END__ |