blib/lib/Crypt/AllOrNothing/Util.pm

filter: Statementstmt 87.6% 85 / 97 Branchbran 75.0% 30 / 40 Conditioncond 77.7% 14 / 18 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 82.6% 148 / 179 CC 27 SCAR 21.5
CC
27
Cov
83%
randomValue
15.0
addLength_andPad
7.0
largeNumToChar
6.3
CRAP 8.5
↑ Summary
1 package Crypt::AllOrNothing::Util;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Crypt::Random qw/ makerandom /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 require Exporter;
9  
10 =head1 NAME
11  
12 Crypt::AllOrNothing::Util - Util functions for Crypt::AllOrNothing
13  
14 =head1 VERSION
15  
16 Version 0.09
17  
18 =cut
19  
20 3 our $VERSION = '0.09';
21  
22 =head1 SYNOPSIS
23  
24 use Crypt::AoN::Util qw/:all/;
25  
26 $randomValue = randomValue(size=>128, return=>'ascii');
27 @brokenString = breakString(string=>$string, size=>$size);
28 addLength_andPad(array=>\@array, size=>$packetSize);
29 remLength_andPad(array=>\@array);
30 $charString = largeNumToChar(size=>$size, number=>$number);
31  
32 =head1 EXPORT
33  
34 randomValue
35 breakString
36 addLength_andPad
37 remLength_andPad
38 largeNumToChar
39  
40 =cut
41  
42 3 our @ISA = qw(Exporter);
43 3 our %EXPORT_TAGS = ( 'all' => [ qw(
44 randomValue
45 breakString
46 addLength_andPad
47 remLength_andPad
48 largeNumToChar
49 ) ] );
50 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
51  
52 3 our @EXPORT = qw(
53 );
54  
55 =head1 FUNCTIONS
56  
57 =head2 remLength_andPad(array=>\@array)
58  
59 This function takes an array as proccessed by addLength_andPad and removes the padding and length from it.
60 'array' is a reference to the array to be processed. This is processed inline, the array you pass will be changed when the function returns.
61  
62 =cut
63  
64 1 ▶sub remLength_andPad {
Subroutine remLength_andPad: called CC=4 SCAR=13.9
Pod documented
65 1 my %params=@_;
66 ######
67 #takes an array, sent as array, passed as reference
68 ######
69 1 my $length = unpack("L",substr($params{array}->[$#{$params{array}}], (length $params{array}->[$#{$params{array}}]) - 4 ));
70 1 my $runningLength=0;
71 1 ▶ for (my $i=0; $i < scalar @{$params{array}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$params{"array"};}) 5 1 6
72 5 ▶ if ($runningLength == $length) {
Branch: 2 branches
BranchTrueFalseTotal
if ($runningLength == $length) { } 1 4 5
elsif (length($params{'array'}[$i]) + $runningLength > $length) { } 1 3 4
73 1 pop @{$params{array}};
74 } elsif ((length $params{array}->[$i]) + $runningLength > $length) {
75 1 substr($params{array}->[$i],$length-$runningLength) = '';
76 1 $runningLength = $length;
77 } else {
78 3 $runningLength += length $params{array}->[$i];
79 }
80 }
81 }
82  
83 =head2 addLength_andPad(array=>\@array, size=>$packetSize)
84  
85 This function takes an array of strings and appends the total length of all the items in the array to the end of the array, padding with specified characters to make the last item in teh array the same length as the rest of the items.
86 'array' is a reference to the array to be processed. This is processed inline, the array you pass will be changed when the function returns. The array must conform to a few guidelines to work with this function. All elements other than the last must be the same length. The last element must be less than or equal to the size of the rest. The elements must be at least 4 bytes large.
87 'size' is the packet size in bytes(ie:characters). this must be greater than 4
88 'padding' is a string containing the padding to be used. The first character of this string will be used to fill all places needing padding. If this parameter is not passed, each padding byte will be a random character(0x00..0xFF)
89  
90 =cut
91  
92 1 ▶sub addLength_andPad {
Subroutine addLength_andPad: called CC=6 SCAR=19.5
Pod documented
93 1 my %params=@_;
94 #####
95 #the array must have fixed length elements and all must be that fixed size except the final element(it can be any size less than or equal to the rest of the array
96 #you must pass size to the func, this is the max size of elements in array; ie: packet size(should be in bytes, not bits)
97 #does not work with size less than 4
98 #if padding is undefined will use random bytes, else use the first char of what is passed to padding
99 #need to have passed array reference, \@array
100 #how to address this?
101 #####
102 1 my $padding;
103 1 ▶ if (!defined $params{padding}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $params{'padding'}) { } 0 1 1
104 0 $padding = 'pack("C",makerandom(Size=>16) & 0xff)';
105 } else {
106 1 $padding = 'substr($params{padding},0,1)';
107 }
108 1 my $lastPacketSize = length $params{array}->[$#{$params{array}}];
109 1 my $plaintextLength = $params{size} * (scalar @{$params{array}} - 1) + $lastPacketSize;
110 1 ▶ if ((($lastPacketSize) + 4) > $params{size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastPacketSize + 4 > $params{'size'}) { } 1 0 1
111 1 ▶ for (my $i=0; $i<($params{size} - $lastPacketSize); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $params{"size"} - $lastPacketSize) 2 1 3
112 2 $params{array}->[$#{$params{array}}] .= eval $padding;
113 }
114 1 my $tmp_item='';
115 1 ▶ for (my $i=0;$i<($params{size} - 4); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $params{"size"} - 4) 3 1 4
116 3 $tmp_item .= eval $padding;
117 }
118 1 push @{$params{array}}, $tmp_item . pack("L",$plaintextLength);
119 } else {
120 0 my $tmp_item='';
121 0 ▶ for (my $i=0; $i < ($params{size} - (($lastPacketSize) + 4)); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $params{"size"} - ($lastPacketSize + 4)) 0 0 0
122 0 $tmp_item .= eval $padding;
123 }
124 0 $params{array}->[$#{$params{array}}] .= $tmp_item . pack("L",$plaintextLength);
125 }
126 }
127  
128 =head2 @brokenString = breakString(string=>$string, size=>$size)
129  
130 This function breaks up a string into an array with each item in the array of length given. This does no padding, so the last item in the array may be shorter. Please see addLength_andPad.
131 'size' is the number of bytes, or characters each segment should be
132 'string' is the string to be broken
133  
134 =cut
135  
136 1 ▶sub breakString {
Subroutine breakString: called CC=2 SCAR=6.9
Pod documented
137 1 my %params=@_;
138 1 my @brokenString=();
139 1 ▶ while (length $params{string} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $params{"string"} > 0) 4 1 5
140 4 push @brokenString, substr $params{string}, 0, $params{size};
141 4 $params{string} = substr $params{string}, length $brokenString[$#brokenString];
142 }
143 1 return @brokenString;
144 }
145  
146 =head2 $randomValue = randomValue(size=>$size,return=>$type)
147  
148 'size' is the number of bits to be in the created randomValue. it can be any value greater than 0, 128 is the default if nothing is passed
149 'return' can currently be 'ascii', 'hex', 'base64', or 'int'. If ascii, the size of the returned value will be exactly that passed, if hex, it will be twice that passed, if base64, 4/3 that passed, if int, the bitsize will be that passed, or possibly a few bits smaller.
150  
151 =cut
152  
153 7 ▶sub randomValue {
Subroutine randomValue: called CC=14 SCAR=27.1
Pod documented
154 7 my %params = @_;
155 #Defaults bits to 128 and return to ASCII
156 7 ▶ if (defined $params{size} && $params{size} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"size"} and $params{"size"} < 0) 0 7 7
Condition66%2/3
defined $params{"size"} and $params{"size"} < 0
!ll&&!rl&&r
160
MC/DC0%0/2
defined $params{"size"} and $params{"size"} < 0
defined $params{"size"} $params{"size"} < 0
Truth table66%2/3
A and B
A= defined $params{"size"}B= $params{"size"} < 0
ABresult
0X0
100
111
157 0 carp "Invalid size, setting to default";
158 0 delete $params{size};
159 }
160 7 ▶ if (defined $params{return} && $params{return} ne 'hex' && $params{return} ne 'ascii' && $params{return} ne 'base64' && $params{return} ne 'int') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"return"} and $params{"return"} ne "hex" and $params{"return"} ne "ascii" and $params{"return"} ne "base64" and $params{"return"} ne "int") 1 6 7
Condition91%11/12
defined $params{"return"} and $params{"return"} ne "hex" and $params{"return"} ne "ascii" and $params{"return"} ne "base64" and $params{"return"} ne "int"
!ll&&!rl&&r
601
defined $params{"return"} and $params{"return"} ne "hex" and $params{"return"} ne "ascii" and $params{"return"} ne "base64"
!ll&&!rl&&r
511
defined $params{"return"} and $params{"return"} ne "hex" and $params{"return"} ne "ascii"
!ll&&!rl&&r
322
defined $params{"return"} and $params{"return"} ne "hex"
!ll&&!rl&&r
124
MC/DC80%4/5
defined $params{"return"} and $params{"return"} ne "hex" and $params{"return"} ne "ascii" and $params{"return"} ne "base64" and $params{"return"} ne "int"
defined $params{"return"} $params{"return"} ne "hex" $params{"return"} ne "ascii" $params{"return"} ne "base64" $params{"return"} ne "int"
Truth table83%5/6
A and B and C and D and E
A= defined $params{"return"}B= $params{"return"} ne "hex"C= $params{"return"} ne "ascii"D= $params{"return"} ne "base64"E= $params{"return"} ne "int"
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
161 1 carp "Invalid return type, setting to default";
162 1 delete $params{return};
163 }
164 7 ▶ if (!defined $params{size}) { $params{size} = 128 };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"size"}) 1 6 7
165 7 ▶ if (!defined $params{return}) { $params{return} = 'ascii' };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"return"}) 2 5 7
166 7 my $key = makerandom(Size=>$params{size});
167 7 ▶ if ($params{return} eq 'ascii') {
Branch: 4 branches
BranchTrueFalseTotal
if ($params{'return'} eq 'ascii') { } 4 3 7
elsif ($params{'return'} eq 'hex') { } 2 1 3
elsif ($params{'return'} eq 'base64') { } 1 0 1
elsif ($params{'return'} eq 'int') { } 0 0 0
168 4 my $processedKey=largeNumToChar(size=>$params{size}, number=>$key);
169 4 return $processedKey;
170 } elsif ($params{return} eq 'hex') {
171 2 my $processedKey=largeNumToChar(size=>$params{size}, number=>$key);
172 2 my $processedKeyHex = unpack( "H*", $processedKey );
173 2 return $processedKeyHex;
174 } elsif ($params{return} eq 'base64') {
175 1 my $processedKey=largeNumToChar(size=>$params{size}, number=>$key);
176 1 my $processedKeyBase64 = MIME::Base64::encode($processedKey,'');
177 1 return $processedKeyBase64;
178 } elsif ($params{return} eq 'int') {
179 0 return $key;
180 }
181 }
182  
183 =head2 largeNumToChar((size=>$size, number=>$number)
184  
185 This function takes a large number and converts it to a character string which it returns.
186 'size' is the number of bits the final character string should have.
187 'number' is the number to convert
188  
189 =cut
190  
191 7 ▶sub largeNumToChar {
Subroutine largeNumToChar: called CC=5 SCAR=18.3
Pod documented
192 7 my %params = @_;
193 7 ▶ if (defined $params{size} && $params{size} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"size"} and $params{"size"} < 0) 0 7 7
Condition33%1/3
defined $params{"size"} and $params{"size"} < 0
!ll&&!rl&&r
070
MC/DC0%0/2
defined $params{"size"} and $params{"size"} < 0
defined $params{"size"} $params{"size"} < 0
Truth table33%1/3
A and B
A= defined $params{"size"}B= $params{"size"} < 0
ABresult
0X0
100
111
194 0 carp "Invalid size, setting to default";
195 0 delete $params{size};
196 }
197 0 ▶ if (!defined $params{size}) { $params{size} = 128 };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"size"}) 0 7 7
198 7 my $charString='';
199 7 ▶ for (my $i=$params{size}-8; $i>=0; $i-=8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 288 7 295
200 288 $charString.= pack( "C", (($params{number} >> $i) & 0xff) );
201 }
202 7 return $charString;
203 }
204  
205 =head1 AUTHOR
206  
207 Timothy Zander, C<< <timothy.zander at alum.rpi.edu> >>
208  
209 =head1 BUGS
210  
211 Please report any bugs or feature requests to
212 C<bug-crypt-aon-util at rt.cpan.org>, or through the web interface at
213 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Crypt-AllOrNothing-Util>.
214 I will be notified, and then you'll automatically be notified of progress on
215 your bug as I make changes.
216  
217 =head1 SUPPORT
218  
219 You can find documentation for this module with the perldoc command.
220  
221 perldoc Crypt::AllOrNothing::Util
222 perldoc Crypt::AllOrNothing
223  
224 You can also look for information at:
225  
226 =over 4
227  
228 =item * AnnoCPAN: Annotated CPAN documentation
229  
230 L<http://annocpan.org/dist/Crypt-AllOrNothing-Util>
231  
232 =item * CPAN Ratings
233  
234 L<http://cpanratings.perl.org/d/Crypt-AllOrNothing-Util>
235  
236 =item * RT: CPAN's request tracker
237  
238 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Crypt-AllOrNothing-Util>
239  
240 =item * Search CPAN
241  
242 L<http://search.cpan.org/dist/Crypt-AllOrNothing-Util>
243  
244 =back
245  
246 =head1 ACKNOWLEDGEMENTS
247  
248 Thanks to Prof. Bulent Yener at RPI for his assistance.
249  
250 =head1 COPYRIGHT & LICENSE
251  
252 Copyright 2007 Timothy Zander, all rights reserved.
253  
254 This program is free software; you can redistribute it and/or modify it
255 under the same terms as Perl itself.
256  
257 =cut
258  
259 3 1; # End of Crypt::AllOrNothing::Util
↑ Summary