?? md5sum.c
字號:
/* md5sum.c - Compute MD5 checksum of files or strings according to the * definition of MD5 in RFC 1321 from April 1992. * Copyright (C) 1995-1999 Free Software Foundation, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu> *//* Hacked to work with BusyBox by Alfred M. Szmidt <ams@trillian.itslinux.org> *//* * June 29, 2001 Manuel Novoa III * * Added MD5SUM_SIZE_VS_SPEED configuration option. * * Current valid values, with data from my system for comparison, are: * (using uClibc and running on linux-2.4.4.tar.bz2) * user times (sec) text size (386) * 0 (fastest) 1.1 6144 * 1 1.4 5392 * 2 3.0 5088 * 3 (smallest) 5.1 4912 */#define MD5SUM_SIZE_VS_SPEED 2/**********************************************************************/#include <stdio.h>#include <errno.h>#include <ctype.h>#include <getopt.h>#include <stdlib.h>#include <string.h>#include <endian.h>#include <sys/types.h>#if defined HAVE_LIMITS_H# include <limits.h>#endif#include "busybox.h"/* For some silly reason, this file uses backwards TRUE and FALSE conventions */#undef TRUE#undef FALSE#define FALSE ((int) 1)#define TRUE ((int) 0) /* It is unfortunate that C does not provide an operator for cyclic rotation. Hope the C compiler is smart enough. gcc 2.95.4 seems to be --aaronl */#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))//----------------------------------------------------------------------------//--------md5.c//----------------------------------------------------------------------------/* md5.c - Functions to compute MD5 message digest of files or memory blocks * according to the definition of MD5 in RFC 1321 from April 1992. *//* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. *///----------------------------------------------------------------------------//--------md5.h//----------------------------------------------------------------------------/* md5.h - Declaration of functions and data types used for MD5 sum computing library functions. */typedef u_int32_t md5_uint32;/* Structure to save state of computation between the single steps. */struct md5_ctx{ md5_uint32 A; md5_uint32 B; md5_uint32 C; md5_uint32 D; md5_uint32 total[2]; md5_uint32 buflen; char buffer[128];};/* * The following three functions are build up the low level used in * the functions `md5_stream' and `md5_buffer'. *//* Initialize structure containing state of computation. (RFC 1321, 3.3: Step 3) */static void md5_init_ctx __P ((struct md5_ctx *ctx));/* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is necessary that LEN is a multiple of 64!!! */static void md5_process_block __P ((const void *buffer, size_t len, struct md5_ctx *ctx));/* Starting with the result of former calls of this function (or the initialization function update the context for the next LEN bytes starting at BUFFER. It is NOT required that LEN is a multiple of 64. */static void md5_process_bytes __P ((const void *buffer, size_t len, struct md5_ctx *ctx));/* Process the remaining bytes in the buffer and put result from CTX in first 16 bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. IMPORTANT: On some systems it is required that RESBUF is correctly aligned for a 32 bits value. */static void *md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf));/* Compute MD5 message digest for bytes read from STREAM. The resulting message digest number will be written into the 16 bytes beginning at RESBLOCK. */static int md5_stream __P ((FILE *stream, void *resblock));/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */static void *md5_buffer __P ((const char *buffer, size_t len, void *resblock));//----------------------------------------------------------------------------//--------end of md5.h//----------------------------------------------------------------------------/* Handle endian-ness */#if __BYTE_ORDER == __LITTLE_ENDIAN #define SWAP(n) (n)#else #define SWAP(n) ((n << 24) | ((n&65280)<<8) | ((n&16711680)>>8) | (n>>24))#endif#if MD5SUM_SIZE_VS_SPEED == 0/* This array contains the bytes used to pad the buffer to the next 64-byte boundary. (RFC 1321, 3.1: Step 1) */static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };#endif/* Initialize structure containing state of computation. (RFC 1321, 3.3: Step 3) */void md5_init_ctx(struct md5_ctx *ctx){ ctx->A = 0x67452301; ctx->B = 0xefcdab89; ctx->C = 0x98badcfe; ctx->D = 0x10325476; ctx->total[0] = ctx->total[1] = 0; ctx->buflen = 0;}/* Process the remaining bytes in the internal buffer and the usual prolog according to the standard and write the result to RESBUF. IMPORTANT: On some systems it is required that RESBUF is correctly aligned for a 32 bits value. */static void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf){ /* Take yet unprocessed bytes into account. */ md5_uint32 bytes = ctx->buflen; size_t pad; /* Now count remaining bytes. */ ctx->total[0] += bytes; if (ctx->total[0] < bytes) ++ctx->total[1]; pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;#if MD5SUM_SIZE_VS_SPEED > 0 memset(&ctx->buffer[bytes], 0, pad); ctx->buffer[bytes] = 0x80;#else memcpy(&ctx->buffer[bytes], fillbuf, pad);#endif /* Put the 64-bit file length in *bits* at the end of the buffer. */ *(md5_uint32 *) & ctx->buffer[bytes + pad] = SWAP(ctx->total[0] << 3); *(md5_uint32 *) & ctx->buffer[bytes + pad + 4] = SWAP( ((ctx->total[1] << 3) | (ctx->total[0] >> 29)) ); /* Process last bytes. */ md5_process_block(ctx->buffer, bytes + pad + 8, ctx);/* Put result from CTX in first 16 bytes following RESBUF. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. IMPORTANT: On some systems it is required that RESBUF is correctly aligned for a 32 bits value. */ ((md5_uint32 *) resbuf)[0] = SWAP(ctx->A); ((md5_uint32 *) resbuf)[1] = SWAP(ctx->B); ((md5_uint32 *) resbuf)[2] = SWAP(ctx->C); ((md5_uint32 *) resbuf)[3] = SWAP(ctx->D); return resbuf;}/* Compute MD5 message digest for bytes read from STREAM. The resulting message digest number will be written into the 16 bytes beginning at RESBLOCK. */static int md5_stream(FILE *stream, void *resblock){ /* Important: BLOCKSIZE must be a multiple of 64. */static const int BLOCKSIZE = 4096; struct md5_ctx ctx; char buffer[BLOCKSIZE + 72]; size_t sum; /* Initialize the computation context. */ md5_init_ctx(&ctx); /* Iterate over full file contents. */ while (1) { /* We read the file in blocks of BLOCKSIZE bytes. One call of the computation function processes the whole buffer so that with the next round of the loop another block can be read. */ size_t n; sum = 0; /* Read block. Take care for partial reads. */ do { n = fread(buffer + sum, 1, BLOCKSIZE - sum, stream); sum += n; } while (sum < BLOCKSIZE && n != 0); if (n == 0 && ferror(stream)) return 1; /* If end of file is reached, end the loop. */ if (n == 0) break; /* Process buffer with BLOCKSIZE bytes. Note that BLOCKSIZE % 64 == 0 */ md5_process_block(buffer, BLOCKSIZE, &ctx); } /* Add the last bytes if necessary. */ if (sum > 0) md5_process_bytes(buffer, sum, &ctx); /* Construct result in desired memory. */ md5_finish_ctx(&ctx, resblock); return 0;}/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest. */static void *md5_buffer(const char *buffer, size_t len, void *resblock){ struct md5_ctx ctx; /* Initialize the computation context. */ md5_init_ctx(&ctx); /* Process whole buffer but last len % 64 bytes. */ md5_process_bytes(buffer, len, &ctx); /* Put result in desired memory area. */ return md5_finish_ctx(&ctx, resblock);}static void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx){ /* When we already have some bits in our internal buffer concatenate both inputs first. */ if (ctx->buflen != 0) { size_t left_over = ctx->buflen; size_t add = 128 - left_over > len ? len : 128 - left_over; memcpy(&ctx->buffer[left_over], buffer, add); ctx->buflen += add; if (left_over + add > 64) { md5_process_block(ctx->buffer, (left_over + add) & ~63, ctx); /* The regions in the following copy operation cannot overlap. */ memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63], (left_over + add) & 63); ctx->buflen = (left_over + add) & 63; } buffer = (const char *) buffer + add; len -= add; } /* Process available complete blocks. */ if (len > 64) { md5_process_block(buffer, len & ~63, ctx); buffer = (const char *) buffer + (len & ~63); len &= 63; } /* Move remaining bytes in internal buffer. */ if (len > 0) { memcpy(ctx->buffer, buffer, len); ctx->buflen = len; }}/* These are the four functions used in the four steps of the MD5 algorithm and defined in the RFC 1321. The first function is a little bit optimized (as found in Colin Plumbs public domain implementation). *//* #define FF(b, c, d) ((b & c) | (~b & d)) */#define FF(b, c, d) (d ^ (b & (c ^ d)))#define FG(b, c, d) FF (d, b, c)#define FH(b, c, d) (b ^ c ^ d)#define FI(b, c, d) (c ^ (b | ~d))/* Process LEN bytes of BUFFER, accumulating context into CTX. It is assumed that LEN % 64 == 0. */static void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx){ md5_uint32 correct_words[16]; const md5_uint32 *words = buffer; size_t nwords = len / sizeof(md5_uint32); const md5_uint32 *endp = words + nwords;#if MD5SUM_SIZE_VS_SPEED > 0 static const md5_uint32 C_array[] = { /* round 1 */ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, /* round 2 */ 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, /* round 3 */ 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -