annotate src/md5.c @ 68:0b7f3b634cf2 r19-16-pre9

Import from CVS: tag r19-16-pre9
author cvs
date Mon, 13 Aug 2007 09:00:03 +0200
parents 56c54cf7c5b6
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 rights reserved.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 License to copy and use this software is granted provided that it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 Algorithm" in all material mentioning or referencing this software
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 or this function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 License is also granted to make and use derivative works provided
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 that such works are identified as "derived from the RSA Data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 Security, Inc. MD5 Message-Digest Algorithm" in all material
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 mentioning or referencing the derived work.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 RSA Data Security, Inc. makes no representations concerning either
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the merchantability of this software or the suitability of this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 software for any particular purpose. It is provided "as is"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 without express or implied warranty of any kind.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 These notices must be retained in any copies of any part of this
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 documentation and/or software.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 /* This file has been Mule-ized. See comment at Fmd5(), though. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 #include "buffer.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #include "insdel.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 typedef unsigned char *POINTER;/* POINTER defines a generic pointer type */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 typedef unsigned short int UINT2;/* UINT2 defines a two byte word */
48
56c54cf7c5b6 Import from CVS: tag r19-16b90
cvs
parents: 20
diff changeset
37 typedef unsigned int UINT4;/* UINT4 defines a four byte word */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 #define PROTO_LIST(list) list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 #define MD_CTX MD5_CTX
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #define MDInit MD5Init
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #define MDUpdate MD5Update
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #define MDFinal MD5Final
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 /* MD5 context. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 typedef struct {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 UINT4 state[4]; /* state (ABCD) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 unsigned char buffer[64]; /* input buffer */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 } MD5_CTX;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 void MD5Init PROTO_LIST ((MD5_CTX *));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 void MD5Update PROTO_LIST
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ((MD5_CTX *, CONST unsigned char *, unsigned int));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 /* Constants for MD5Transform routine.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 #define S11 7
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #define S12 12
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 #define S13 17
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 #define S14 22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 #define S21 5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 #define S22 9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 #define S23 14
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 #define S24 20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #define S31 4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 #define S32 11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 #define S33 16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #define S34 23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 #define S41 6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 #define S42 10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 #define S43 15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 #define S44 21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 static void MD5Transform PROTO_LIST ((UINT4 [4], CONST unsigned char [64]));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 static void Encode PROTO_LIST
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ((unsigned char *, UINT4 *, unsigned int));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 static void Decode PROTO_LIST
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ((UINT4 *, CONST unsigned char *, unsigned int));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 static void MD5_memcpy PROTO_LIST ((POINTER, CONST POINTER, unsigned int));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 static unsigned char PADDING[64] = {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 /* F, G, H and I are basic MD5 functions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 #define H(x, y, z) ((x) ^ (y) ^ (z))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 #define I(x, y, z) ((y) ^ ((x) | (~z)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 /* ROTATE_LEFT rotates x left n bits.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 Rotation is separate from addition to prevent recomputation.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 #define FF(a, b, c, d, x, s, ac) { \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (a) = ROTATE_LEFT ((a), (s)); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (a) += (b); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 #define GG(a, b, c, d, x, s, ac) { \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (a) = ROTATE_LEFT ((a), (s)); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (a) += (b); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 #define HH(a, b, c, d, x, s, ac) { \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (a) = ROTATE_LEFT ((a), (s)); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (a) += (b); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 #define II(a, b, c, d, x, s, ac) { \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (a) = ROTATE_LEFT ((a), (s)); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (a) += (b); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 /* MD5 initialization. Begins an MD5 operation, writing a new context.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 MD5Init (MD5_CTX *context)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 context->count[0] = context->count[1] = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 /* Load magic initialization constants. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 context->state[0] = 0x67452301;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 context->state[1] = 0xefcdab89;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 context->state[2] = 0x98badcfe;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 context->state[3] = 0x10325476;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 /* MD5 block update operation. Continues an MD5 message-digest
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 operation, processing another message block, and updating the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 context.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 MD5Update (MD5_CTX *context, CONST unsigned char *input, unsigned int inputLen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 unsigned int i, indecks, partLen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 /* Compute number of bytes mod 64 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 indecks = (unsigned int)((context->count[0] >> 3) & 0x3F);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 /* Update number of bits */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 if ((context->count[0] += ((UINT4)inputLen << 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 < ((UINT4)inputLen << 3))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 context->count[1]++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 context->count[1] += ((UINT4)inputLen >> 29);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 partLen = 64 - indecks;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 /* Transform as many times as possible. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 if (inputLen >= partLen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 MD5_memcpy ((POINTER)&context->buffer[indecks], (CONST POINTER)input,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 partLen);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 MD5Transform (context->state, context->buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 for (i = partLen; i + 63 < inputLen; i += 64)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 MD5Transform (context->state, &input[i]);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 indecks = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 i = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 /* Buffer remaining input */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 MD5_memcpy ((POINTER)&context->buffer[indecks], (CONST POINTER)&input[i],
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 inputLen-i);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 /* MD5 finalization. Ends an MD5 message-digest operation, writing the
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
180 message digest and zeroizing the context. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 MD5Final (unsigned char digest[16], MD5_CTX *context)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 unsigned char bits[8];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 unsigned int indecks, padLen;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 /* Save number of bits */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 Encode (bits, context->count, 8);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 /* Pad out to 56 mod 64.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 indecks = (unsigned int)((context->count[0] >> 3) & 0x3f);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 padLen = (indecks < 56) ? (56 - indecks) : (120 - indecks);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 MD5Update (context, PADDING, padLen);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 /* Append length (before padding) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 MD5Update (context, bits, 8);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 /* Store state in digest */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 Encode (digest, context->state, 16);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 /* Zeroize sensitive information.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 MD5_memset ((POINTER)context, 0, sizeof (*context));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 /* MD5 basic transformation. Transforms state based on block.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 MD5Transform (UINT4 state[4], CONST unsigned char block[64])
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 Decode (x, block, 64);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 /* Round 1 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 /* Round 2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 /* Round 3 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 /* Round 4 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 state[0] += a;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 state[1] += b;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 state[2] += c;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 state[3] += d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 /* Zeroize sensitive information.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 MD5_memset ((POINTER)x, 0, sizeof (x));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 /* Encodes input (UINT4) into output (unsigned char). Assumes len is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 a multiple of 4.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 Encode (unsigned char *output, UINT4 *input, unsigned int len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 unsigned int i, j;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 for (i = 0, j = 0; j < len; i++, j += 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 output[j] = (unsigned char)(input[i] & 0xff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 /* Decodes input (unsigned char) into output (UINT4). Assumes len is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 a multiple of 4.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 Decode (UINT4 *output, CONST unsigned char *input, unsigned int len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 unsigned int i, j;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 for (i = 0, j = 0; j < len; i++, j += 4)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 MD5_memcpy (POINTER output, CONST POINTER input, unsigned int len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 memcpy (output, input, len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 MD5_memset (POINTER output, int value, unsigned int len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 memset (output, value, len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 /* unused */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 #if 0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 LispMDString (char *string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 MD_CTX context;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 unsigned char digest[16];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 unsigned int len = strlen(string);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 MDInit (&context);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 MDUpdate (&context, string, len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 MDFinal (digest, &context);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 /* XEmacs interface code. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 Lisp_Object Qmd5;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 /* #### There could be potential problems here with Mule. I don't
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 know enough about the uses of MD5 to be able to tell for sure
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 whether this is a problem. The basic potential problem is that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 the hash value will be computed based on the internal representation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 of the buffer; this would likely cause problems if the string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 contains extended characters, because the extended characters
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 will get sent over the wire in an external form that is different
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 from their internal representation, and thus their MD5 hash would
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 be different. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
368 DEFUN ("md5", Fmd5, 1, 3, 0, /*
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 Return the MD5 (a secure message digest algorithm) of an object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 OBJECT is either a string or a buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 Optional arguments START and END denote buffer positions for computing the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 hash of a portion of OBJECT.
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
373 */
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
374 (object, start, end))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 MD_CTX context;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 unsigned char digest[16];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 unsigned char thehash[32];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 MDInit (&context);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 if (NILP (object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 MDUpdate (&context, (CONST unsigned char *) "", 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 else if (BUFFERP (object))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 struct buffer *b = decode_buffer (object, 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 Bufpos begv, endv;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 Lisp_Object string;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 /* Figure out where we need to get info from */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 get_buffer_range_char (b, start, end, &begv, &endv, GB_ALLOW_NIL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 /* Get the string data from the buffer */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 string = make_string_from_buffer (b, begv, endv - begv);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 /* Compute the digest */
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 2
diff changeset
400 MDUpdate (&context, (unsigned char *) XSTRING_DATA (string),
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 2
diff changeset
401 XSTRING_LENGTH (string));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 Bytecount len, bstart, bend;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 CHECK_STRING (object);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 get_string_range_byte (object, start, end, &bstart, &bend,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 GB_HISTORICAL_STRING_BEHAVIOR);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 len = bend - bstart;
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 2
diff changeset
410 MDUpdate (&context, ((unsigned char *) XSTRING_DATA (object)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 + bstart), len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 MDFinal (digest, &context);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 for (i = 0; i < 16; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 sprintf ((char *) (thehash + (i * 2)), "%02x", digest[i]);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 return (make_string (thehash, 32));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 syms_of_md5 (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 {
20
859a2309aef8 Import from CVS: tag r19-15b93
cvs
parents: 16
diff changeset
424 DEFSUBR (Fmd5);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 defsymbol (&Qmd5, "md5");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 vars_of_md5 (void)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 Fprovide (Qmd5);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 }