| 1 | package Crypter; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use 5.022; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 2 | ▶ | use Strong::Random; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use POSIX qw(ceil); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 2 | our $VERSION = '1.1'; | |||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 12 | 1 | my $class = shift; | |||||||||||
| 13 | 1 | my %opts = ( | |||||||||||
| 14 | password => undef, | ||||||||||||
| 15 | random => [], | ||||||||||||
| 16 | n => 0, | ||||||||||||
| 17 | obfuscate => 0, | ||||||||||||
| 18 | uncrypt_obfuscate_overstep => 0, | ||||||||||||
| 19 | @_, | ||||||||||||
| 20 | ); | ||||||||||||
| 21 | |||||||||||||
| 22 | 1 | my $self = { | |||||||||||
| 23 | %opts, | ||||||||||||
| 24 | }; | ||||||||||||
| 25 | |||||||||||||
| 26 | 1 | $self = bless $self, $class; | |||||||||||
| 27 | |||||||||||||
| 28 | # Init randomerz | ||||||||||||
| 29 | 1 | $self->init_randoms_by_password; | |||||||||||
| 30 | |||||||||||||
| 31 | 1 | return $self; | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | |||||||||||||
| 35 | 2 | ▶ | sub init_randoms_by_password { | ||||||||||
|
Subroutine
init_randoms_by_password: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||
| 36 | 2 | my $self = shift; | |||||||||||
| 37 | |||||||||||||
| 38 | 2 | $self->{random} = []; | |||||||||||
| 39 | |||||||||||||
| 40 | 2 | ▶ | my $password = $self->{password} // die "Password is not specified"; | ||||||||||
|
Condition50%1/2
$self->{"password"} // die("Password is not specified")
MC/DC0%0/1
$self->{"password"} // die("Password is not specified")
$self->{"password"}
Truth table50%1/2
A
A= $self->{"password"}
| |||||||||||||
| 41 | 2 | my @random = (); | |||||||||||
| 42 | |||||||||||||
| 43 | 2 | for (1..ceil(length($password)/4)) { | |||||||||||
| 44 | 2 | my $p = substr($self->{password}, 4*$_-4, 4); | |||||||||||
| 45 | 2 | ▶ | $p .= ' 'x (4 - length $p) if (length $p < 4); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | |||||||||||||
| 47 | |||||||||||||
| 48 | 2 | my $x = unpack('I!', $p); | |||||||||||
| 49 | |||||||||||||
| 50 | 2 | ▶ | $x = $x ? 1.0 / $x : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | |||||||||||||
| 52 | |||||||||||||
| 53 | 2 | my $rnd = new Strong::Random($x); | |||||||||||
| 54 | 2 | $rnd->rand(); | |||||||||||
| 55 | |||||||||||||
| 56 | 2 | my $nn = unpack('I!', $p) % 256; | |||||||||||
| 57 | |||||||||||||
| 58 | 2 | $rnd->rand for 1..$nn; | |||||||||||
| 59 | |||||||||||||
| 60 | 2 | my $seed = sprintf("%.8g", $rnd->{seed}); | |||||||||||
| 61 | |||||||||||||
| 62 | 2 | push @{$self->{random}}, new Strong::Random($seed); | |||||||||||
| 63 | } | ||||||||||||
| 64 | } | ||||||||||||
| 65 | |||||||||||||
| 66 | 1 | ▶ | sub crypt { | ||||||||||
|
Subroutine
crypt: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||
| 67 | 1 | my $self = shift; | |||||||||||
| 68 | 1 | my $str = shift; | |||||||||||
| 69 | |||||||||||||
| 70 | 1 | my @input = split //, $str; | |||||||||||
| 71 | |||||||||||||
| 72 | 1 | my $out = ''; | |||||||||||
| 73 | 1 | ▶ | for (my $i = 0 ; $i < scalar @input ; $i ++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 12 | my $chr = $input[$i]; | |||||||||||
| 75 | 12 | my $ord = ord $chr; | |||||||||||
| 76 | |||||||||||||
| 77 | 12 | my $r = $self->{random}->[$self->{n}]; | |||||||||||
| 78 | 12 | ▶ | $self->{n} = 0 if (++$self->{n} >= scalar @{$self->{random}}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | |||||||||||||
| 80 | 12 | ▶ | if ($self->{obfuscate}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 0 | my $nr = $r->rand($self->{obfuscate}); | |||||||||||
| 82 | 0 | $out .= chr $r->rand(256) for 1..$nr; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 12 | $ord ^= $_->rand(256) for @{$self->{random}}; | |||||||||||
| 86 | 12 | $out .= chr $ord; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 1 | return $out; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | |||||||||||||
| 93 | 1 | ▶ | sub uncrypt { | ||||||||||
|
Subroutine
uncrypt: called CC=9 SCAR=31.0
Pod
undocumented
| |||||||||||||
| 94 | 1 | my $self = shift; | |||||||||||
| 95 | 1 | my $input = shift; | |||||||||||
| 96 | |||||||||||||
| 97 | 1 | my @input = split //, $input; | |||||||||||
| 98 | 1 | my $out = ''; | |||||||||||
| 99 | 1 | ▶ | for (my $i = 0 ; $i < scalar @input ; $i ++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 12 | my $chr = $input[$i]; | |||||||||||
| 101 | 12 | my $ord = ord $chr; | |||||||||||
| 102 | |||||||||||||
| 103 | |||||||||||||
| 104 | 12 | my $r = $self->{random}->[$self->{n}]; | |||||||||||
| 105 | 12 | ▶ | $self->{n} = 0 if (++$self->{n} >= scalar @{$self->{random}}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | |||||||||||||
| 107 | 12 | ▶ | if ($self->{obfuscate}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | 0 | ▶ | if ($self->{uncrypt_obfuscate_overstep}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | 0 | ▶ | if (scalar @input > $self->{uncrypt_obfuscate_overstep}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 0 | $i += $self->{uncrypt_obfuscate_overstep}; | |||||||||||
| 111 | 0 | $chr = $input[$i]; | |||||||||||
| 112 | 0 | $ord = ord $chr; | |||||||||||
| 113 | } else { | ||||||||||||
| 114 | 0 | $self->{uncrypt_obfuscate_overstep} -= scalar @input; | |||||||||||
| 115 | 0 | return $out; | |||||||||||
| 116 | } | ||||||||||||
| 117 | } else { | ||||||||||||
| 118 | 0 | my $nr = $r->rand($self->{obfuscate}); | |||||||||||
| 119 | 0 | $r->rand(256) for 1..$nr; | |||||||||||
| 120 | |||||||||||||
| 121 | 0 | $i += $nr; | |||||||||||
| 122 | 0 | ▶ | if ($i >= scalar @input) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 123 | 0 | $self->{uncrypt_obfuscate_overstep} += $i - scalar @input; | |||||||||||
| 124 | 0 | return $out; | |||||||||||
| 125 | } | ||||||||||||
| 126 | 0 | $chr = $input[$i]; | |||||||||||
| 127 | |||||||||||||
| 128 | 0 | $ord = ord $chr; | |||||||||||
| 129 | } | ||||||||||||
| 130 | } | ||||||||||||
| 131 | |||||||||||||
| 132 | 12 | $ord ^= $_->rand(256) for @{$self->{random}}; | |||||||||||
| 133 | 12 | $out .= chr $ord; | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 1 | return $out; | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 1 | ▶ | sub reset { | ||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 140 | 1 | my $self = shift; | |||||||||||
| 141 | |||||||||||||
| 142 | 1 | $self->{n} = 0; | |||||||||||
| 143 | 1 | $self->{uncrypt_obfuscate_overstep} = 0; | |||||||||||
| 144 | |||||||||||||
| 145 | 1 | $self->init_randoms_by_password(); | |||||||||||
| 146 | } | ||||||||||||
| 147 | |||||||||||||
| 148 | 2 | 1; | |||||||||||
| 149 | |||||||||||||
| 150 | =encoding utf8 | ||||||||||||
| 151 | |||||||||||||
| 152 | =head1 NAME | ||||||||||||
| 153 | |||||||||||||
| 154 | Crypter - New kind of encryption data | ||||||||||||
| 155 | |||||||||||||
| 156 | =head1 SYNOPSIS | ||||||||||||
| 157 | |||||||||||||
| 158 | |||||||||||||
| 159 | my $crypter = new Crypter(password => '123'); | ||||||||||||
| 160 | |||||||||||||
| 161 | my $encrypted = ''; | ||||||||||||
| 162 | |||||||||||||
| 163 | my $encrypted = $crypter->crypt('Hello World!'); | ||||||||||||
| 164 | |||||||||||||
| 165 | $crypter->reset(); # Needeble for cleanup internals | ||||||||||||
| 166 | |||||||||||||
| 167 | my $decrypted = $crypter->uncrypt($encrypted); | ||||||||||||
| 168 | |||||||||||||
| 169 | #### $decrypted eq 'Hello World!'; | ||||||||||||
| 170 | |||||||||||||
| 171 | |||||||||||||
| 172 | =head1 VERSION | ||||||||||||
| 173 | |||||||||||||
| 174 | 1.1 | ||||||||||||
| 175 | |||||||||||||
| 176 | =head1 AUTHOR | ||||||||||||
| 177 | |||||||||||||
| 178 | All code writen by Ivan Trunaev. | ||||||||||||
| 179 | |||||||||||||
| 180 | Contacts <ivan@trunaev.ru> | ||||||||||||
| 181 | |||||||||||||
| 182 | =cut | ||||||||||||
| 183 | |||||||||||||