Bug 30714: Unit test
[koha.git] / Koha / Encryption.pm
1 package Koha::Encryption;
2
3 # Copyright 2022 Koha Development Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use base qw( Crypt::CBC );
23
24 use Koha::Exceptions;
25
26 =head1 NAME
27
28 Koha::Encryption - Koha class to encrypt or decrypt strings
29
30 =head1 SYNOPSIS
31
32   use Koha::Encryption;
33   my $secret    = Koha::AuthUtils::generate_salt( 'weak', 16 );
34   my $crypt     = Koha::Encryption->new;
35   my $encrypted = $crypt->encrypt_hex($secret);
36   my $decrypted = $crypt->decrypt_hex($encrypted);
37
38   return 1 if $decrypted eq $secret;
39
40 It's based on Crypt::CBC
41
42 =cut
43
44 =head2 METHODS
45
46 =head3 new
47
48     my $cipher = Koha::Encryption->new;
49
50     Constructor. Uses encryption_key from koha-conf.xml.
51
52 =cut
53
54 sub new {
55     my ( $class ) = @_;
56     my $key = C4::Context->config('encryption_key');
57     if( !$key ) {
58         Koha::Exceptions::MissingParameter->throw('No encryption_key in koha-conf.xml');
59     }
60     return $class->SUPER::new(
61         -key    => $key,
62         -cipher => 'Cipher::AES'
63     );
64 }
65
66 1;