home *** CD-ROM | disk | FTP | other *** search
/ Dream 49 / Amiga_Dream_49.iso / beos / utils / skey-1.000 / SKey-1.4.3 / Source / Sources / SKeySubr.cc < prev    next >
C/C++ Source or Header  |  1997-04-14  |  2KB  |  97 lines

  1. #include "md4.h"
  2. #include "md5.h"
  3. #include "skey.h"
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. /*
  9.  * Crunch a key:
  10.  * concatenate the seed and the password, run through MD4 and
  11.  * collapse to 64 bits. This is defined as the user's starting key.
  12.  */
  13. int md4_keycrunch(char *result, char *seed, char *passwd)
  14. {
  15.     char *buf;
  16.     MD4_CTX md;
  17.     unsigned long results[4];
  18.     unsigned int buflen;
  19.     
  20.     buflen = strlen(seed) + strlen(passwd);
  21.     if((buf = (char *)malloc(buflen+1)) == NULL)
  22.         return -1;
  23.     strcpy(buf, seed);
  24.     strcat(buf, passwd);
  25.  
  26.     /* Crunch the key through MD4 */
  27.     MD4Init(&md);
  28.     MD4Update(&md, (unsigned char *)buf, buflen);
  29.     MD4Final((unsigned char *)results, &md);
  30.     free(buf);
  31.  
  32.     results[0] ^= results[2];
  33.     results[1] ^= results[3];
  34.  
  35.     memcpy(result, (char *)results,8);
  36.  
  37.     return 0;
  38. }
  39.  
  40. int md5_keycrunch(char *result, char *seed, char *passwd)
  41. {
  42.     char *buf;
  43.     MD5_CTX md;
  44.     unsigned long results[4];
  45.     unsigned int buflen;
  46.     
  47.     buflen = strlen(seed) + strlen(passwd);
  48.     if((buf = (char *)malloc(buflen+1)) == NULL)
  49.         return -1;
  50.     strcpy(buf, seed);
  51.     strcat(buf, passwd);
  52.  
  53.     /* Crunch the key through MD5 */
  54.     MD5Init(&md);
  55.     MD5Update(&md, (unsigned char *)buf, buflen);
  56.     MD5Final((unsigned char *)results, &md);
  57.     free(buf);
  58.  
  59.     results[0] ^= results[2];
  60.     results[1] ^= results[3];
  61.  
  62.     memcpy(result, (char *)results,8);
  63.  
  64.     return 0;
  65. }
  66.  
  67. /* The one-way function f(). Takes 8 bytes and returns 8 bytes in place */
  68. void md4_f(char *x)
  69. {
  70.     MD4_CTX md;
  71.     unsigned long results[4];
  72.  
  73.     MD4Init(&md);
  74.     MD4Update(&md,(unsigned char *)x,8);
  75.     MD4Final((unsigned char *)results,&md);
  76.     /* Fold 128 to 64 bits */
  77.     results[0] ^= results[2];
  78.     results[1] ^= results[3];
  79.  
  80.     memcpy(x,(char *)results,8);
  81. }
  82.  
  83. void md5_f(char *x)
  84. {
  85.     MD5_CTX md;
  86.     unsigned long results[4];
  87.  
  88.     MD5Init(&md);
  89.     MD5Update(&md,(unsigned char *)x,8);
  90.     MD5Final((unsigned char *)results,&md);
  91.     /* Fold 128 to 64 bits */
  92.     results[0] ^= results[2];
  93.     results[1] ^= results[3];
  94.  
  95.     memcpy(x,(char *)results,8);
  96. }
  97.