]>
Dogcows Code - chaz/p5-File-KDBX/blob - lib/File/KDBX/Cipher/Stream.pm
f5072a967e543d415690dc78180eb1c840aa8071
1 package File
::KDBX
::Cipher
::Stream
;
2 # ABSTRACT: A cipher stream encrypter/decrypter
7 use Crypt
::Digest
qw(digest_data);
8 use File
::KDBX
::Constants
qw(:cipher :random_stream);
10 use File
::KDBX
::Util
qw(:class);
11 use Scalar
::Util
qw(blessed);
15 extends
'File::KDBX::Cipher';
17 our $VERSION = '0.903'; # VERSION
20 has 'counter', is => 'ro', default => 0;
21 has 'offset', is => 'ro';
22 sub key_size
{ { Salsa20
=> 32, ChaCha
=> 32 }->{$_[0]->{algorithm
} || ''} // 0 }
23 sub iv_size
{ { Salsa20
=> 8, ChaCha
=> 12 }->{$_[0]->{algorithm
} || ''} // -1 }
30 if (my $uuid = $args{uuid
}) {
31 if ($uuid eq CIPHER_UUID_CHACHA20
&& length($args{iv
}) == 16) {
33 my $buf = substr($self->{iv
}, 0, 4, '');
34 $self->{counter
} = unpack('L<', $buf);
36 elsif ($uuid eq CIPHER_UUID_SALSA20
) {
37 # only need eight bytes...
38 $self->{iv
} = substr($args{iv
}, 8);
41 elsif (my $id = $args{stream_id
}) {
42 my $key_ref = ref $args{key
} ? $args{key
} : \
$args{key
};
43 if ($id == STREAM_ID_CHACHA20
) {
44 ($self->{key
}, $self->{iv
}) = unpack('a32 a12', digest_data
('SHA512', $$key_ref));
46 elsif ($id == STREAM_ID_SALSA20
) {
47 ($self->{key
}, $self->{iv
}) = (digest_data
('SHA256', $$key_ref), STREAM_SALSA20_IV
);
57 my $stream = $self->_stream;
58 return join('', map { $stream->crypt(ref $_ ? $$_ : $_) } grep { defined } @_);
64 return $self->_stream->keystream(@_);
70 my $class = blessed
($self);
72 my $dup = bless {%$self, @_}, $class;
73 delete $dup->{stream
};
80 $self->{stream
} //= do {
82 my $pkg = 'Crypt::Stream::'.$self->algorithm;
83 my $counter = $self->counter;
85 if (defined (my $offset = $self->offset)) {
86 $counter = int($offset / 64);
89 my $s = $pkg->new($self->key, $self->iv, $counter);
90 # seek to correct position within block
91 $s->keystream($pos) if $pos;
95 throw
'Failed to initialize stream cipher library',
97 algorithm
=> $self->{algorithm
},
98 key_length
=> length($self->key),
99 iv_length
=> length($self->iv),
100 iv
=> unpack('H*', $self->iv),
101 key
=> unpack('H*', $self->key);
107 sub encrypt
{ goto &crypt }
108 sub decrypt
{ goto &crypt }
110 sub finish
{ delete $_[0]->{stream
}; '' }
122 File::KDBX::Cipher::Stream - A cipher stream encrypter/decrypter
130 use File::KDBX::Cipher::Stream;
132 my $cipher = File::KDBX::Cipher::Stream->new(algorithm => $algorithm, key => $key, iv => $iv);
136 A subclass of L<File::KDBX::Cipher> for encrypting and decrypting data using a stream cipher.
142 $counter = $cipher->counter;
144 Get the initial counter / block count into the keystream.
148 $offset = $cipher->offset;
150 Get the initial byte offset into the keystream. This has precedence over L</counter> if both are set.
156 $ciphertext = $cipher->crypt($plaintext);
157 $plaintext = $cipher->crypt($ciphertext);
159 Encrypt or decrypt some data. These ciphers are symmetric, so encryption and decryption are the same
160 operation. This method is an alias for both L<File::KDBX::Cipher/encrypt> and L<File::KDBX::Cipher/decrypt>.
164 $stream = $cipher->keystream;
166 Access the keystream.
170 $cipher_copy = $cipher->dup(%attributes);
172 Get a copy of an existing cipher with the counter reset, optionally applying new attributes.
176 Please report any bugs or feature requests on the bugtracker website
177 L<https://github.com/chazmcgarvey/File-KDBX/issues>
179 When submitting a bug or request, please include a test-file or a
180 patch to an existing test-file that illustrates the bug or desired
185 Charles McGarvey <ccm@cpan.org>
187 =head1 COPYRIGHT AND LICENSE
189 This software is copyright (c) 2022 by Charles McGarvey.
191 This is free software; you can redistribute it and/or modify it under
192 the same terms as the Perl 5 programming language system itself.
This page took 0.041508 seconds and 3 git commands to generate.