home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume38 / shadow / part12 / encrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-14  |  2.1 KB  |  99 lines

  1. /*
  2.  * Copyright 1990, 1993, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  *
  11.  * This software is provided on an AS-IS basis and the author makes
  12.  * no warrantee of any kind.
  13.  */
  14.  
  15. #include <string.h>
  16. #include "config.h"
  17.  
  18. #ifndef lint
  19. static    char    sccsid[] = "@(#)encrypt.c    3.5    07:45:28    22 Apr 1993";
  20. #endif
  21.  
  22. extern    char    *crypt();
  23.  
  24. char *
  25. pw_encrypt (clear, salt)
  26. char    *clear;
  27. char    *salt;
  28. {
  29. #ifdef    SW_CRYPT
  30.     static    char    cipher[128];
  31. #else
  32.     static    char    cipher[32];
  33. #endif
  34.     static    int    count;
  35.     char    newsalt[2];
  36.     char    *cp;
  37.     long    now;
  38.  
  39.     /*
  40.      * See if a new salt is needed and get a few random
  41.      * bits of information.  The amount of randomness is
  42.      * probably not all that crucial since the salt only
  43.      * serves to thwart a dictionary attack.
  44.      */
  45.  
  46.     if (salt == (char *) 0) {
  47.         now = time ((long *) 0) + count++;
  48.         now ^= clock ();
  49.         now ^= getpid ();
  50.         now = ((now >> 12) ^ (now)) & 07777;
  51.         newsalt[0] = i64c ((now >> 6) & 077);
  52.         newsalt[1] = i64c (now & 077);
  53.         salt = newsalt;
  54.     }
  55. #ifdef    SW_CRYPT
  56.     /*
  57.      * Copy over the salt.  It is always the first two
  58.      * characters of the string.
  59.      */
  60.  
  61.     cipher[0] = salt[0];
  62.     cipher[1] = salt[1];
  63.     cipher[2] = '\0';
  64.  
  65.     /*
  66.      * Loop up to ten times on the cleartext password.
  67.      * This is because the input limit for passwords is
  68.      * 80 characters.
  69.      *
  70.      * The initial salt is that provided by the user, or the
  71.      * one generated above.  The subsequent salts are gotten
  72.      * from the first two characters of the previous encrypted
  73.      * block of characters.
  74.      */
  75.  
  76.     for (count = 0;count < 10;count++) {
  77.         cp = crypt (clear, salt);
  78.         strcat (cipher, cp + 2);
  79.         salt = cipher + 11 * count + 2;
  80.  
  81.         if (strlen (clear) > 8)
  82.             clear += 8;
  83.         else
  84.             break;
  85.     }
  86. #else
  87.     cp = crypt (clear, salt);
  88.     strcpy (cipher, cp);
  89.  
  90. #ifdef    DOUBLESIZE
  91.     if (strlen (clear) > 8) {
  92.         cp = crypt (clear + 8, salt);
  93.         strcat (cipher, cp + 2);
  94.     }
  95. #endif    /* DOUBLESIZE */
  96. #endif    /* SW_CRYPT */
  97.     return cipher;
  98. }
  99.