Debian Codegraph @

src/.svn/text-base/mdx.pl.svn-base

raw
1 #BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net)
2 #BSD All rights reserved.
3 #BSD
4 #BSD Redistribution and use in source and binary forms, with or without
5 #BSD modification, are permitted provided that the following conditions
6 #BSD are met:
7 #BSD
8 #BSD - Redistributions of source code must retain the above copyright
9 #BSD notice, this list of conditions and the following disclaimer.
10 #BSD
11 #BSD - Redistributions in binary form must reproduce the above copyright
12 #BSD notice, this list of conditions and the following disclaimer in the
13 #BSD documentation and/or other materials provided with the distribution.
14 #BSD
15 #BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 #BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 #BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 #BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 #BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 #BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 #BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 #BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 #BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 #BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 #BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 #BSD POSSIBILITY OF SUCH DAMAGE.
27
28 ########################################################################
29
30 { # start md5 packaged varbs
31 my ( @S, @T, @M );
32 my $code = '';
33 my $MD5_TRYLOADING = 1;
34
35 =item B<md5>
36
37 Params: $data
38
39 Return: $hex_md5_string
40
41 This function takes a data scalar, and composes a MD5 hash of it, and
42 returns it in a hex ascii string. It will use the fastest MD5 function
43 available.
44
45 =cut
46
47 sub md5 {
48 return undef if ( !defined $_[0] ); # oops, forgot the data
49 if ($MD5_TRYLOADING) {
50 $MD5_TRYLOADING = 0;
51 eval "require MD5";
52 }
53 return MD5->hexhash( $_[0] ) if ($MD5::VERSION);
54 my $DATA = _md5_pad( $_[0] );
55 &_md5_init() if ( !defined $M[0] );
56 return _md5_perl_generated( \$DATA );
57 }
58
59 ########################################################################
60
61 sub _md5_init {
62 return if ( defined $S[0] );
63 my $i;
64 for ( $i = 1 ; $i <= 64 ; $i++ ) {
65 $T[ $i - 1 ] = int( ( 2**32 ) * abs( sin($i) ) );
66 }
67 my @t = ( 7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21 );
68 for ( $i = 0 ; $i < 64 ; $i++ ) {
69 $S[$i] = $t[ ( int( $i / 16 ) * 4 ) + ( $i % 4 ) ];
70 }
71 @M = (
72 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
73 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12,
74 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2,
75 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9
76 );
77 &_md5_generate();
78
79 # check to see if it works correctly
80 my $TEST = _md5_pad('foobar');
81 if ( _md5_perl_generated( \$TEST ) ne
82 '3858f62230ac3c915f300c664312c63f' )
83 {
84 utils_carp('md5: MD5 self-test not successful.');
85 }
86 }
87
88 ########################################################################
89
90 # This function is from Digest::Perl::MD5, and bears the following
91 # copyrights:
92 #
93 # Copyright 2000 Christian Lackas, Imperia Software Solutions
94 # Copyright 1998-1999 Gisle Aas.
95 # Copyright 1995-1996 Neil Winton.
96 # Copyright 1991-1992 RSA Data Security, Inc.
97 #
98
99 sub _md5_pad {
100 my $l = length( my $msg = shift() . chr(128) );
101 $msg .= "\0" x ( ( $l % 64 <= 56 ? 56 : 120 ) - $l % 64 );
102 $l = ( $l - 1 ) * 8;
103 $msg .= pack 'VV', $l & 0xffffffff, ( $l >> 16 >> 16 );
104 return $msg;
105 }
106
107 ########################################################################
108
109 sub _md5_generate {
110 my $N = 'abcddabccdabbcda';
111 my ( $i, $M ) = ( 0, '' );
112 $M = '&0xffffffff' if ( ( 1 << 16 ) << 16 ); # mask for 64bit systems
113
114 $code = <<EOT;
115 sub _md5_perl_generated {
116 BEGIN { \$^H |= 1; }; # use integer
117 my (\$A,\$B,\$C,\$D)=(0x67452301,0xefcdab89,0x98badcfe,0x10325476);
118 my (\$a,\$b,\$c,\$d,\$t,\$i);
119 my \$dr=shift;
120 my \$l=length(\$\$dr);
121 for my \$L (0 .. ((\$l/64)-1) ) {
122 my \@D = unpack('V16', substr(\$\$dr, \$L*64,64));
123 (\$a,\$b,\$c,\$d)=(\$A,\$B,\$C,\$D);
124 EOT
125
126 for ( $i = 0 ; $i < 16 ; $i++ ) {
127 my ( $a, $b, $c, $d ) =
128 split( '', substr( $N, ( $i % 4 ) * 4, 4 ) );
129 $code .=
130 "\$t=((\$$d^(\$$b\&(\$$c^\$$d)))+\$$a+\$D[$M[$i]]+$T[$i])$M;\n";
131 $code .=
132 "\$$a=(((\$t<<$S[$i])|((\$t>>(32-$S[$i]))&((1<<$S[$i])-1)))+\$$b)$M;\n";
133 }
134 for ( ; $i < 32 ; $i++ ) {
135 my ( $a, $b, $c, $d ) =
136 split( '', substr( $N, ( $i % 4 ) * 4, 4 ) );
137 $code .=
138 "\$t=((\$$c^(\$$d\&(\$$b^\$$c)))+\$$a+\$D[$M[$i]]+$T[$i])$M;\n";
139 $code .=
140 "\$$a=(((\$t<<$S[$i])|((\$t>>(32-$S[$i]))&((1<<$S[$i])-1)))+\$$b)$M;\n";
141 }
142 for ( ; $i < 48 ; $i++ ) {
143 my ( $a, $b, $c, $d ) =
144 split( '', substr( $N, ( $i % 4 ) * 4, 4 ) );
145 $code .= "\$t=((\$$b^\$$c^\$$d)+\$$a+\$D[$M[$i]]+$T[$i])$M;\n";
146 $code .=
147 "\$$a=(((\$t<<$S[$i])|((\$t>>(32-$S[$i]))&((1<<$S[$i])-1)))+\$$b)$M;\n";
148 }
149 for ( ; $i < 64 ; $i++ ) {
150 my ( $a, $b, $c, $d ) =
151 split( '', substr( $N, ( $i % 4 ) * 4, 4 ) );
152 $code .= "\$t=((\$$c^(\$$b|(~\$$d)))+\$$a+\$D[$M[$i]]+$T[$i])$M;\n";
153 $code .=
154 "\$$a=(((\$t<<$S[$i])|((\$t>>(32-$S[$i]))&((1<<$S[$i])-1)))+\$$b)$M;\n";
155 }
156
157 $code .= <<EOT;
158 \$A=\$A+\$a\&0xffffffff; \$B=\$B+\$b\&0xffffffff;
159 \$C=\$C+\$c\&0xffffffff; \$D=\$D+\$d\&0xffffffff;
160 } # for
161 return unpack('H*', pack('V4',\$A,\$B,\$C,\$D)); }
162 EOT
163 eval "$code";
164 }
165
166 } # md5 package container
167
168 ########################################################################
169
170 { # start md4 packaged varbs
171 my ( @S, @T, @M );
172 my $code = '';
173
174 =item B<md4>
175
176 Params: $data
177
178 Return: $hex_md4_string
179
180 This function takes a data scalar, and composes a MD4 hash of it, and
181 returns it in a hex ascii string. It will use the fastest MD4 function
182 available.
183
184 =cut
185
186 sub md4 {
187 return undef if ( !defined $_[0] ); # oops, forgot the data
188 my $DATA = _md5_pad( $_[0] );
189 &_md4_init() if ( !defined $M[0] );
190 return _md4_perl_generated( \$DATA );
191 }
192
193 ########################################################################
194
195 sub _md4_init {
196 return if ( defined $S[0] );
197 my $i;
198 my @t = ( 3, 7, 11, 19, 3, 5, 9, 13, 3, 9, 11, 15 );
199 for ( $i = 0 ; $i < 48 ; $i++ ) {
200 $S[$i] = $t[ ( int( $i / 16 ) * 4 ) + ( $i % 4 ) ];
201 }
202 @M = (
203 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
204 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15,
205 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
206 );
207
208 my $N = 'abcddabccdabbcda';
209 my $M = '';
210 $M = '&0xffffffff' if ( ( 1 << 16 ) << 16 ); # mask for 64bit systems
211
212 $code = <<EOT;
213 sub _md4_perl_generated {
214 BEGIN { \$^H |= 1; }; # use integer
215 my (\$A,\$B,\$C,\$D)=(0x67452301,0xefcdab89,0x98badcfe,0x10325476);
216 my (\$a,\$b,\$c,\$d,\$t,\$i);
217 my \$dr=shift;
218 my \$l=length(\$\$dr);
219 for my \$L (0 .. ((\$l/64)-1) ) {
220 my \@D = unpack('V16', substr(\$\$dr, \$L*64,64));
221 (\$a,\$b,\$c,\$d)=(\$A,\$B,\$C,\$D);
222 EOT
223
224 for ( $i = 0 ; $i < 16 ; $i++ ) {
225 my ( $a, $b, $c, $d ) =
226 split( '', substr( $N, ( $i % 4 ) * 4, 4 ) );
227 $code .= "\$t=((\$$d^(\$$b\&(\$$c^\$$d)))+\$$a+\$D[$M[$i]])$M;\n";
228 $code .=
229 "\$$a=(((\$t<<$S[$i])|((\$t>>(32-$S[$i]))&((1<<$S[$i])-1))))$M;\n";
230 }
231 for ( ; $i < 32 ; $i++ ) {
232 my ( $a, $b, $c, $d ) =
233 split( '', substr( $N, ( $i % 4 ) * 4, 4 ) );
234 $code .=
235 "\$t=(( (\$$b&\$$c)|(\$$b&\$$d)|(\$$c&\$$d) )+\$$a+\$D[$M[$i]]+0x5a827999)$M;\n";
236 $code .=
237 "\$$a=(((\$t<<$S[$i])|((\$t>>(32-$S[$i]))&((1<<$S[$i])-1))))$M;\n";
238 }
239 for ( ; $i < 48 ; $i++ ) {
240 my ( $a, $b, $c, $d ) =
241 split( '', substr( $N, ( $i % 4 ) * 4, 4 ) );
242 $code .=
243 "\$t=(( \$$b^\$$c^\$$d )+\$$a+\$D[$M[$i]]+0x6ed9eba1)$M;\n";
244 $code .=
245 "\$$a=(((\$t<<$S[$i])|((\$t>>(32-$S[$i]))&((1<<$S[$i])-1))))$M;\n";
246 }
247
248 $code .= <<EOT;
249 \$A=\$A+\$a\&0xffffffff; \$B=\$B+\$b\&0xffffffff;
250 \$C=\$C+\$c\&0xffffffff; \$D=\$D+\$d\&0xffffffff;
251 } # for
252 return unpack('H*', pack('V4',\$A,\$B,\$C,\$D)); }
253 EOT
254 eval "$code";
255
256 my $TEST = _md5_pad('foobar');
257 if ( _md4_perl_generated( \$TEST ) ne
258 '547aefd231dcbaac398625718336f143' )
259 {
260 utils_carp('md4: MD4 self-test not successful.');
261 }
262 }
263
264 } # md4 package container
265