?? md5.c
字號:
/* * md5 - RSA Data Security, Inc. MD5 Message-Digest Algorithm * * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MER- * CHANTABILITY AND FITNESS. IN NO EVENT SHALL LANDON CURT * NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * @(#) $Revision: 29.3 $ * @(#) $Id: md5.c,v 29.3 2004/08/03 12:28:29 chongo Exp $ * @(#) $Source: /usr/local/src/cmd/calc/RCS/md5.c,v $ * * This file is not covered under version 2.1 of the GNU LGPL. *//* *********************************************************************** ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** ** ** ** License to copy and use this software is granted provided that ** ** it is identified as the "RSA Data Security, Inc. MD5 Message- ** ** Digest Algorithm" in all material mentioning or referencing this ** ** software or this function. ** ** ** ** License is also granted to make and use derivative works ** ** provided that such works are identified as "derived from the RSA ** ** Data Security, Inc. MD5 Message-Digest Algorithm" in all ** ** material mentioning or referencing the derived work. ** ** ** ** RSA Data Security, Inc. makes no representations concerning ** ** either the merchantability of this software or the suitability ** ** of this software for any particular purpose. It is provided "as ** ** is" without express or implied warranty of any kind. ** ** ** ** These notices must be retained in any copies of any part of this ** ** documentation and/or software. ** *********************************************************************** *//* *********************************************************************** ** Message-digest routines: ** ** To form the message digest for a message M ** ** (1) Initialize a context buffer md5Ctx using MD5Init ** ** (2) Call MD5Update on md5Ctx and M ** ** (3) Call MD5Final on md5Ctx ** ** The message digest is now in md5Ctx->digest[0...15] ** *********************************************************************** */#include <stdio.h>#include "longbits.h"#include "align32.h"#include "endian_calc.h"#include "value.h"#include "hash.h"#include "md5.h"/* * The F, G, H and I are basic MD5 functions. The following * identity saves one boolean operation. * * F: (((x) & (y)) | (~(x) & (z))) == ((z) ^ ((x) & ((y) ^ (z)))) * G: (((x) & (z)) | ((y) & ~(z))) == ((y) ^ ((z) & ((x) ^ (y)))) *//* F, G, H and I are basic MD5 functions */#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))#define H(x, y, z) ((x) ^ (y) ^ (z))#define I(x, y, z) ((y) ^ ((x) | (~z)))/* rotate a 32 bit value */#define ROT(X,n) (((X)<<(n)) | ((X)>>(32-(n))))/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 *//* Rotation is separate from addition to prevent recomputation */#define S11 7#define S12 12#define S13 17#define S14 22#define FF(a, b, c, d, x, s, ac) \ {(a) += F((b), (c), (d)) + (x) + (USB32)(ac); \ (a) = ROT((a), (s)); \ (a) += (b); \ }#define S21 5#define S22 9#define S23 14#define S24 20#define GG(a, b, c, d, x, s, ac) \ {(a) += G((b), (c), (d)) + (x) + (USB32)(ac); \ (a) = ROT((a), (s)); \ (a) += (b); \ }#define S31 4#define S32 11#define S33 16#define S34 23#define HH(a, b, c, d, x, s, ac) \ {(a) += H((b), (c), (d)) + (x) + (USB32)(ac); \ (a) = ROT((a), (s)); \ (a) += (b); \ }#define S41 6#define S42 10#define S43 15#define S44 21#define II(a, b, c, d, x, s, ac) \ {(a) += I((b), (c), (d)) + (x) + (USB32)(ac); \ (a) = ROT((a), (s)); \ (a) += (b); \ }/* forward declaration */static void MD5Init(HASH *state);static void MD5Update(HASH *state, USB8 *inBuf, USB32 count);static void MD5Transform(USB32*, USB32*);static void MD5Final(HASH *state);/* static USB32 in[MD5_CHUNKWORDS]; */static void MD5_chkpt(HASH *state);static void MD5_note(int special, HASH *state);static void MD5_type(int type, HASH *state);static ZVALUE MD5_final_state(HASH *state);static int MD5_cmp(HASH *a, HASH *b);static void MD5_print(HASH *state);/* * MD5Init - initialize the message-digest context * * The routine MD5Init initializes the message-digest context * md5Ctx. All fields are set to zero. */static voidMD5Init(HASH *state){ MD5_CTX *md5Ctx = &state->h_union.h_md5; /* digest state */ /* load magic initialization constants */ md5Ctx->digest[0] = (USB32)0x67452301; md5Ctx->digest[1] = (USB32)0xefcdab89; md5Ctx->digest[2] = (USB32)0x98badcfe; md5Ctx->digest[3] = (USB32)0x10325476; /* Initialise bit count */ md5Ctx->countLo = 0L; md5Ctx->countHi = 0L; md5Ctx->datalen = 0L;}/* * MD5Update - update message-digest context */static voidMD5Update(HASH *state, USB8 *inBuf, USB32 count){ MD5_CTX *md5Ctx = &state->h_union.h_md5; /* digest state */ USB32 datalen = md5Ctx->datalen; USB32 cpylen;#if CALC_BYTE_ORDER == BIG_ENDIAN int cnt;#endif /* * Update the full count, even if some of it is buffered for later */ MD5COUNT(md5Ctx, count); /* determine the size we need to copy */ cpylen = MD5_CHUNKSIZE - datalen; /* case: new data will not fill the inBuf */ if (cpylen > count) { memcpy((char *)md5Ctx->data + datalen, (char *)inBuf, count); md5Ctx->datalen = datalen + count; return; } /* case: md5Ctx->in will be filled */ memcpy((char *)md5Ctx->data + datalen, inBuf, cpylen); /* * process data in MD5_CHUNKSIZE chunks */ for (;;) {#if CALC_BYTE_ORDER == BIG_ENDIAN if (state->bytes) { /* byte swap data into little endian order */ for (cnt=0; cnt < (int)MD5_CHUNKWORDS; ++cnt) { SWAP_B8_IN_B32(md5Ctx->data + cnt, md5Ctx->data + cnt); } }#endif MD5Transform(md5Ctx->digest, md5Ctx->data); inBuf += cpylen; count -= cpylen; if (count < MD5_CHUNKSIZE) break; cpylen = MD5_CHUNKSIZE; memcpy(md5Ctx->data, inBuf, cpylen); } /* * Handle any remaining bytes of data. * This should only happen once on the final lot of data */ if (count > 0) { memcpy(md5Ctx->data, inBuf, count); } md5Ctx->datalen = count;}/* * MD5Final - terminate the message-digest computation * * The routine MD5Final terminates the message-digest computation and * ends with the desired message digest in md5Ctx->digest[0...15]. */static voidMD5Final(HASH *state){ MD5_CTX *md5Ctx = &state->h_union.h_md5; /* digest state */ USB32 count = md5Ctx->datalen; USB32 lowBitcount = md5Ctx->countLo; USB32 highBitcount = md5Ctx->countHi; USB8 *data = (USB8 *)md5Ctx->data;#if CALC_BYTE_ORDER == BIG_ENDIAN int i;#endif /* Pad to end of chunk */ memset(data + count, 0, MD5_CHUNKSIZE - count); /* * If processing bytes, set the first byte of padding to 0x80. * if processing words: on a big-endian machine set the first * byte of padding to 0x80000000, on a little-endian machine set * the first four bytes to 0x80. * * This is safe since there is always at least one byte or word free. */#if CALC_BYTE_ORDER == BIG_ENDIAN if (state->bytes) { data[count] = 0x80; for (i=0; i < (int)MD5_CHUNKWORDS; ++i) { SWAP_B8_IN_B32(md5Ctx->data + i, md5Ctx->data + i); } } else { if (count % 4) { math_error("This should not happen in MD5Final"); /*NOTREACHED*/ } data[count + 3] = 0x80; }#else data[count] = 0x80;#endif if (count >= MD5_CHUNKSIZE-8) { MD5Transform(md5Ctx->digest, md5Ctx->data); /* Now load another chunk with 56 bytes of padding */ memset(data, 0, MD5_CHUNKSIZE-8); } /* append length in bits and transform */ md5Ctx->data[MD5_LOW] = (lowBitcount << 3); md5Ctx->data[MD5_HIGH] = (highBitcount << 3) | (lowBitcount >> 29); MD5Transform(md5Ctx->digest, md5Ctx->data);}/* * Basic MD5 step. Transforms digest based on in. */static voidMD5Transform(USB32 *digest, USB32 *in){ USB32 a = digest[0], b = digest[1], c = digest[2], d = digest[3]; /* Round 1 */ FF( a, b, c, d, in[ 0], S11, 3614090360UL); /* 1 */ FF( d, a, b, c, in[ 1], S12, 3905402710UL); /* 2 */ FF( c, d, a, b, in[ 2], S13, 606105819UL); /* 3 */ FF( b, c, d, a, in[ 3], S14, 3250441966UL); /* 4 */ FF( a, b, c, d, in[ 4], S11, 4118548399UL); /* 5 */ FF( d, a, b, c, in[ 5], S12, 1200080426UL); /* 6 */ FF( c, d, a, b, in[ 6], S13, 2821735955UL); /* 7 */ FF( b, c, d, a, in[ 7], S14, 4249261313UL); /* 8 */ FF( a, b, c, d, in[ 8], S11, 1770035416UL); /* 9 */ FF( d, a, b, c, in[ 9], S12, 2336552879UL); /* 10 */ FF( c, d, a, b, in[10], S13, 4294925233UL); /* 11 */ FF( b, c, d, a, in[11], S14, 2304563134UL); /* 12 */ FF( a, b, c, d, in[12], S11, 1804603682UL); /* 13 */ FF( d, a, b, c, in[13], S12, 4254626195UL); /* 14 */ FF( c, d, a, b, in[14], S13, 2792965006UL); /* 15 */ FF( b, c, d, a, in[15], S14, 1236535329UL); /* 16 */ /* Round 2 */ GG( a, b, c, d, in[ 1], S21, 4129170786UL); /* 17 */ GG( d, a, b, c, in[ 6], S22, 3225465664UL); /* 18 */ GG( c, d, a, b, in[11], S23, 643717713UL); /* 19 */ GG( b, c, d, a, in[ 0], S24, 3921069994UL); /* 20 */ GG( a, b, c, d, in[ 5], S21, 3593408605UL); /* 21 */ GG( d, a, b, c, in[10], S22, 38016083UL); /* 22 */ GG( c, d, a, b, in[15], S23, 3634488961UL); /* 23 */ GG( b, c, d, a, in[ 4], S24, 3889429448UL); /* 24 */ GG( a, b, c, d, in[ 9], S21, 568446438UL); /* 25 */ GG( d, a, b, c, in[14], S22, 3275163606UL); /* 26 */ GG( c, d, a, b, in[ 3], S23, 4107603335UL); /* 27 */ GG( b, c, d, a, in[ 8], S24, 1163531501UL); /* 28 */ GG( a, b, c, d, in[13], S21, 2850285829UL); /* 29 */ GG( d, a, b, c, in[ 2], S22, 4243563512UL); /* 30 */ GG( c, d, a, b, in[ 7], S23, 1735328473UL); /* 31 */ GG( b, c, d, a, in[12], S24, 2368359562UL); /* 32 */ /* Round 3 */ HH( a, b, c, d, in[ 5], S31, 4294588738UL); /* 33 */ HH( d, a, b, c, in[ 8], S32, 2272392833UL); /* 34 */ HH( c, d, a, b, in[11], S33, 1839030562UL); /* 35 */ HH( b, c, d, a, in[14], S34, 4259657740UL); /* 36 */ HH( a, b, c, d, in[ 1], S31, 2763975236UL); /* 37 */ HH( d, a, b, c, in[ 4], S32, 1272893353UL); /* 38 */ HH( c, d, a, b, in[ 7], S33, 4139469664UL); /* 39 */ HH( b, c, d, a, in[10], S34, 3200236656UL); /* 40 */ HH( a, b, c, d, in[13], S31, 681279174UL); /* 41 */ HH( d, a, b, c, in[ 0], S32, 3936430074UL); /* 42 */ HH( c, d, a, b, in[ 3], S33, 3572445317UL); /* 43 */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -