home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / pwgenera.zip / hash.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  2KB  |  52 lines

  1. /* hash.c: The opiehash() library function.
  2.  
  3. Portions of this software are Copyright 1996 by Craig Metz, All Rights
  4. Reserved. The Inner Net Copyright Notice and License Agreement applies to
  5. these portions of the software.
  6.  
  7. Portions of this software are Copyright 1995 by Randall Atkinson and Dan
  8. McDonald, All Rights Reserved. All Rights under this copyright are assigned
  9. to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
  10. License Agreement applies to this software.
  11.  
  12.         History:
  13.  
  14.     Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
  15.                Renamed MDx functions to prevent conflicts. Changed unsigned
  16.                long to UINT4 for Alpha. Use unified context structure.
  17.         Created at NRL for OPIE 2.2 from opiesubr.c.
  18. */
  19. #include "opie_cfg.h"
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. #include "opie.h"
  25.  
  26. /*
  27.  * The one-way function f(x).
  28.  * Takes 8 bytes and returns 8 bytes in place.
  29.  * The value of "algorithm" determines whether MD4 or MD5 is used,
  30.  * where (algorithm==5) specifies MD5 and (algorithm==4) specifies MD4.
  31.  */
  32. VOIDRET opiehash FUNCTION((x, algorithm), char *x AND unsigned algorithm)
  33. {
  34.   struct opiemdx_ctx ctx;
  35.   UINT4 results[4];
  36.  
  37.   if (4 == algorithm) {
  38.     opiemd4init(&ctx);
  39.     opiemd4update(&ctx, (unsigned char *) x, 8);
  40.     opiemd4final((unsigned char *) results, &ctx);
  41.   } else {
  42.     opiemd5init(&ctx);
  43.     opiemd5update(&ctx, (unsigned char *) x, 8);
  44.     opiemd5final((unsigned char *) results, &ctx);
  45.   }
  46.   /* Fold 128 to 64 bits */
  47.   results[0] ^= results[2];
  48.   results[1] ^= results[3];
  49.  
  50.   memcpy(x, (char *)results, 8);
  51. }
  52.