File Coverage

blib/lib/Crypt/Hill.pm
Criterion Covered Total %
statement 44 44 100.0
branch 4 4 100.0
condition n/a
subroutine 11 11 100.0
pod 2 3 66.6
total 61 62 98.3


line stmt bran cond sub pod time code
1             package Crypt::Hill;
2              
3             $Crypt::Hill::VERSION = '0.11';
4             $Crypt::Hill::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Crypt::Hill - Interface to the Hill cipher (2x2).
9              
10             =head1 VERSION
11              
12             Version 0.11
13              
14             =cut
15              
16 5     5   146738 use 5.006;
  5         43  
17 5     5   1796 use Data::Dumper;
  5         25543  
  5         323  
18 5         340 use Crypt::Hill::Utils qw(
19             to_matrix_1_x_2
20             to_matrix_2_x_1
21             inverse_matrix
22             generate_table
23             get_determinant
24             multiply_mod
25 5     5   1467 );
  5         11  
26              
27 5     5   1621 use Moo;
  5         43715  
  5         29  
28 5     5   8744 use namespace::autoclean;
  5         49732  
  5         16  
29              
30             my $CHARSETS = ['A'..'Z'];
31             my $KEY_LENGTH = 4;
32             my $BLOCK_SIZE = 2;
33              
34             has 'table' => (is => 'ro', default => sub { generate_table($CHARSETS); });
35             has 'block_size' => (is => 'ro', default => sub { $BLOCK_SIZE });
36             has 'key' => (is => 'ro', required => 1);
37             has 'encrypt_key' => (is => 'rw');
38              
39             =head1 DESCRIPTION
40              
41             The Hill cipher is an example of a block cipher. A block cipher is a cipher in
42             which groups of letters are enciphered together in equal length blocks. The Hill
43             cipher was developed by Lester Hill & introduced in an article published in 1929.
44              
45             The L module is using block size 2.Acceptable characters are A to Z.
46              
47             =head1 CONSTRUCTOR
48              
49             The constructor expects one parameter i.e. key to be used in the encryption and
50             decryption.The key should consists of no more than 4 ALPHABETS.
51              
52             use strict; use warnings;
53             use Crypt::Hill;
54              
55             my $crypt = Crypt::Hill->new({ key => 'DDCF' });
56              
57             =cut
58              
59             sub BUILD {
60 4     4 0 17 my ($self) = @_;
61              
62 4         11 my $encrypt_key = $self->_encrypt_key;
63 1         6 $self->encrypt_key($encrypt_key);
64             }
65              
66             =head1 METHODS
67              
68             =head2 encode($message)
69              
70             Encodes the message using the key provided and returns encoded message.
71              
72             use strict; use warnings;
73             use Crypt::Hill;
74              
75             my $crypt = Crypt::Hill->new({ key => 'DDCF' });
76             my $encoded = $crypt->encode('HELP');
77              
78             print "Encoded: [$encoded]\n";
79              
80             =cut
81              
82             sub encode {
83 1     1 1 6 my ($self, $message) = @_;
84              
85 1         5 return $self->_process($self->encrypt_key, $message);
86             }
87              
88             =head2 decode($encoded_message)
89              
90             Decodes the encoded message using the key provided.
91              
92             use strict; use warnings;
93             use Crypt::Hill;
94              
95             my $crypt = Crypt::Hill->new({ key => 'DDCF' });
96             my $encoded = $crypt->encode('HELP');
97             my $decoded = $crypt->decode($encoded);
98              
99             print "Encoded: [$encoded]\n";
100             print "Decoded: [$decoded]\n";
101              
102             =cut
103              
104             sub decode {
105 1     1 1 3 my ($self, $message) = @_;
106              
107 1         3 return $self->_process($self->_decrypt_key, $message);
108             }
109              
110             #
111             #
112             # PRIVATE METHODS
113              
114             sub _process {
115 2     2   4 my ($self, $key, $message) = @_;
116              
117 2         12 my @chars = @$CHARSETS;
118 2         3 my $modulos = scalar(@chars);
119 2         5 my $size = $self->block_size;
120 2         3 my $table = $self->table;
121 2         5 my $_message = to_matrix_2_x_1($message, $CHARSETS, $table, $size);
122 2         3 my $result = '';
123 2         7 foreach (@$_message) {
124 4         10 my $_matrix = multiply_mod($key, $_, $modulos);
125 4         15 $result .= sprintf("%s%s", $chars[$_matrix->[0][0]], $chars[$_matrix->[1][0]]);
126             }
127              
128 2         13 return $result;
129             }
130              
131             # convert key to matrix (1x2)
132             sub _encrypt_key {
133 4     4   7 my ($self) = @_;
134              
135 4         12 my $table = $self->table;
136 4         8 my $key = $self->key;
137 4 100       26 die "ERROR: Key should be of length $KEY_LENGTH." unless (length($key) == $KEY_LENGTH);
138              
139 2         5 my $size = $self->block_size;
140 2         8 my $enc_key = to_matrix_1_x_2($key, $CHARSETS, $table, $size);
141 2 100       10 die "ERROR: Invalid key [$key] supplied." if (get_determinant($enc_key) == 0);
142              
143 1         2 return $enc_key;
144             }
145              
146             # descrypt key to matrix (2x2)
147             sub _decrypt_key {
148 1     1   2 my ($self) = @_;
149              
150 1         3 my $key = $self->encrypt_key;
151 1         2 my $modulus = scalar(@$CHARSETS);
152              
153 1         4 return inverse_matrix($key, $modulus);
154             }
155              
156             =head1 AUTHOR
157              
158             Mohammad S Anwar, C<< >>
159              
160             =head1 REPOSITORY
161              
162             L
163              
164             =head1 BUGS
165              
166             Please report any bugs/feature requests to C or
167             through the web interface at L.
168             I will be notified & then you'll automatically be notified of progress on your bug
169             as I make changes.
170              
171             =head1 SUPPORT
172              
173             You can find documentation for this module with the perldoc command.
174              
175             perldoc Crypt::Hill
176              
177             You can also look for information at:
178              
179             =over 4
180              
181             =item * RT: CPAN's request tracker
182              
183             L
184              
185             =item * AnnoCPAN: Annotated CPAN documentation
186              
187             L
188              
189             =item * CPAN Ratings
190              
191             L
192              
193             =item * Search CPAN
194              
195             L
196              
197             =back
198              
199             =head1 LICENSE AND COPYRIGHT
200              
201             Copyright (C) 2014 - 2017 Mohammad S Anwar.
202              
203             This program is free software; you can redistribute it and/or modify it under
204             the terms of the the Artistic License (2.0). You may obtain a copy of the full
205             license at:
206              
207             L
208              
209             Any use, modification, and distribution of the Standard or Modified Versions is
210             governed by this Artistic License.By using, modifying or distributing the Package,
211             you accept this license. Do not use, modify, or distribute the Package, if you do
212             not accept this license.
213              
214             If your Modified Version has been derived from a Modified Version made by someone
215             other than you,you are nevertheless required to ensure that your Modified Version
216             complies with the requirements of this license.
217              
218             This license does not grant you the right to use any trademark, service mark,
219             tradename, or logo of the Copyright Holder.
220              
221             This license includes the non-exclusive, worldwide, free-of-charge patent license
222             to make, have made, use, offer to sell, sell, import and otherwise transfer the
223             Package with respect to any patent claims licensable by the Copyright Holder that
224             are necessarily infringed by the Package. If you institute patent litigation
225             (including a cross-claim or counterclaim) against any party alleging that the
226             Package constitutes direct or contributory patent infringement,then this Artistic
227             License to you shall terminate on the date that such litigation is filed.
228              
229             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
230             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
231             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
232             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
233             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
234             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
235             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
236              
237             =cut
238              
239             1; # End of Crypt::Hill