📄 pppmd5.c
字号:
/***********************************************************************//* *//* Module: pppmd5.c *//* Release: 2001.3 *//* Version: 2001.0 *//* Purpose: Implementation of the MD5 Message-Digest Algorithm *//* *//*---------------------------------------------------------------------*//* *//* Copyright 2001, Blunk Microsystems *//* ALL RIGHTS RESERVED *//* *//* Licensees have the non-exclusive right to use, modify, or extract *//* this computer program for software development at a single site. *//* This program may be resold or disseminated in executable format *//* only. The source code may not be redistributed or resold. *//* *//*---------------------------------------------------------------------*//* *//* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. 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. *//* *//***********************************************************************/#include "pppp.h"#include "md5.h"#include <string.h>/***********************************************************************//* Symbol Definitions *//***********************************************************************//*** Constants for transform().*/#define S11 7#define S12 12#define S13 17#define S14 22#define S21 5#define S22 9#define S23 14#define S24 20#define S31 4#define S32 11#define S33 16#define S34 23#define S41 6#define S42 10#define S43 15#define S44 21/***********************************************************************//* Macro Definitions *//***********************************************************************//*** F, G, H and I are basic MD5 functions.*/#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))#define H(x, y, z) ((x) ^ (y) ^ (z))#define I(x, y, z) ((y) ^ ((x) | (~z)))/*** ROTATE_LEFT rotates x left n bits.*/#define ROTATE_LEFT(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 FF(a, b, c, d, x, s, ac) \ { \ (a) += F ((b), (c), (d)) + (x) + (ui32)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }#define GG(a, b, c, d, x, s, ac) \ { \ (a) += G ((b), (c), (d)) + (x) + (ui32)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }#define HH(a, b, c, d, x, s, ac) \ { \ (a) += H ((b), (c), (d)) + (x) + (ui32)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }#define II(a, b, c, d, x, s, ac) \ { \ (a) += I ((b), (c), (d)) + (x) + (ui32)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }/***********************************************************************//* Global Variable Declarations *//***********************************************************************/static ui8 PADDING[64] ={ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};/***********************************************************************//* Local Function Definitions *//***********************************************************************//***********************************************************************//* encode: Encodes input (ui32) into output (ui8). Assumes len is *//* a multiple of 4. *//* *//* Inputs: output = pointer to buffer for encoded output *//* input = pointer to buffer holding input data *//* len = number of bytes of input data *//* *//***********************************************************************/static void encode(ui8 *output, const ui32 *input, uint len){ uint i, j; for (i = j = 0; j < len; i++, j += 4) { output[j] = (ui8)input[i]; output[j+1] = (ui8)(input[i] >> 8); output[j+2] = (ui8)(input[i] >> 16); output[j+3] = (ui8)(input[i] >> 24); }}/***********************************************************************//* decode: Decodes input (ui8) into output (ui32). Assumes len is *//* a multiple of 4. *//* *//* Inputs: output = pointer to buffer for decoded output *//* input = pointer to buffer holding input data *//* len = number of bytes of input data *//* *//***********************************************************************/static void decode(ui32 *output, const ui8 *input, uint len){ uint i, j; for (i = j = 0; j < len; ++i, j += 4) output[i] = input[j] | (input[j+1] << 8) | (input[j+2] << 16) | (input[j+3] << 24);}/***********************************************************************//* transform: MD5 basic transformation. Transforms state based on *//* block. *//* *//* Inputs: state = *//* block = *//* *//***********************************************************************/static void transform(ui32 state[4], const ui8 block[64]){ ui32 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; decode(x, block, 64); /*-------------------------------------------------------------------*/ /* Round 1. */ /*-------------------------------------------------------------------*/ FF(a, b, c, d, x[ 0], S11, 0xD76AA478); /* 1 */ FF(d, a, b, c, x[ 1], S12, 0xE8C7B756); /* 2 */ FF(c, d, a, b, x[ 2], S13, 0x242070DB); /* 3 */ FF(b, c, d, a, x[ 3], S14, 0xC1BDCEEE); /* 4 */ FF(a, b, c, d, x[ 4], S11, 0xF57C0FAF); /* 5 */ FF(d, a, b, c, x[ 5], S12, 0x4787C62A); /* 6 */ FF(c, d, a, b, x[ 6], S13, 0xA8304613); /* 7 */ FF(b, c, d, a, x[ 7], S14, 0xFD469501); /* 8 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -