blib/lib/Crypt/OTP26.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 100% 83 / 83 CC 5 SCAR 4.6
CC
5
Cov
100%
__ANON__
3.0
crypt
2.0
decrypt
2.0
CRAP 1.6
↑ Summary
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 Crypt::OTP26 - a classic form of encryption
6  
7 =head1 DESCRIPTION
8  
9 This implements a mod-26 One Time Pad encryption, similar to the sort
10 classically used with pen and paper, as described in
11 L<http://en.wikipedia.org/wiki/One_time_pad>
12  
13 Its primary use is to explore the intriguing situation detailed
14 at L<http://itre.cis.upenn.edu/~myl/languagelog/archives/003314.html>
15  
16 NB: We don't handle the cases of generating or securely transmitting the pads
17 themselves.
18  
19 Also, only lower case alpha (ascii 'a-z') characters are handled.
20 If you are actually encrypting and transmitting useful and valuable data, you
21 should use a proper strong crypto module.
22  
23 And though it's based on the OneTimePad concept, it actually supports the pad
24 being shorter or longer than the encrypted text, in which case it is truncated
25 or repeated as appropriate. So... don't do that!
26  
27 =cut
28  
29 package Crypt::OTP26;
30  
31 3 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
32 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 3 our $VERSION = 0.03;
35 3 ▶use constant ORD_A => ord('a');
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 =head1 METHODS
38  
39 =head2 C<new>
40  
41 my $otp = Crypt::OTP26->new();
42  
43 =cut
44  
45 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
46 1 my $class = shift;
47 1 return bless {}, $class;
48 }
49  
50 =head2 C<crypt>
51  
52 Encrypts an alpha text (a-z) with an alpha pad (a-z), by
53 performing mod26 addition on it.
54  
55 my $encrypted = $otp->crypt( $pad, $text );
56 # though it's commutative, so can be in either order
57  
58 my $encrypted = $otp->crypt( 'aced', 'scam' );
59 # returns 'seep'
60  
61 =cut
62  
63 5 ▶sub crypt {
Subroutine crypt: called CC=2 SCAR=6.9
Pod documented
64 5 my ($self, $string1, $string2) = @_;
65  
66 5 my $stream = $self->mk_stream( $string1, $string2 );
67  
68 5 my $new_string = '';
69
70 5 ▶ while (my ($char1, $char2) = $stream->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$stream()) 24 5 29
71 24 $new_string .= $self->crypt_char( $char1, $char2 );
72 }
73 5 return $new_string;
74 }
75  
76 =head2 C<decrypt>
77  
78 Decrpyts a previously encrypted text using mod26 sutraction.
79  
80 my $encrypted = $otp->decrypt( $crypt, $pad );
81 my $encrypted = $otp->decrypt( 'aced', 'seep' );
82 # returns 'scam'
83  
84 =cut
85  
86 6 ▶sub decrypt {
Subroutine decrypt: called CC=2 SCAR=6.9
Pod documented
87 6 my ($self, $string1, $string2) = @_;
88  
89 6 my $stream = $self->mk_stream( $string1, $string2 );
90  
91 6 my $new_string = '';
92
93 6 ▶ while (my ($char1, $char2) = $stream->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$stream()) 28 6 34
94 28 $new_string .= $self->decrypt_char( $char2, $char1 );
95 }
96 6 return $new_string;
97 }
98  
99 =head2 C<char2int>
100  
101 Return the mod26 integer value of an ascii character.
102  
103 my $int = $otp->char2int('a');
104 # returns 0
105  
106 =cut
107  
108 113 ▶sub char2int {
Subroutine char2int: called CC=1 SCAR=0.0
Pod documented
109 113 my $self = shift;
110 113 my $char = shift;
111 113 return ord(lc $char) - ORD_A;
112 }
113  
114 =head2 C<int2char>
115  
116 my $char = $otp->int2char( 1 );
117 # returns 'b'
118  
119 Will always return 'a'-'z'
120  
121 =cut
122  
123 59 ▶sub int2char {
Subroutine int2char: called CC=1 SCAR=0.0
Pod documented
124 59 my $self = shift;
125 59 my $int = shift;
126 59 return chr( ORD_A + ($int % 26));
127 }
128  
129 =head2 C<crypt_char>
130  
131 my $char = $otp->crypt_char( 'a', 's' );
132 # returns 's'
133  
134 Crypts 2 characters by performing mod26 addition on them. Called internally by
135 L<crypt> above.
136  
137 =cut
138
139 25 ▶sub crypt_char {
Subroutine crypt_char: called CC=1 SCAR=0.0
Pod documented
140 25 my ($self, $char1, $char2) = @_;
141 25 my $int1 = $self->char2int( $char1 );
142 25 my $int2 = $self->char2int( $char2 );
143 25 my $int3 = $int1 + $int2;
144 25 return $self->int2char( $int3 );
145 }
146  
147 =head2 C<decrypt_char>
148  
149 Decrypts the character with the appropriate letter from the pad, by performing
150 mod26 subtraction. Called internally L<decrypt> above.
151  
152 my $char = $otp->decrypt_char( $crypt_char, $pad_char );
153 my $char = $otp->decrypt_char( 't', 's' );
154 # returns 'b'
155  
156 =cut
157  
158 30 ▶sub decrypt_char {
Subroutine decrypt_char: called CC=1 SCAR=0.0
Pod documented
159 30 my ($self, $crypt_char, $pad_char) = @_;
160 30 my $int1 = $self->char2int( $crypt_char );
161 30 my $int2 = $self->char2int( $pad_char );
162 30 my $int3 = $int1 - $int2;
163 30 return $self->int2char( $int3 );
164 }
165  
166 =head2 C<mk_stream>
167  
168 Private method for iterating the pad and the string.
169  
170 =cut
171  
172 12 ▶sub mk_stream {
Subroutine mk_stream: called CC=1 SCAR=0.0
Pod documented
173 12 my $self = shift;
174 12 my ($string1, $string2) = @_;
175  
176 # $string1 is the pad, and will repeat if necessary
177  
178 12 my @stream1 = split '', $string1;
179 12 my @stream2 = split '', $string2;
180  
181 69 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
182 69 ▶ return unless @stream2;
Branch: 1 branch
BranchTrueFalseTotal
unless @stream2 12 57 69
183 57 ▶ @stream1 = split '', $string1 unless @stream1;
Branch: 1 branch
BranchTrueFalseTotal
unless @stream1 6 51 57
184 57 my $char1 = splice(@stream1, 0, 1);
185 57 my $char2 = splice(@stream2, 0, 1);
186 57 return ($char1, $char2);
187 }
188 12 }
189  
190 =head1 AUTHOR
191  
192 (C) 2009
193 osfameron@cpan.org
194  
195 May be distributed under the same conditions as Perl itself
196  
197 Repo is at L<http://github.com/osfameron/crypt-otp26/>
198  
199 (Clone url: git://github.com/osfameron/crypt-otp26.git )
200  
201 =cut
202  
203 3 1;
204  
↑ Summary