home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 November / PCO_1198.ISO / filesbbs / os2 / fn127os2.arj / FN127OS2.ZIP / fn127os2 / src / md5_calc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-25  |  2.6 KB  |  87 lines

  1. /*
  2.  # $Id: md5_calc.c,v 1.6 1998/04/10 10:27:18 fbm Exp fbm $
  3.  # Copyright (C) 1997,1998 Farrell McKay
  4.  # All rights reserved.
  5.  #
  6.  # This file is part of the Fortify distribution, a toolkit for
  7.  # upgrading the cryptographic strength of the Netscape Navigator
  8.  # web browser, authored by Farrell McKay.
  9.  #
  10.  # This toolkit is provided to the recipient under the
  11.  # following terms and conditions:-
  12.  #   1.  This copyright notice must not be removed or modified.
  13.  #   2.  This toolkit may not be reproduced or included in any commercial
  14.  #       media distribution, or commercial publication (for example CD-ROM,
  15.  #       disk, book, magazine, journal) without first obtaining the author's
  16.  #       express permission.
  17.  #   3.  This toolkit, or any component of this toolkit, may not be
  18.  #       commercially resold, redeveloped, rewritten, enhanced or otherwise
  19.  #       used as the basis for commercial venture, without first obtaining
  20.  #       the author's express permission.
  21.  #   4.  Subject to the above conditions being observed (1-3), this toolkit
  22.  #       may be freely reproduced or redistributed.
  23.  #   5.  This software is provided "as-is", without express or implied
  24.  #       warranty.  In no event shall the author be liable for any direct,
  25.  #       indirect or consequential damages however caused.
  26.  #   6.  Subject to the above conditions being observed (1-5),
  27.  #       this toolkit may be used at no cost to the recipient.
  28.  #
  29.  # Farrell McKay
  30.  # Wayfarer Systems Pty Ltd        contact@fortify.net
  31.  */
  32.  
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <unistd.h>
  36. #include "md5.h"
  37. #include "spans.h"
  38. #include "trace.h"
  39.  
  40. #define BUFSIZE 1024*16
  41.  
  42. static int
  43. md5_calc_handler(int start, int stop, void *p1, void *p2, void *p3)
  44. {
  45.     int            i, posn, nb;
  46.     int            fd = (int) p1;
  47.         static unsigned char    buf[BUFSIZE];
  48.     MD5_CTX            *c = (MD5_CTX *) p2;
  49.  
  50.     trace(4, ("t4>> md5_calc span %#x-%#x\n", start, stop));
  51.  
  52.     posn = (long) lseek(fd, (off_t) start, SEEK_SET);
  53.  
  54.     while (posn < stop || stop < 0) {
  55.         nb = sizeof(buf);
  56.         if (stop > 0 && stop - posn < nb)
  57.             nb = stop - posn;
  58.         i = read(fd, buf, nb);
  59.         if (i <= 0)
  60.             break;
  61.         MD5_Update(c, buf, (unsigned long)i);
  62.         posn += i;
  63.     }
  64.     return 0;
  65. }
  66.  
  67. char *
  68. md5_calc(int fd, char *span)
  69. {
  70.         int                     i;
  71.         MD5_CTX                 c;
  72.         unsigned char           *p, md[MD5_DIGEST_LENGTH];
  73.         static unsigned char    buf[BUFSIZE];
  74.  
  75.     lseek(fd, 0L, SEEK_SET);
  76.  
  77.     MD5_Init(&c);
  78.  
  79.     do_spans(span, md5_calc_handler, (void *) fd, (void *) &c, NULL);
  80.  
  81.     MD5_Final(&(md[0]),&c);
  82.  
  83.         for (i = 0, p = buf; i < MD5_DIGEST_LENGTH; i++, p += 2)
  84.                 sprintf(p, "%02x", md[i]);
  85.         return buf;
  86. }
  87.