Enigma.pm

filter: Statementstmt 20.0% 27 / 135 Branchbran 8.3% 1 / 12 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 30.7% 4 / 13 Podpod 0.0% 0 / 9 totaltotal 19.3% 36 / 186 SCAR 47.4
CC
14
Cov
20%
input
30.0
setup
20.0
advance_rotors
20.0
CRAP 113.9
↑ Summary bombe.pl »
1 package Crypt::Enigma;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9  
10 1 our @ISA = qw(Exporter);
11  
12 # Items to export into callers namespace by default. Note: do not export
13 # names by default without a very good reason. Use EXPORT_OK instead.
14 # Do not simply export all your public functions/methods/constants.
15  
16 # This allows declaration use Crypt::Enigma ':all';
17 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
18 # will save memory.
19 1 our %EXPORT_TAGS = ( 'all' => [ qw(
20
21 ) ] );
22  
23 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
24  
25 1 our @EXPORT = qw(
26
27 );
28 1 our $VERSION = '1.1';
29  
30  
31 # Preloaded methods go here.
32  
33 # Enigma.pm by Jason Blakey
34 # jblakey@frogboy.net
35  
36  
37 #
38 # DEFINITIONS
39 #
40  
41 # Define our rotors, where the notches are on each rotor, and the
42 # reflector.
43 1 our (@ROTOR0) = qw (e k m f l g d q v z n t o w y h x u s p a i b r c j);
44 1 our (@ROTOR1) = qw (a j d k s i r u x b l h w t m c q g z n p y f v o e);
45 1 our (@ROTOR2) = qw (b d f h j l c p r t x v z n y e i w g a k m u s q o);
46 1 our (@ROTOR3) = qw (e s o v p z j a y q u i r h x l n f t g k d c m w b);
47 1 our (@ROTOR4) = qw (v z b r g i t y u p s d n h l x a w m j q o f e c k);
48  
49 # To quiet the -w warnings...
50 1 ▶if (@ROTOR0 and @ROTOR1 and @ROTOR2 and @ROTOR3 and @ROTOR4) {};
Branch: 1 branch
BranchTrueFalseTotal
if (@Crypt::Enigma::ROTOR0 and @Crypt::Enigma::ROTOR1 and @Crypt::Enigma::ROTOR2 and @Crypt::Enigma::ROTOR3 and @Crypt::Enigma::ROTOR4) 1 0 1
Condition33%4/12
@Crypt::Enigma::ROTOR0 and @Crypt::Enigma::ROTOR1 and @Crypt::Enigma::ROTOR2 and @Crypt::Enigma::ROTOR3 and @Crypt::Enigma::ROTOR4
!ll&&!rl&&r
001
@Crypt::Enigma::ROTOR0 and @Crypt::Enigma::ROTOR1 and @Crypt::Enigma::ROTOR2 and @Crypt::Enigma::ROTOR3
!ll&&!rl&&r
001
@Crypt::Enigma::ROTOR0 and @Crypt::Enigma::ROTOR1 and @Crypt::Enigma::ROTOR2
!ll&&!rl&&r
001
@Crypt::Enigma::ROTOR0 and @Crypt::Enigma::ROTOR1
!ll&&!rl&&r
001
MC/DC0%0/5
@Crypt::Enigma::ROTOR0 and @Crypt::Enigma::ROTOR1 and @Crypt::Enigma::ROTOR2 and @Crypt::Enigma::ROTOR3 and @Crypt::Enigma::ROTOR4
@Crypt::Enigma::ROTOR0 @Crypt::Enigma::ROTOR1 @Crypt::Enigma::ROTOR2 @Crypt::Enigma::ROTOR3 @Crypt::Enigma::ROTOR4
Truth table0%0/6
A and B and C and D and E
A= @Crypt::Enigma::ROTOR0B= @Crypt::Enigma::ROTOR1C= @Crypt::Enigma::ROTOR2D= @Crypt::Enigma::ROTOR3E= @Crypt::Enigma::ROTOR4
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
51  
52 1 our (@NOTCHES) = qw (7 25 11 7 2);
53  
54 1 our (@REFLECTOR) = qw (y r u h q s l d p x n g o k m i e b f z c w v j a t);
55  
56 #
57 # SUBROUTINES
58 #
59  
60 sub new {
61 0 ▶ my ($class) = $_[0];
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
62 # Create a new anonymous hash...
63 0 my ($self) = {};
64  
65 # Make it an object...
66 0 bless ($self, $class);
67  
68 # And return the created object...
69 0 return ($self);
70 }
71  
72 sub setup {
73 0 ▶ my ($self) = $_[0];
Subroutine setup: not called CC=4 SCAR=30.0
Pod undocumented
74 0 my ($rotors) = $_[1];
75 0 my ($ring_settings) = $_[2];
76 0 my ($initial_settings) = $_[3];
77  
78 # Reverse the input so that they make sense...
79 0 my (@rotors) = reverse (split (//, $rotors));
80 0 my (@ring_settings) = reverse (split (//, $ring_settings));
81 0 my (@initial_settings) = reverse (split (//, $initial_settings));
82  
83 0 our (%NOTCHES);
84  
85 #
86 # So first, we need to put the rotors inside the
87 # enigma in the correct order and combination.
88 #
89  
90 0 my ($rotor);
91 0 foreach $rotor (0 .. $#rotors) {
92 # Initialize the number of clicks on each rotor...
93 0 $self->{ROTORS}->{$rotor}->{CLICKS} = 0;
94  
95 # Store the notch position for this rotor.
96 0 $self->{ROTORS}->{$rotor}->{NOTCH} = $NOTCHES[$rotor];
97  
98 # Store the ring setting for this rotor.
99 0 my ($ring_setting) = chr2num ($ring_settings[$rotor]);
100 0 $self->{ROTORS}->{$rotor}->{RING_SETTING} = $ring_setting;
101  
102 # Determine the variable name for this rotor
103 0 my ($rotorname) = "ROTOR".($rotors[$rotor] - 1);
104  
105 0 my ($input);
106 0 foreach $input (0 .. $#{$rotorname}) {
107  
108 0 my ($output) = ${$rotorname}[$input];
109 0 $output = chr2num ($output);
110  
111 0 my ($new_input) = ($input + $ring_setting) % 26;
112 0 my ($new_output) = ($output - $ring_setting) % 26;
113  
114 0 $self->{ROTORS}->{$rotor}->{FORWARD}->{$new_input} =
115 $new_output;
116 0 $self->{ROTORS}->{$rotor}->{REVERSE}->{$new_output} =
117 $new_input;
118 }
119  
120 #
121 # And advance each rotor until it is in the requested
122 # initial position.
123 #
124  
125 0 my ($initial_setting) = $initial_settings[$rotor];
126 0 my ($required_turns) =
127 (chr2num ($initial_setting) - $ring_setting) % 26;
128  
129 0 ▶ while ($required_turns > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required_turns > 0) 0 0 0
130 0 $self->roll_rotor ($rotor);
131 0 $required_turns--;
132 }
133 }
134  
135 # And return...
136 0 return ();
137 }
138  
139 sub stekker {
140 0 ▶ my ($self) = $_[0];
Subroutine stekker: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my ($input_chr) = $_[1];
142 0 my ($output_chr) = $_[2];
143  
144 0 my ($input_num) = chr2num ($input_chr);
145 0 my ($output_num) = chr2num ($output_chr);
146  
147 # Store the stekker'ed positions...
148 0 $self->{STEKKER}->{$input_num} = $output_num;
149 0 $self->{STEKKER}->{$output_num} = $input_num;
150 }
151  
152 sub input {
153 0 ▶ my ($self) = $_[0];
Subroutine input: not called CC=5 SCAR=34.0
Pod undocumented
154 0 my ($input_chr) = $_[1];
155  
156 0 our (%REFLECTOR);
157  
158 # First, we convert the letter to a number
159 0 my ($input_num) = chr2num($input_chr);
160  
161 # Next, we go through the stekkerboard.
162 # If the input character was stekker'ed...
163 0 ▶ if (defined ($self->{STEKKER}->{$input_num})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"STEKKER"}{$input_num}) 0 0 0
164 0 $input_num = $self->{STEKKER}->{$input_num};
165 }
166  
167 # Next, we need to move the rotors forward by 1 click...
168 0 $self->advance_rotors ();
169  
170 # Next, we go through the rotors forward.
171 0 my ($rotor);
172 0 foreach $rotor (sort (keys (%{$self->{ROTORS}}))) {
173 $input_num =
174 0 $self->{ROTORS}->{$rotor}->{FORWARD}->{$input_num};
175 }
176  
177 # Now, we go through the reflector.
178 0 $input_chr = $REFLECTOR[$input_num];
179 0 $input_num = chr2num ($input_chr);
180  
181 # Next, we go back through the rotors in REVERSE.
182 0 foreach $rotor (reverse (sort (keys (%{$self->{ROTORS}})))) {
183 $input_num =
184 0 $self->{ROTORS}->{$rotor}->{REVERSE}->{$input_num};
185 }
186  
187 # Next, back through the Stekker.
188 0 ▶ if (defined ($self->{STEKKER}->{$input_num})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"STEKKER"}{$input_num}) 0 0 0
189 0 $input_num = $self->{STEKKER}->{$input_num};
190 }
191  
192 # Convert the number back into a character.
193 0 my ($output_char) = num2chr($input_num);
194  
195 # And finally return the result.
196 0 return ($output_char);
197 }
198  
199 sub advance_rotors {
200 0 ▶ my ($self) = $_[0];
Subroutine advance_rotors: not called CC=4 SCAR=30.0
Pod undocumented
201  
202 # The first rotor always gets moved one click forward, so
203 # default to a positive advance_check.
204 0 my ($we_should_advance) = 1;
205  
206 # Step through each rotor...
207 0 my ($rotor);
208 0 foreach $rotor (sort (keys (%{$self->{ROTORS}}))) {
209  
210 # If we should advance this rotor...
211 0 ▶ if ($we_should_advance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($we_should_advance) 0 0 0
212  
213 # Now, we need to rotate the rotors values...
214 0 $self->roll_rotor ($rotor);
215  
216 # If the new notch position is not 0, then
217 # we leave advance_rotor set so that we will
218 # advance the next rotor...
219  
220 my ($notch_position) =
221 0 $self->{ROTORS}->{$rotor}->{NOTCH};
222  
223 0 ▶ if ($notch_position != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notch_position != 0) 0 0 0
224 0 $we_should_advance = 0;
225 }
226 }
227 }
228  
229 # And return.
230 0 return ();
231 }
232  
233 sub roll_rotor {
234 0 ▶ my ($self) = $_[0];
Subroutine roll_rotor: not called CC=3 SCAR=24.8
Pod undocumented
235 0 my ($rotor) = $_[1];
236  
237 0 my (%temp);
238  
239 #
240 # Step through each transformation this rotor holds.
241 #
242  
243 my ($position);
244 0 foreach $position (keys (%{$self->{ROTORS}->{$rotor}->{FORWARD}})) {
245  
246 # move the position up one...
247 0 my ($new_position) = ($position + 1) % 26;
248  
249 # And move the value for that position down one...
250 my ($value) =
251 0 $self->{ROTORS}->{$rotor}->{FORWARD}->{$position};
252 0 my ($new_value) = ($value - 1) % 26;
253  
254 # Store the transformation in a temporary hash...
255 0 $temp{$rotor}->{FORWARD}->{$new_position} = $new_value;
256 }
257  
258 # Now, copy the temprotors over into rotors...
259 0 foreach $position (sort (keys (%{$temp{$rotor}->{FORWARD}}))) {
260 0 my ($value) = $temp{$rotor}->{FORWARD}->{$position};
261 0 $self->{ROTORS}->{$rotor}->{FORWARD}->{$position} = $value;
262 0 $self->{ROTORS}->{$rotor}->{REVERSE}->{$value}= $position;
263 }
264  
265 # Next, we compute the new notch position for this rotor we just
266 # clicked.
267  
268 0 my ($current_notch) = $self->{ROTORS}->{$rotor}->{NOTCH};
269  
270 0 my ($new_notch) = ($current_notch - 1) % 26;
271 0 $self->{ROTORS}->{$rotor}->{NOTCH} = $new_notch;
272  
273 # Update the number of clicks for this rotor.
274 0 my ($clicks) = $self->{ROTORS}->{$rotor}->{CLICKS};
275 0 $self->{ROTORS}->{$rotor}->{CLICKS} = $clicks + 1;
276  
277 # And return...
278 0 return ();
279 }
280  
281 sub view {
282 0 ▶ my ($self) = $_[0];
Subroutine view: not called CC=2 SCAR=17.9
Pod undocumented
283  
284 0 my ($view) = "";
285  
286 0 my ($rotor);
287 0 foreach $rotor (reverse (sort (keys (%{$self->{ROTORS}})))) {
288  
289 0 my ($clicks) = $self->{ROTORS}->{$rotor}->{CLICKS};
290 0 my ($ring_setting) = $self->{ROTORS}->{$rotor}->{RING_SETTING};
291  
292 0 my ($num) = ($clicks + $ring_setting) % 26;
293 0 my ($chr) = num2chr ($num);
294 0 $view .= "$chr";
295 }
296 0 return ($view);
297 }
298  
299 # A couple of number -> letter, letter -> number routines...
300 sub chr2num {
301 0 ▶ my ($character) = $_[0];
Subroutine chr2num: not called CC=1 SCAR=6.9
Pod undocumented
302 0 return (ord ($character) - 97);
303 }
304  
305 sub num2chr {
306 0 ▶ my ($number) = $_[0];
Subroutine num2chr: not called CC=1 SCAR=6.9
Pod undocumented
307 0 return (chr($number + 97));
308 }
309  
310 1 return (1);
311 __END__
↑ Summary bombe.pl »