File: CBC.pm

package info (click to toggle)
libfile-kdbx-perl 0.906-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,052 kB
  • sloc: perl: 10,607; makefile: 2
file content (98 lines) | stat: -rw-r--r-- 2,046 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package File::KDBX::Cipher::CBC;
# ABSTRACT: A CBC block cipher mode encrypter/decrypter

use warnings;
use strict;

use Crypt::Mode::CBC;
use File::KDBX::Error;
use File::KDBX::Util qw(:class);
use namespace::clean;

extends 'File::KDBX::Cipher';

our $VERSION = '0.906'; # VERSION

has key_size => 32;
sub iv_size     { 16 }
sub block_size  { 16 }

sub encrypt {
    my $self = shift;

    my $mode = $self->{mode} ||= do {
        my $m = Crypt::Mode::CBC->new($self->algorithm);
        $m->start_encrypt($self->key, $self->iv);
        $m;
    };

    return join('', map { $mode->add(ref $_ ? $$_ : $_) } grep { defined } @_);
}

sub decrypt {
    my $self = shift;

    my $mode = $self->{mode} ||= do {
        my $m = Crypt::Mode::CBC->new($self->algorithm);
        $m->start_decrypt($self->key, $self->iv);
        $m;
    };

    return join('', map { $mode->add(ref $_ ? $$_ : $_) } grep { defined } @_);
}

sub finish {
    my $self = shift;
    return '' if !$self->{mode};
    my $out = $self->{mode}->finish;
    delete $self->{mode};
    return $out;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

File::KDBX::Cipher::CBC - A CBC block cipher mode encrypter/decrypter

=head1 VERSION

version 0.906

=head1 SYNOPSIS

    use File::KDBX::Cipher::CBC;

    my $cipher = File::KDBX::Cipher::CBC->new(algorithm => $algo, key => $key, iv => $iv);

=head1 DESCRIPTION

A subclass of L<File::KDBX::Cipher> for encrypting and decrypting data using the CBC block cipher mode.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website
L<https://github.com/chazmcgarvey/File-KDBX/issues>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

Charles McGarvey <ccm@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2022 by Charles McGarvey.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut