home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / share / os2 / varios / apache / util_md5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-26  |  7.1 KB  |  194 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54. /************************************************************************
  55.  * NCSA HTTPd Server
  56.  * Software Development Group
  57.  * National Center for Supercomputing Applications
  58.  * University of Illinois at Urbana-Champaign
  59.  * 605 E. Springfield, Champaign, IL 61820
  60.  * httpd@ncsa.uiuc.edu
  61.  *
  62.  * Copyright  (C)  1995, Board of Trustees of the University of Illinois
  63.  *
  64.  ************************************************************************
  65.  *
  66.  * md5.c: NCSA HTTPd code which uses the md5c.c RSA Code
  67.  *
  68.  *  Original Code Copyright (C) 1994, Jeff Hostetler, Spyglass, Inc.
  69.  *  Portions of Content-MD5 code Copyright (C) 1993, 1994 by Carnegie Mellon
  70.  *     University (see Copyright below).
  71.  *  Portions of Content-MD5 code Copyright (C) 1991 Bell Communications 
  72.  *     Research, Inc. (Bellcore) (see Copyright below).
  73.  *  Portions extracted from mpack, John G. Myers - jgm+@cmu.edu
  74.  *  Content-MD5 Code contributed by Martin Hamilton (martin@net.lut.ac.uk)
  75.  *
  76.  */
  77.  
  78.  
  79.  
  80. /* md5.c --Module Interface to MD5. */
  81. /* Jeff Hostetler, Spyglass, Inc., 1994. */
  82.  
  83. #include "httpd.h"
  84. #include "util_md5.h"
  85.  
  86. char *md5 (pool *p, unsigned char *string)
  87. {
  88.     MD5_CTX my_md5;
  89.     unsigned char hash[16];
  90.     char *r, result[33];
  91.     int i;
  92.     
  93.     /*
  94.      * Take the MD5 hash of the string argument.
  95.      */
  96.  
  97.     MD5Init(&my_md5);
  98.     MD5Update(&my_md5, string, strlen((const char *)string));
  99.     MD5Final(hash, &my_md5);
  100.  
  101.     for (i=0, r=result; i<16; i++, r+=2)
  102.         sprintf(r, "%02x", hash[i]);
  103.     *r = '\0';
  104.  
  105.     return pstrdup(p, result);
  106. }
  107.  
  108. /* these portions extracted from mpack, John G. Myers - jgm+@cmu.edu */
  109.  
  110. /* (C) Copyright 1993,1994 by Carnegie Mellon University
  111.  * All Rights Reserved.
  112.  *
  113.  * Permission to use, copy, modify, distribute, and sell this software
  114.  * and its documentation for any purpose is hereby granted without
  115.  * fee, provided that the above copyright notice appear in all copies
  116.  * and that both that copyright notice and this permission notice
  117.  * appear in supporting documentation, and that the name of Carnegie
  118.  * Mellon University not be used in advertising or publicity
  119.  * pertaining to distribution of the software without specific,
  120.  * written prior permission.  Carnegie Mellon University makes no
  121.  * representations about the suitability of this software for any
  122.  * purpose.  It is provided "as is" without express or implied
  123.  * warranty.
  124.  *
  125.  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  126.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  127.  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  128.  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  129.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  130.  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  131.  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  132.  * SOFTWARE.
  133.  */
  134.  
  135. /*
  136.  * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
  137.  *
  138.  * Permission to use, copy, modify, and distribute this material
  139.  * for any purpose and without fee is hereby granted, provided
  140.  * that the above copyright notice and this permission notice
  141.  * appear in all copies, and that the name of Bellcore not be
  142.  * used in advertising or publicity pertaining to this
  143.  * material without the specific, prior written permission
  144.  * of an authorized representative of Bellcore.  BELLCORE
  145.  * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
  146.  * OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
  147.  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.  
  148.  */
  149.  
  150. static char basis_64[] =
  151.    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  152.  
  153. char *md5contextTo64(pool *a, MD5_CTX *context)
  154. {
  155.     unsigned char digest[18];
  156.     char *encodedDigest;
  157.     int i;
  158.     char *p;
  159.  
  160.     encodedDigest = (char *)pcalloc(a, 25 * sizeof(char));
  161.  
  162.     MD5Final(digest, context);
  163.     digest[sizeof(digest)-1] = digest[sizeof(digest)-2] = 0;
  164.  
  165.     p = encodedDigest;
  166.     for (i=0; i < sizeof(digest); i+=3) {
  167.         *p++ = basis_64[digest[i]>>2];
  168.         *p++ = basis_64[((digest[i] & 0x3)<<4) | ((int)(digest[i+1] & 0xF0)>>4)];
  169.         *p++ = basis_64[((digest[i+1] & 0xF)<<2) | ((int)(digest[i+2] & 0xC0)>>6)];
  170.         *p++ = basis_64[digest[i+2] & 0x3F];
  171.     }
  172.     *p-- = '\0';
  173.     *p-- = '=';
  174.     *p-- = '=';
  175.     return encodedDigest;
  176. }
  177.  
  178. char *md5digest(pool *p, FILE *infile)
  179. {
  180.     MD5_CTX context;
  181.     unsigned char buf[1000];
  182.     long length = 0;
  183.     int nbytes;
  184.  
  185.     MD5Init(&context);
  186.     while ((nbytes = fread(buf, 1, sizeof(buf), infile))) {
  187.         length += nbytes;
  188.         MD5Update(&context, buf, nbytes);
  189.     }
  190.     rewind(infile);
  191.     return md5contextTo64(p, &context);
  192. }
  193.  
  194.