home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / varia / pgp / pgpamiga / source / passwd.c < prev    next >
C/C++ Source or Header  |  1994-06-08  |  3KB  |  90 lines

  1. /*      passwd.c - Password reading/hashing routines
  2.         (c) 1989 Philip Zimmermann.  All rights reserved.
  3.         Implemented in Microsoft C.
  4.         Routines for getting a pass phrase from the user's console.
  5. */
  6.  
  7. #include        <stdio.h>       /* for fprintf() */
  8. #include        <ctype.h>       /* for isdigit(), toupper(), etc. */
  9. #include        <string.h>      /* for strlen() */
  10.  
  11. #include        "random.h"      /* for getstring() */
  12. #include        "md5.h"
  13. #include        "language.h"
  14. #include        "pgp.h"
  15. #include        "system.h"
  16.  
  17. #define MAXKEYLEN 254   /* max byte length of pass phrase */
  18.  
  19. boolean showpass = FALSE;
  20.  
  21. /*
  22. **      hashpass - Hash pass phrase down to 128 bits (16 bytes).
  23. **  keylen must be less than 1024.
  24. **      Use the MD5 algorithm.
  25. */
  26. void hashpass (char *keystring, int keylen, byte *hash)
  27. {
  28.         MD5_CTX mdContext;
  29.  
  30.         /* Calculate the hash */
  31.         MD5Init(&mdContext);
  32.         MD5Update(&mdContext, (unsigned char *) keystring, keylen);
  33.         MD5Final(hash, &mdContext);
  34. }       /* hashpass */
  35.  
  36.  
  37. /*
  38. **      GetHashedPassPhrase - get pass phrase from user, hashes it to an IDEA key.
  39.         Parameters:
  40.                 returns char *keystring as the pass phrase itself
  41.                 return char *hash as the 16-byte hash of the pass phrase
  42.                                 using MD5.
  43.                 byte noecho:
  44.                         0=ask once, echo.
  45.                         1=ask once, no echo.
  46.                         2=ask twice, no echo.
  47.         Return 0 if no characters are input, else return 1.
  48.         If we return 0, the hashed key will not be useful.
  49. */
  50. int GetHashedPassPhrase(char *hash, boolean noecho)
  51. {
  52.         char keystr1[MAXKEYLEN+1], keystr2[MAXKEYLEN+1];
  53.         int len;
  54.  
  55.         if (showpass)
  56.                 noecho = 0;
  57.  
  58.         while (TRUE) {
  59.                 fprintf(pgpout,PSTR("\nEnter pass phrase: "));
  60. #ifdef AMIGA
  61.                 requesterdesc = "Please enter pass phrase:";
  62. #endif
  63.                 getstring(keystr1,MAXKEYLEN-1,!noecho);
  64.                 if (noecho<2)   /* no need to ask again if user can see it */
  65.                         break;
  66.                 fprintf(pgpout,PSTR("\nEnter same pass phrase again: "));
  67.                 getstring(keystr2,MAXKEYLEN-1,!noecho);
  68.                 if (strcmp(keystr1,keystr2)==0)
  69.                         break;
  70.                 fprintf(pgpout,PSTR("\n\007Error: Pass phrases were different.  Try again."));
  71.                 memset(keystr2, 0, sizeof(keystr2));
  72.         }
  73. #ifdef AMIGA
  74.         requesterdesc = NULL;
  75. #endif
  76.  
  77.         if (noecho && (filter_mode || quietmode))
  78.                 putc('\n', pgpout);
  79.  
  80.         len = strlen(keystr1);
  81.         if (len == 0)
  82.                 return 0;
  83.         /* We assume ASCII pass phrases, with no charset conversions. */
  84.         /* This will have to change for EBCDIC */
  85.         hashpass (keystr1, strlen(keystr1), (byte *) hash);
  86.         memset(keystr1, 0, sizeof(keystr1));
  87.         return 1;
  88. }       /* GetHashedPassPhrase */
  89.  
  90.