line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::AllOrNothing::Util; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
44775
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
51
|
|
4
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
5
|
2
|
|
|
2
|
|
9
|
use Carp; |
|
2
|
|
|
|
|
12
|
|
|
2
|
|
|
|
|
192
|
|
6
|
2
|
|
|
2
|
|
630
|
use Crypt::Random qw/ makerandom /; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use MIME::Base64 (); |
8
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
43
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
44
|
|
|
|
|
|
|
randomValue |
45
|
|
|
|
|
|
|
breakString |
46
|
|
|
|
|
|
|
addLength_andPad |
47
|
|
|
|
|
|
|
remLength_andPad |
48
|
|
|
|
|
|
|
largeNumToChar |
49
|
|
|
|
|
|
|
) ] ); |
50
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub remLength_andPad { |
65
|
|
|
|
|
|
|
my %params=@_; |
66
|
|
|
|
|
|
|
###### |
67
|
|
|
|
|
|
|
#takes an array, sent as array, passed as reference |
68
|
|
|
|
|
|
|
###### |
69
|
|
|
|
|
|
|
my $length = unpack("L",substr($params{array}->[$#{$params{array}}], (length $params{array}->[$#{$params{array}}]) - 4 )); |
70
|
|
|
|
|
|
|
my $runningLength=0; |
71
|
|
|
|
|
|
|
for (my $i=0; $i < scalar @{$params{array}}; $i++) { |
72
|
|
|
|
|
|
|
if ($runningLength == $length) { |
73
|
|
|
|
|
|
|
pop @{$params{array}}; |
74
|
|
|
|
|
|
|
} elsif ((length $params{array}->[$i]) + $runningLength > $length) { |
75
|
|
|
|
|
|
|
substr($params{array}->[$i],$length-$runningLength) = ''; |
76
|
|
|
|
|
|
|
$runningLength = $length; |
77
|
|
|
|
|
|
|
} else { |
78
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
sub addLength_andPad { |
93
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $padding; |
103
|
|
|
|
|
|
|
if (!defined $params{padding}) { |
104
|
|
|
|
|
|
|
$padding = 'pack("C",makerandom(Size=>16) & 0xff)'; |
105
|
|
|
|
|
|
|
} else { |
106
|
|
|
|
|
|
|
$padding = 'substr($params{padding},0,1)'; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
my $lastPacketSize = length $params{array}->[$#{$params{array}}]; |
109
|
|
|
|
|
|
|
my $plaintextLength = $params{size} * (scalar @{$params{array}} - 1) + $lastPacketSize; |
110
|
|
|
|
|
|
|
if ((($lastPacketSize) + 4) > $params{size}) { |
111
|
|
|
|
|
|
|
for (my $i=0; $i<($params{size} - $lastPacketSize); $i++) { |
112
|
|
|
|
|
|
|
$params{array}->[$#{$params{array}}] .= eval $padding; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
my $tmp_item=''; |
115
|
|
|
|
|
|
|
for (my $i=0;$i<($params{size} - 4); $i++) { |
116
|
|
|
|
|
|
|
$tmp_item .= eval $padding; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
push @{$params{array}}, $tmp_item . pack("L",$plaintextLength); |
119
|
|
|
|
|
|
|
} else { |
120
|
|
|
|
|
|
|
my $tmp_item=''; |
121
|
|
|
|
|
|
|
for (my $i=0; $i < ($params{size} - (($lastPacketSize) + 4)); $i++) { |
122
|
|
|
|
|
|
|
$tmp_item .= eval $padding; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
sub breakString { |
137
|
|
|
|
|
|
|
my %params=@_; |
138
|
|
|
|
|
|
|
my @brokenString=(); |
139
|
|
|
|
|
|
|
while (length $params{string} > 0) { |
140
|
|
|
|
|
|
|
push @brokenString, substr $params{string}, 0, $params{size}; |
141
|
|
|
|
|
|
|
$params{string} = substr $params{string}, length $brokenString[$#brokenString]; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub randomValue { |
154
|
|
|
|
|
|
|
my %params = @_; |
155
|
|
|
|
|
|
|
#Defaults bits to 128 and return to ASCII |
156
|
|
|
|
|
|
|
if (defined $params{size} && $params{size} < 0) { |
157
|
|
|
|
|
|
|
carp "Invalid size, setting to default"; |
158
|
|
|
|
|
|
|
delete $params{size}; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
if (defined $params{return} && $params{return} ne 'hex' && $params{return} ne 'ascii' && $params{return} ne 'base64' && $params{return} ne 'int') { |
161
|
|
|
|
|
|
|
carp "Invalid return type, setting to default"; |
162
|
|
|
|
|
|
|
delete $params{return}; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
if (!defined $params{size}) { $params{size} = 128 }; |
165
|
|
|
|
|
|
|
if (!defined $params{return}) { $params{return} = 'ascii' }; |
166
|
|
|
|
|
|
|
my $key = makerandom(Size=>$params{size}); |
167
|
|
|
|
|
|
|
if ($params{return} eq 'ascii') { |
168
|
|
|
|
|
|
|
my $processedKey=largeNumToChar(size=>$params{size}, number=>$key); |
169
|
|
|
|
|
|
|
return $processedKey; |
170
|
|
|
|
|
|
|
} elsif ($params{return} eq 'hex') { |
171
|
|
|
|
|
|
|
my $processedKey=largeNumToChar(size=>$params{size}, number=>$key); |
172
|
|
|
|
|
|
|
my $processedKeyHex = unpack( "H*", $processedKey ); |
173
|
|
|
|
|
|
|
return $processedKeyHex; |
174
|
|
|
|
|
|
|
} elsif ($params{return} eq 'base64') { |
175
|
|
|
|
|
|
|
my $processedKey=largeNumToChar(size=>$params{size}, number=>$key); |
176
|
|
|
|
|
|
|
my $processedKeyBase64 = MIME::Base64::encode($processedKey,''); |
177
|
|
|
|
|
|
|
return $processedKeyBase64; |
178
|
|
|
|
|
|
|
} elsif ($params{return} eq 'int') { |
179
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub largeNumToChar { |
192
|
|
|
|
|
|
|
my %params = @_; |
193
|
|
|
|
|
|
|
if (defined $params{size} && $params{size} < 0) { |
194
|
|
|
|
|
|
|
carp "Invalid size, setting to default"; |
195
|
|
|
|
|
|
|
delete $params{size}; |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
if (!defined $params{size}) { $params{size} = 128 }; |
198
|
|
|
|
|
|
|
my $charString=''; |
199
|
|
|
|
|
|
|
for (my $i=$params{size}-8; $i>=0; $i-=8) { |
200
|
|
|
|
|
|
|
$charString.= pack( "C", (($params{number} >> $i) & 0xff) ); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
return $charString; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head1 AUTHOR |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Timothy Zander, C<< >> |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head1 BUGS |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
212
|
|
|
|
|
|
|
C, or through the web interface at |
213
|
|
|
|
|
|
|
L. |
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 |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=item * CPAN Ratings |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
L |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
L |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=item * Search CPAN |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
L |
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
|
|
|
|
|
|
|
1; # End of Crypt::AllOrNothing::Util |