1N/A/* $Id: MD5.xs,v 1.42 2003/12/06 22:35:16 gisle Exp $ */ 1N/A * This library is free software; you can redistribute it and/or 1N/A * modify it under the same terms as Perl itself. 1N/A * Copyright 1998-2000 Gisle Aas. 1N/A * Copyright 1995-1996 Neil Winton. 1N/A * Copyright 1991-1992 RSA Data Security, Inc. 1N/A * This code is derived from Neil Winton's MD5-1.7 Perl module, which in 1N/A * turn is derived from the reference implementation in RFC 1321 which 1N/A * comes with this message: 1N/A * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 1N/A * License to copy and use this software is granted provided that it 1N/A * is identified as the "RSA Data Security, Inc. MD5 Message-Digest 1N/A * Algorithm" in all material mentioning or referencing this software 1N/A * License is also granted to make and use derivative works provided 1N/A * that such works are identified as "derived from the RSA Data 1N/A * Security, Inc. MD5 Message-Digest Algorithm" in all material 1N/A * mentioning or referencing the derived work. 1N/A * RSA Data Security, Inc. makes no representations concerning either 1N/A * the merchantability of this software or the suitability of this 1N/A * software for any particular purpose. It is provided "as is" 1N/A * without express or implied warranty of any kind. 1N/A * These notices must be retained in any copies of any part of this 1N/A /* SvPVbyte does not work in perl-5.6.1, borrowed version for 5.7.3 */ 1N/A/* Perl does not guarantee that U32 is exactly 32 bits. Some system 1N/A * has no integral type with exactly 32 bits. For instance, A Cray has 1N/A * short, int and long all at 64 bits so we need to apply this macro 1N/A * to reduce U32 values to 32 bits at appropriate places. If U32 1N/A * really does have 32 bits then this is a no-op. 1N/A/* The MD5 algorithm is defined in terms of little endian 32-bit 1N/A * values. The following macros (and functions) allow us to convert 1N/A * between native integers and such values. 1N/A |(((x)&
0x0000FF00)<<
8) \
1N/A |(((x)&
0x00FF0000)>>
8) )
1N/A *s++ = (
U8)((u >>
8) &
0xFF);
1N/A *s++ = (
U8)((u >>
16) &
0xFF);
1N/A *s = (
U8)((u >>
24) &
0xFF);
1N/A/* This stucture keeps the current state of algorithm. 1N/A U32 A, B, C, D;
/* current digest */ 1N/A/* Padding is added at the end of the message in order to fill a 1N/A * complete 64 byte block (- 8 bytes for the message length). The 1N/A * padding is also the reason the buffer in MD5_CTX have to be 1N/A 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1N/A 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1N/A 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1N/A/* Constants for MD5Transform routine. 1N/A/* F, G, H and I are basic MD5 functions. 1N/A#
define F(x, y, z) ((((x) & ((y) ^ (z))) ^ (z)))
1N/A#
define G(x, y, z) F(z, x, y)
1N/A#
define H(x, y, z) ((x) ^ (y) ^ (z))
1N/A#
define I(x, y, z) ((y) ^ ((x) | (~z)))
1N/A/* ROTATE_LEFT rotates x left n bits. 1N/A/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 1N/A * Rotation is separate from addition to prevent recomputation. 1N/A (a) += G ((b), (c), (d)) + X[x] + (
U32)(
ac); \
1N/A (a) += H ((b), (c), (d)) + X[x] + (
U32)(
ac); \
1N/A (a) += I ((b), (c), (d)) + X[x] + (
U32)(
ac); \
1N/A /* message length */ 1N/A U32 X[
16];
/* converted values, used in round 2-4 */ 1N/A for (i = 0; i <
16; i++) {
1N/A FF (a, b, c, d,
S11,
0xd76aa478);
/* 1 */ 1N/A FF (d, a, b, c,
S12,
0xe8c7b756);
/* 2 */ 1N/A FF (c, d, a, b,
S13,
0x242070db);
/* 3 */ 1N/A FF (b, c, d, a,
S14,
0xc1bdceee);
/* 4 */ 1N/A FF (a, b, c, d,
S11,
0xf57c0faf);
/* 5 */ 1N/A FF (d, a, b, c,
S12,
0x4787c62a);
/* 6 */ 1N/A FF (c, d, a, b,
S13,
0xa8304613);
/* 7 */ 1N/A FF (b, c, d, a,
S14,
0xfd469501);
/* 8 */ 1N/A FF (a, b, c, d,
S11,
0x698098d8);
/* 9 */ 1N/A FF (d, a, b, c,
S12,
0x8b44f7af);
/* 10 */ 1N/A FF (c, d, a, b,
S13,
0xffff5bb1);
/* 11 */ 1N/A FF (b, c, d, a,
S14,
0x895cd7be);
/* 12 */ 1N/A FF (a, b, c, d,
S11,
0x6b901122);
/* 13 */ 1N/A FF (d, a, b, c,
S12,
0xfd987193);
/* 14 */ 1N/A FF (c, d, a, b,
S13,
0xa679438e);
/* 15 */ 1N/A FF (b, c, d, a,
S14,
0x49b40821);
/* 16 */ 1N/A GG (a, b, c, d,
1,
S21,
0xf61e2562);
/* 17 */ 1N/A GG (d, a, b, c,
6,
S22,
0xc040b340);
/* 18 */ 1N/A GG (c, d, a, b,
11,
S23,
0x265e5a51);
/* 19 */ 1N/A GG (b, c, d, a, 0,
S24,
0xe9b6c7aa);
/* 20 */ 1N/A GG (a, b, c, d,
5,
S21,
0xd62f105d);
/* 21 */ 1N/A GG (d, a, b, c,
10,
S22,
0x2441453);
/* 22 */ 1N/A GG (c, d, a, b,
15,
S23,
0xd8a1e681);
/* 23 */ 1N/A GG (b, c, d, a,
4,
S24,
0xe7d3fbc8);
/* 24 */ 1N/A GG (a, b, c, d,
9,
S21,
0x21e1cde6);
/* 25 */ 1N/A GG (d, a, b, c,
14,
S22,
0xc33707d6);
/* 26 */ 1N/A GG (c, d, a, b,
3,
S23,
0xf4d50d87);
/* 27 */ 1N/A GG (b, c, d, a,
8,
S24,
0x455a14ed);
/* 28 */ 1N/A GG (a, b, c, d,
13,
S21,
0xa9e3e905);
/* 29 */ 1N/A GG (d, a, b, c,
2,
S22,
0xfcefa3f8);
/* 30 */ 1N/A GG (c, d, a, b,
7,
S23,
0x676f02d9);
/* 31 */ 1N/A GG (b, c, d, a,
12,
S24,
0x8d2a4c8a);
/* 32 */ 1N/A HH (a, b, c, d,
5,
S31,
0xfffa3942);
/* 33 */ 1N/A HH (d, a, b, c,
8,
S32,
0x8771f681);
/* 34 */ 1N/A HH (c, d, a, b,
11,
S33,
0x6d9d6122);
/* 35 */ 1N/A HH (b, c, d, a,
14,
S34,
0xfde5380c);
/* 36 */ 1N/A HH (a, b, c, d,
1,
S31,
0xa4beea44);
/* 37 */ 1N/A HH (d, a, b, c,
4,
S32,
0x4bdecfa9);
/* 38 */ 1N/A HH (c, d, a, b,
7,
S33,
0xf6bb4b60);
/* 39 */ 1N/A HH (b, c, d, a,
10,
S34,
0xbebfbc70);
/* 40 */ 1N/A HH (a, b, c, d,
13,
S31,
0x289b7ec6);
/* 41 */ 1N/A HH (d, a, b, c, 0,
S32,
0xeaa127fa);
/* 42 */ 1N/A HH (c, d, a, b,
3,
S33,
0xd4ef3085);
/* 43 */ 1N/A HH (b, c, d, a,
6,
S34,
0x4881d05);
/* 44 */ 1N/A HH (a, b, c, d,
9,
S31,
0xd9d4d039);
/* 45 */ 1N/A HH (d, a, b, c,
12,
S32,
0xe6db99e5);
/* 46 */ 1N/A HH (c, d, a, b,
15,
S33,
0x1fa27cf8);
/* 47 */ 1N/A HH (b, c, d, a,
2,
S34,
0xc4ac5665);
/* 48 */ 1N/A II (a, b, c, d, 0,
S41,
0xf4292244);
/* 49 */ 1N/A II (d, a, b, c,
7,
S42,
0x432aff97);
/* 50 */ 1N/A II (c, d, a, b,
14,
S43,
0xab9423a7);
/* 51 */ 1N/A II (b, c, d, a,
5,
S44,
0xfc93a039);
/* 52 */ 1N/A II (a, b, c, d,
12,
S41,
0x655b59c3);
/* 53 */ 1N/A II (d, a, b, c,
3,
S42,
0x8f0ccc92);
/* 54 */ 1N/A II (c, d, a, b,
10,
S43,
0xffeff47d);
/* 55 */ 1N/A II (b, c, d, a,
1,
S44,
0x85845dd1);
/* 56 */ 1N/A II (a, b, c, d,
8,
S41,
0x6fa87e4f);
/* 57 */ 1N/A II (d, a, b, c,
15,
S42,
0xfe2ce6e0);
/* 58 */ 1N/A II (c, d, a, b,
6,
S43,
0xa3014314);
/* 59 */ 1N/A II (b, c, d, a,
13,
S44,
0x4e0811a1);
/* 60 */ 1N/A II (a, b, c, d,
4,
S41,
0xf7537e82);
/* 61 */ 1N/A II (d, a, b, c,
11,
S42,
0xbd3af235);
/* 62 */ 1N/A II (c, d, a, b,
2,
S43,
0x2ad7d2bb);
/* 63 */ 1N/A II (b, c, d, a,
9,
S44,
0xeb86d391);
/* 64 */ 1N/A croak(
"Not a reference to a Digest::MD5 object");
1N/A return (
MD5_CTX*)0;
/* some compilers insist on a return value */ 1N/A "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1N/A/********************************************************************/ 1N/A /* The MD5Update() function is faster if it can work with 1N/A * complete blocks. This will fill up any buffered block 1N/A /* Process blocks until EOF or error */ 1N/A croak(
"Reading from filehandle failed");
1N/A msg =
"probably called as method";
1N/A msg =
"called with reference argument";
1N/A msg =
"probably called as class method";