blib/lib/Crypt/RandomEncryption.pm

filter: Statementstmt 12.5% 10 / 80 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 30.0% 3 / 10 Podpod 66.6% 2 / 3 totaltotal 13.0% 15 / 115 CC 12 SCAR 29.2
CC
12
Cov
10%
_RC4
30.0
encrypt
30.0
decrypt
20.0
CRAP 18.6
↑ Summary
1 package Crypt::RandomEncryption;
2  
3 1 ▶use 5.014002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.01';
8  
9 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
10 0 my $class = shift;
11 0 my $self = bless { @_ }, $class;
12 0 return $self;
13 }
14  
15 sub _packit
16 0 ▶{
Subroutine _packit: not called CC=1 SCAR=6.9
17 0 shift;
18 0 my $phrase=shift;
19 0 return pack("H*",$phrase);
20 }
21  
22 sub _unpackit
23 0 ▶{
Subroutine _unpackit: not called CC=1 SCAR=6.9
24 0 shift;
25 0 my $phrase=shift;
26 0 return unpack("H*",$phrase);
27 }
28  
29 sub _random
30 0 ▶{
Subroutine _random: not called CC=1 SCAR=6.9
31 0 shift;
32 0 my @range=('a'..'z', 'A'..'Z', 0..9);
33 0 return $range[rand($#range+1)];
34 }
35  
36 0 ▶sub _RC4 {
Subroutine _RC4: not called CC=5 SCAR=34.0
37 0 shift;
38 0 my($plaintext, $key) = @_;
39
40 0 my $keylen = length($key);
41 0 my @keyarray;
42 0 ▶ for(my $i=0; $i<=255; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 255) 0 0 0
43 0 $keyarray[$i]=ord(substr($key,($i % $keylen)+1, 1));
44 }
45 0 my @asciiarray;
46 0 ▶ for(my $i=0; $i<=255; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 255) 0 0 0
47 0 $asciiarray[$i] = $i;
48 }
49 0 my $j=0;
50 0 ▶ for(my $i=0; $i<=255; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 255) 0 0 0
51 0 $j=($j + $asciiarray[$i] + $keyarray[$i]) % 256;
52 0 ($asciiarray[$i], $asciiarray[$j])=($asciiarray[$j], $asciiarray[$i]);
53 }
54 0 my $i=0;
55 0 $j=0;
56 0 my $t;
57 my $y;
58 0 my $RC4;
59 0 ▶ for (my $x = 0; $x < length($plaintext); $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < length $plaintext) 0 0 0
60 0 $i=($i + 1) % 256;
61 0 $j=($j + $asciiarray[$i])%256;
62 0 $t = ($asciiarray[$i] + $asciiarray[$j]) % 256;
63 0 ($asciiarray[$i], $asciiarray[$j])=($asciiarray[$j], $asciiarray[$i]);
64 0 $y = $asciiarray[$t];
65 0 $RC4=$RC4 . chr(ord(substr($plaintext, $x, 1))^$y);
66 }
67 0 return $RC4;
68 }
69  
70 sub encrypt
71 0 ▶{
Subroutine encrypt: not called CC=5 SCAR=34.0
Pod documented
72 0 my $class=shift;
73 0 my ($item, $key, $level)=@_;
74  
75 0 ▶ unless($level) { $level=1; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 0 0 0
76 0 $item=$class->_unpackit($class->_RC4($item, $key));
77 0 my @items=split(//, $item);
78  
79 0 my @bet;
80 0 ▶ for(my $j=0; $j < $level; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $level) 0 0 0
81 0 ▶ for(my $i=0; $i <= $#items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#items) 0 0 0
82 0 my $r=$class->_random();
83 0 push(@bet, ($r, $items[$i]));
84 0 ▶ if($i == $#items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#items) 0 0 0
85 0 push(@bet, $r);
86 }
87 }
88 0 @items=@bet;
89 0 @bet=();
90 }
91 0 my $bet=join('', @items);
92 0 my $final=$class->_unpackit($class->_RC4($bet, $key));
93
94 0 return $final;
95 }
96  
97 sub decrypt
98 0 ▶{
Subroutine decrypt: not called CC=4 SCAR=30.0
Pod documented
99 0 my $class=shift;
100 0 my ($encrypted, $key, $level)=@_;
101  
102 0 ▶ unless($level) { $level=1; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($level) 0 0 0
103 0 my $first=$class->_RC4($class->_packit($encrypted),$key);
104 0 my @items=split(//, $first);
105  
106 0 my @bet;
107 0 ▶ for(my $j=0; $j < $level; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $level) 0 0 0
108 0 ▶ for(my $i=1; $i <= $#items; $i=$i+2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#items) 0 0 0
109 0 push(@bet, $items[$i]);
110 }
111 0 @items=@bet;
112 0 @bet=();
113 }
114 0 my $bet=join('', @items);
115 0 my $plain=$class->_RC4($class->_packit($bet),$key);
116
117 0 return $plain;
118 }
119  
120 1 1;
121  
122  
123 __END__
↑ Summary