1 /* ====================================================================
2 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
58 /************************************************************************
60 * Software Development Group
61 * National Center for Supercomputing Applications
62 * University of Illinois at Urbana-Champaign
63 * 605 E. Springfield, Champaign, IL 61820
66 * Copyright (C) 1995, Board of Trustees of the University of Illinois
68 ************************************************************************
70 * md5.c: NCSA HTTPd code which uses the md5c.c RSA Code
72 * Original Code Copyright (C) 1994, Jeff Hostetler, Spyglass, Inc.
73 * Portions of Content-MD5 code Copyright (C) 1993, 1994 by Carnegie Mellon
74 * University (see Copyright below).
75 * Portions of Content-MD5 code Copyright (C) 1991 Bell Communications
76 * Research, Inc. (Bellcore) (see Copyright below).
77 * Portions extracted from mpack, John G. Myers - jgm+@cmu.edu
78 * Content-MD5 Code contributed by Martin Hamilton (martin@net.lut.ac.uk)
84 /* md5.c --Module Interface to MD5. */
85 /* Jeff Hostetler, Spyglass, Inc., 1994. */
90 API_EXPORT(char *) ap_md5_binary(pool *p, const unsigned char *buf, int length)
92 const char *hex = "0123456789abcdef";
94 unsigned char hash[16];
99 * Take the MD5 hash of the string argument.
103 ap_MD5Update(&my_md5, buf, (unsigned int)length);
104 ap_MD5Final(hash, &my_md5);
106 for (i = 0, r = result; i < 16; i++) {
107 *r++ = hex[hash[i] >> 4];
108 *r++ = hex[hash[i] & 0xF];
112 return ap_pstrdup(p, result);
115 API_EXPORT(char *) ap_md5(pool *p, const unsigned char *string)
117 return ap_md5_binary(p, string, (int) strlen((char *)string));
120 /* these portions extracted from mpack, John G. Myers - jgm+@cmu.edu */
122 /* (C) Copyright 1993,1994 by Carnegie Mellon University
123 * All Rights Reserved.
125 * Permission to use, copy, modify, distribute, and sell this software
126 * and its documentation for any purpose is hereby granted without
127 * fee, provided that the above copyright notice appear in all copies
128 * and that both that copyright notice and this permission notice
129 * appear in supporting documentation, and that the name of Carnegie
130 * Mellon University not be used in advertising or publicity
131 * pertaining to distribution of the software without specific,
132 * written prior permission. Carnegie Mellon University makes no
133 * representations about the suitability of this software for any
134 * purpose. It is provided "as is" without express or implied
137 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
138 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
139 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
140 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
142 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
143 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
148 * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
150 * Permission to use, copy, modify, and distribute this material
151 * for any purpose and without fee is hereby granted, provided
152 * that the above copyright notice and this permission notice
153 * appear in all copies, and that the name of Bellcore not be
154 * used in advertising or publicity pertaining to this
155 * material without the specific, prior written permission
156 * of an authorized representative of Bellcore. BELLCORE
157 * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
158 * OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
159 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
162 static char basis_64[] =
163 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
165 API_EXPORT(char *) ap_md5contextTo64(pool *a, AP_MD5_CTX * context)
167 unsigned char digest[18];
172 encodedDigest = (char *) ap_pcalloc(a, 25 * sizeof(char));
174 ap_MD5Final(digest, context);
175 digest[sizeof(digest) - 1] = digest[sizeof(digest) - 2] = 0;
178 for (i = 0; i < sizeof(digest); i += 3) {
179 *p++ = basis_64[digest[i] >> 2];
180 *p++ = basis_64[((digest[i] & 0x3) << 4) | ((int) (digest[i + 1] & 0xF0) >> 4)];
181 *p++ = basis_64[((digest[i + 1] & 0xF) << 2) | ((int) (digest[i + 2] & 0xC0) >> 6)];
182 *p++ = basis_64[digest[i + 2] & 0x3F];
187 return encodedDigest;
190 #ifdef CHARSET_EBCDIC
192 API_EXPORT(char *) ap_md5digest(pool *p, FILE *infile, int convert)
195 unsigned char buf[1000];
199 ap_MD5Init(&context);
200 while ((nbytes = fread(buf, 1, sizeof(buf), infile))) {
203 ascii2ebcdic(buf, buf, nbytes);
205 ap_MD5Update(&context, buf, nbytes);
208 return ap_md5contextTo64(p, &context);
213 API_EXPORT(char *) ap_md5digest(pool *p, FILE *infile)
216 unsigned char buf[1000];
220 ap_MD5Init(&context);
221 while ((nbytes = fread(buf, 1, sizeof(buf), infile))) {
223 ap_MD5Update(&context, buf, nbytes);
226 return ap_md5contextTo64(p, &context);
229 #endif /* CHARSET_EBCDIC */