27 #include <sys/types.h>
54 static const unsigned char fillbuf[64] = { 0x80, 0 };
60 #define FF(b, c, d) (d ^ (b & (c ^ d)))
61 #define FG(b, c, d) FF (d, b, c)
62 #define FH(b, c, d) (b ^ c ^ d)
63 #define FI(b, c, d) (c ^ (b | ~d))
66 #define OPENOPTS(BINARY) "r"
86 if (ctx->
total[0] < len)
108 #define OP(a, b, c, d, s, T) \
111 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
120 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
129 OP(A, B, C, D, 7, 0xd76aa478);
130 OP(D, A, B, C, 12, 0xe8c7b756);
131 OP(C, D, A, B, 17, 0x242070db);
132 OP(B, C, D, A, 22, 0xc1bdceee);
133 OP(A, B, C, D, 7, 0xf57c0faf);
134 OP(D, A, B, C, 12, 0x4787c62a);
135 OP(C, D, A, B, 17, 0xa8304613);
136 OP(B, C, D, A, 22, 0xfd469501);
137 OP(A, B, C, D, 7, 0x698098d8);
138 OP(D, A, B, C, 12, 0x8b44f7af);
139 OP(C, D, A, B, 17, 0xffff5bb1);
140 OP(B, C, D, A, 22, 0x895cd7be);
141 OP(A, B, C, D, 7, 0x6b901122);
142 OP(D, A, B, C, 12, 0xfd987193);
143 OP(C, D, A, B, 17, 0xa679438e);
144 OP(B, C, D, A, 22, 0x49b40821);
151 #define OP(f, a, b, c, d, k, s, T) \
154 a += f (b, c, d) + correct_words[k] + T; \
161 OP(
FG, A, B, C, D, 1, 5, 0xf61e2562);
162 OP(
FG, D, A, B, C, 6, 9, 0xc040b340);
163 OP(
FG, C, D, A, B, 11, 14, 0x265e5a51);
164 OP(
FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
165 OP(
FG, A, B, C, D, 5, 5, 0xd62f105d);
166 OP(
FG, D, A, B, C, 10, 9, 0x02441453);
167 OP(
FG, C, D, A, B, 15, 14, 0xd8a1e681);
168 OP(
FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
169 OP(
FG, A, B, C, D, 9, 5, 0x21e1cde6);
170 OP(
FG, D, A, B, C, 14, 9, 0xc33707d6);
171 OP(
FG, C, D, A, B, 3, 14, 0xf4d50d87);
172 OP(
FG, B, C, D, A, 8, 20, 0x455a14ed);
173 OP(
FG, A, B, C, D, 13, 5, 0xa9e3e905);
174 OP(
FG, D, A, B, C, 2, 9, 0xfcefa3f8);
175 OP(
FG, C, D, A, B, 7, 14, 0x676f02d9);
176 OP(
FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
179 OP(
FH, A, B, C, D, 5, 4, 0xfffa3942);
180 OP(
FH, D, A, B, C, 8, 11, 0x8771f681);
181 OP(
FH, C, D, A, B, 11, 16, 0x6d9d6122);
182 OP(
FH, B, C, D, A, 14, 23, 0xfde5380c);
183 OP(
FH, A, B, C, D, 1, 4, 0xa4beea44);
184 OP(
FH, D, A, B, C, 4, 11, 0x4bdecfa9);
185 OP(
FH, C, D, A, B, 7, 16, 0xf6bb4b60);
186 OP(
FH, B, C, D, A, 10, 23, 0xbebfbc70);
187 OP(
FH, A, B, C, D, 13, 4, 0x289b7ec6);
188 OP(
FH, D, A, B, C, 0, 11, 0xeaa127fa);
189 OP(
FH, C, D, A, B, 3, 16, 0xd4ef3085);
190 OP(
FH, B, C, D, A, 6, 23, 0x04881d05);
191 OP(
FH, A, B, C, D, 9, 4, 0xd9d4d039);
192 OP(
FH, D, A, B, C, 12, 11, 0xe6db99e5);
193 OP(
FH, C, D, A, B, 15, 16, 0x1fa27cf8);
194 OP(
FH, B, C, D, A, 2, 23, 0xc4ac5665);
197 OP(
FI, A, B, C, D, 0, 6, 0xf4292244);
198 OP(
FI, D, A, B, C, 7, 10, 0x432aff97);
199 OP(
FI, C, D, A, B, 14, 15, 0xab9423a7);
200 OP(
FI, B, C, D, A, 5, 21, 0xfc93a039);
201 OP(
FI, A, B, C, D, 12, 6, 0x655b59c3);
202 OP(
FI, D, A, B, C, 3, 10, 0x8f0ccc92);
203 OP(
FI, C, D, A, B, 10, 15, 0xffeff47d);
204 OP(
FI, B, C, D, A, 1, 21, 0x85845dd1);
205 OP(
FI, A, B, C, D, 8, 6, 0x6fa87e4f);
206 OP(
FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
207 OP(
FI, C, D, A, B, 6, 15, 0xa3014314);
208 OP(
FI, B, C, D, A, 13, 21, 0x4e0811a1);
209 OP(
FI, A, B, C, D, 4, 6, 0xf7537e82);
210 OP(
FI, D, A, B, C, 11, 10, 0xbd3af235);
211 OP(
FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
212 OP(
FI, B, C, D, A, 9, 21, 0xeb86d391);
236 size_t left_over = ctx->
buflen;
237 size_t add = 128 - left_over > len ? len : 128 - left_over;
239 memcpy(&ctx->
buffer[left_over], buffer, add);
242 if (left_over + add > 64)
247 memcpy(ctx->
buffer, &ctx->
buffer[(left_over + add) & ~63], (left_over + add) & 63);
248 ctx->
buflen = (left_over + add) & 63;
251 buffer = (
const char *) buffer + add;
259 buffer = (
const char *) buffer + (len & ~63);
266 memcpy(ctx->
buffer, buffer, len);
315 ctx->
total[0] += bytes;
316 if (ctx->
total[0] < bytes)
321 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
322 memcpy(&ctx->
buffer[bytes], fillbuf, pad);
341 #define BLOCKSIZE 4096
361 n = fread(buffer + sum, 1,
BLOCKSIZE - sum, stream);
367 if (n == 0 && ferror(stream))
400 int md5_file(
char *filename,
int binary,
unsigned char *md5_result)
404 fp = fopen(filename,
OPENOPTS(binary));
407 printf(
"md5sum: %s: %s\n", filename, strerror(errno));
413 printf(
"md5sum: %s: %s\n", filename, strerror(errno));
423 if (fp != stdin && fclose(fp) == EOF)
425 printf(
"md5sum: %s: %s\n", filename, strerror(errno));
void * md5_read_ctx(const struct md5_ctx *ctx, void *resbuf)
#define OP(a, b, c, d, s, T)
void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
void md5_init_ctx(struct md5_ctx *ctx)
int md5_stream(FILE *stream, void *resblock)
void * md5_finish_ctx(struct md5_ctx *ctx, void *resbuf)
void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx)
int md5_file(char *filename, int binary, unsigned char *md5_result)