home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 April / PCO0499.ISO / filesbbs / os2 / apach134.arj / APACH134.ZIP / src / main / util_md5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-01  |  7.6 KB  |  205 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  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
  14.  *    distribution.
  15.  *
  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/)."
  20.  *
  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
  24.  *    apache@apache.org.
  25.  *
  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.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  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.  * ====================================================================
  48.  *
  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/>.
  55.  *
  56.  */
  57.  
  58. /************************************************************************
  59.  * NCSA HTTPd Server
  60.  * Software Development Group
  61.  * National Center for Supercomputing Applications
  62.  * University of Illinois at Urbana-Champaign
  63.  * 605 E. Springfield, Champaign, IL 61820
  64.  * httpd@ncsa.uiuc.edu
  65.  *
  66.  * Copyright  (C)  1995, Board of Trustees of the University of Illinois
  67.  *
  68.  ************************************************************************
  69.  *
  70.  * md5.c: NCSA HTTPd code which uses the md5c.c RSA Code
  71.  *
  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)
  79.  *
  80.  */
  81.  
  82.  
  83.  
  84. /* md5.c --Module Interface to MD5. */
  85. /* Jeff Hostetler, Spyglass, Inc., 1994. */
  86.  
  87. #include "httpd.h"
  88. #include "util_md5.h"
  89.  
  90. API_EXPORT(char *) ap_md5_binary(pool *p, const unsigned char *buf, int length)
  91. {
  92.     const char *hex = "0123456789abcdef";
  93.     AP_MD5_CTX my_md5;
  94.     unsigned char hash[16];
  95.     char *r, result[33];
  96.     int i;
  97.  
  98.     /*
  99.      * Take the MD5 hash of the string argument.
  100.      */
  101.  
  102.     ap_MD5Init(&my_md5);
  103.     ap_MD5Update(&my_md5, buf, length);
  104.     ap_MD5Final(hash, &my_md5);
  105.  
  106.     for (i = 0, r = result; i < 16; i++) {
  107.     *r++ = hex[hash[i] >> 4];
  108.     *r++ = hex[hash[i] & 0xF];
  109.     }
  110.     *r = '\0';
  111.  
  112.     return ap_pstrdup(p, result);
  113. }
  114.  
  115. API_EXPORT(char *) ap_md5(pool *p, const unsigned char *string)
  116. {
  117.     return ap_md5_binary(p, string, strlen(string));
  118. }
  119.  
  120. /* these portions extracted from mpack, John G. Myers - jgm+@cmu.edu */
  121.  
  122. /* (C) Copyright 1993,1994 by Carnegie Mellon University
  123.  * All Rights Reserved.
  124.  *
  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
  135.  * warranty.
  136.  *
  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
  144.  * SOFTWARE.
  145.  */
  146.  
  147. /*
  148.  * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
  149.  *
  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.  
  160.  */
  161.  
  162. static char basis_64[] =
  163. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  164.  
  165. API_EXPORT(char *) ap_md5contextTo64(pool *a, AP_MD5_CTX * context)
  166. {
  167.     unsigned char digest[18];
  168.     char *encodedDigest;
  169.     int i;
  170.     char *p;
  171.  
  172.     encodedDigest = (char *) ap_pcalloc(a, 25 * sizeof(char));
  173.  
  174.     ap_MD5Final(digest, context);
  175.     digest[sizeof(digest) - 1] = digest[sizeof(digest) - 2] = 0;
  176.  
  177.     p = encodedDigest;
  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];
  183.     }
  184.     *p-- = '\0';
  185.     *p-- = '=';
  186.     *p-- = '=';
  187.     return encodedDigest;
  188. }
  189.  
  190. API_EXPORT(char *) ap_md5digest(pool *p, FILE *infile)
  191. {
  192.     AP_MD5_CTX context;
  193.     unsigned char buf[1000];
  194.     long length = 0;
  195.     int nbytes;
  196.  
  197.     ap_MD5Init(&context);
  198.     while ((nbytes = fread(buf, 1, sizeof(buf), infile))) {
  199.     length += nbytes;
  200.     ap_MD5Update(&context, buf, nbytes);
  201.     }
  202.     rewind(infile);
  203.     return ap_md5contextTo64(p, &context);
  204. }
  205.