📄 jk_md5.c
字号:
/* * This is work is derived from material Copyright RSA Data Security, Inc. * * The RSA copyright statement and Licence for that original material is * included below. This is followed by the Apache copyright statement and * licence for the modifications made to that material. *//* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm *//* 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. *//* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * Portions of this software are based upon public domain software * originally written at the National Center for Supercomputing Applications, * University of Illinois, Urbana-Champaign. *//* * The ap_MD5Encode() routine uses much code obtained from the FreeBSD 3.0 * MD5 crypt() function, which is licenced as follows: * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE" (Revision 42): * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp * ---------------------------------------------------------------------------- *//*************************************************************************** * Description: MD5 encoding wrapper * * Author: Henri Gomez <hgomez@slib.fr> * * Version: $Revision: 1.4 $ * ***************************************************************************//* * JK MD5 Encoding function (jk_MD5Encode) * * Jk delegate MD5 encoding to ap_MD5Encode when used in Apache Web-Server. * When another web-server is used like NES/IIS, we should use corresponding calls. * NES/IIS specialists will add the necessary code but until that, I reused the code * from Apache HTTP server. * * Nota: If you use an EBCDIC system without Apache, you'll have to use MD5 encoding * corresponding call or have a ebcdic2ascii() functions somewhere. * For example current AS/400 have MD5 encoding support APIs but olders not.... */ #include "jk_global.h"#include "jk_env.h"#include "jk_md5.h"char * JK_METHOD jk2_hextocstr(unsigned char *org, char * dst, int n){ char * os = dst; unsigned char v; static unsigned char zitohex[] = "0123456789ABCDEF"; while (--n >= 0) { v = *org++; *dst++ = zitohex[v >> 4]; *dst++ = zitohex[v&0x0f]; } *dst = 0; return (os);}#ifndef USE_APACHE_MD5 /* Constants for jk2_MD5Transform routine. */#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 21static void jk2_MD5Transform(JK_UINT4 state[4], const unsigned char block[64]);static void jk2_Encode(unsigned char *output, const JK_UINT4 *input, unsigned int len);static void jk2_Decode(JK_UINT4 *output, const unsigned char *input, unsigned int len);static void jk2_MD5Init(JK_MD5_CTX *context);static void jk2_MD5Update(JK_MD5_CTX *context, const unsigned char *input, unsigned int inputLen);/*static void jk2_MD5Final(unsigned char digest[JK_MD5_DIGESTSIZE], JK_MD5_CTX *context);*/static unsigned char 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};/* 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) + (JK_UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }#define GG(a, b, c, d, x, s, ac) { \ (a) += G ((b), (c), (d)) + (x) + (JK_UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }#define HH(a, b, c, d, x, s, ac) { \ (a) += H ((b), (c), (d)) + (x) + (JK_UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }#define II(a, b, c, d, x, s, ac) { \ (a) += I ((b), (c), (d)) + (x) + (JK_UINT4)(ac); \ (a) = ROTATE_LEFT ((a), (s)); \ (a) += (b); \ }/* MD5 initialization. Begins an MD5 operation, writing a new context. */static void jk2_MD5Init(JK_MD5_CTX *context){ context->count[0] = context->count[1] = 0; /* Load magic initialization constants. */ context->state[0] = 0x67452301; context->state[1] = 0xefcdab89; context->state[2] = 0x98badcfe; context->state[3] = 0x10325476;}/* MD5 block update operation. Continues an MD5 message-digest operation, processing another message block, and updating the context. */static void jk2_MD5Update(JK_MD5_CTX *context, const unsigned char *input, unsigned int inputLen){ unsigned int i, idx, partLen; /* Compute number of bytes mod 64 */ idx = (unsigned int) ((context->count[0] >> 3) & 0x3F); /* Update number of bits */ if ((context->count[0] += ((JK_UINT4) inputLen << 3)) < ((JK_UINT4) inputLen << 3)) { context->count[1]++; } context->count[1] += (JK_UINT4) inputLen >> 29; partLen = 64 - idx; /* Transform as many times as possible. */#ifndef CHARSET_EBCDIC if (inputLen >= partLen) { memcpy(&context->buffer[idx], input, partLen); jk2_MD5Transform(context->state, context->buffer); for (i = partLen; i + 63 < inputLen; i += 64) { jk2_MD5Transform(context->state, &input[i]); } idx = 0; } else { i = 0; } /* Buffer remaining input */ memcpy(&context->buffer[idx], &input[i], inputLen - i);#else /*CHARSET_EBCDIC*/ if (inputLen >= partLen) { ebcdic2ascii(&context->buffer[idx], input, partLen);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -