]>
Dogcows Code - chaz/p5-File-KDBX/blob - lib/File/KDBX/IO/Crypt.pm
1 package File
::KDBX
::IO
::Crypt
;
2 # ABSTRACT: Encrypter/decrypter IO handle
9 use File
::KDBX
::Util
qw(:class :empty);
12 extends
'File::KDBX::IO';
14 our $VERSION = '0.904'; # VERSION
15 our $BUFFER_SIZE = 16384;
22 while (my ($attr, $default) = each %ATTRS) {
23 no strict
'refs'; ## no critic (ProhibitNoStrict)
26 *$self->{$attr} = shift if @_;
27 *$self->{$attr} //= (ref $default eq 'CODE') ? $default->($self) : $default;
34 my %args = @_ % 2 == 1 ? (fh
=> shift, @_) : @_;
35 my $self = $class->SUPER::new
;
36 $self->_fh($args{fh
}) or throw
'IO handle required';
37 $self->cipher($args{cipher
}) or throw
'Cipher required';
44 $ENV{DEBUG_STREAM
} and print STDERR
"FILL\t$self\n";
45 my $cipher = $self->cipher or return;
47 $fh->read(my $buf = '', $BUFFER_SIZE);
48 if (0 < length($buf)) {
49 my $plaintext = eval { $cipher->decrypt($buf) };
51 $self->_set_error($err);
54 return $plaintext if 0 < length($plaintext);
58 my $plaintext = eval { $cipher->finish };
60 $self->_set_error($err);
68 my ($self, $buf, $fh) = @_;
70 $ENV{DEBUG_STREAM
} and print STDERR
"WRITE\t$self\n";
71 my $cipher = $self->cipher or return 0;
73 my $new_data = eval { $cipher->encrypt($buf) } || '';
75 $self->_set_error($err);
78 $self->_buffer_out_add($new_data) if nonempty
$new_data;
85 $ENV{DEBUG_STREAM
} and print STDERR
"POPPED\t$self\n";
86 return if $self->_mode ne 'w';
87 my $cipher = $self->cipher or return;
89 my $new_data = eval { $cipher->finish } || '';
91 $self->_set_error($err);
94 $self->_buffer_out_add($new_data) if nonempty
$new_data;
101 my ($self, $fh) = @_;
103 $ENV{DEBUG_STREAM
} and print STDERR
"FLUSH\t$self\n";
104 return if $self->_mode ne 'w';
106 my $buffer = $self->_buffer_out;
108 my $read = shift @$buffer;
110 $fh->print($read) or return -1;
117 $ENV{DEBUG_STREAM
} and print STDERR
"err\t$self\n";
118 if (exists &Errno
::EPROTO
) {
121 elsif (exists &Errno
::EIO
) {
124 $self->cipher(undef);
125 $self->_error($ERROR = File
::KDBX
::Error-
>new(@_));
138 File::KDBX::IO::Crypt - Encrypter/decrypter IO handle
146 use File::KDBX::IO::Crypt;
147 use File::KDBX::Cipher;
149 my $cipher = File::KDBX::Cipher->new(...);
151 open(my $out_fh, '>:raw', 'ciphertext.bin');
152 $out_fh = File::KDBX::IO::Crypt->new($out_fh, cipher => $cipher);
154 print $out_fh $plaintext;
158 open(my $in_fh, '<:raw', 'ciphertext.bin');
159 $in_fh = File::KDBX::IO::Crypt->new($in_fh, cipher => $cipher);
161 my $plaintext = do { local $/; <$in_fh> );
169 A L<File::KDBX::Cipher> instance to do the actual encryption or decryption.
175 $fh = File::KDBX::IO::Crypt->new(%attributes);
176 $fh = File::KDBX::IO::Crypt->new($fh, %attributes);
178 Construct a new crypto IO handle.
182 Please report any bugs or feature requests on the bugtracker website
183 L<https://github.com/chazmcgarvey/File-KDBX/issues>
185 When submitting a bug or request, please include a test-file or a
186 patch to an existing test-file that illustrates the bug or desired
191 Charles McGarvey <ccm@cpan.org>
193 =head1 COPYRIGHT AND LICENSE
195 This software is copyright (c) 2022 by Charles McGarvey.
197 This is free software; you can redistribute it and/or modify it under
198 the same terms as the Perl 5 programming language system itself.
This page took 0.045287 seconds and 4 git commands to generate.