Bug 20473: Whitespace
[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 C4::Context;
25 use Koha::Exceptions;
26
27 =head1 NAME
28
29 Koha::Encryption - Koha class to encrypt or decrypt strings
30
31 =head1 SYNOPSIS
32
33   use Koha::Encryption;
34   my $secret    = Koha::AuthUtils::generate_salt( 'weak', 16 );
35   my $crypt     = Koha::Encryption->new;
36   my $encrypted = $crypt->encrypt_hex($secret);
37   my $decrypted = $crypt->decrypt_hex($encrypted);
38
39   return 1 if $decrypted eq $secret;
40
41 It's based on Crypt::CBC
42
43 =cut
44
45 =head2 METHODS
46
47 =head3 new
48
49     my $cipher = Koha::Encryption->new;
50
51     Constructor. Uses encryption_key from koha-conf.xml.
52
53 =cut
54
55 sub new {
56     my ( $class ) = @_;
57     my $key = C4::Context->config('encryption_key');
58     if( !$key ) {
59         Koha::Exceptions::MissingParameter->throw('No encryption_key in koha-conf.xml');
60     }
61     return $class->SUPER::new(
62         -key    => $key,
63         -cipher => 'Cipher::AES'
64     );
65 }
66
67 1;