lib/misc/pwcrypt.cc
changeset 2 b3afb9f1e801
parent 0 6f7a81934006
equal deleted inserted replaced
1:30113bfbe723 2:b3afb9f1e801
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
     1 // Copyright (C) 1999,2000,2005 Bruce Guenter <bruceg@em.ca>
     2 //
     2 //
     3 // This program is free software; you can redistribute it and/or modify
     3 // This program is free software; you can redistribute it and/or modify
     4 // it under the terms of the GNU General Public License as published by
     4 // it under the terms of the GNU General Public License as published by
     5 // the Free Software Foundation; either version 2 of the License, or
     5 // the Free Software Foundation; either version 2 of the License, or
     6 // (at your option) any later version.
     6 // (at your option) any later version.
    28 
    28 
    29 #ifndef HAVE_RANDOM
    29 #ifndef HAVE_RANDOM
    30 long int random(void);
    30 long int random(void);
    31 #endif
    31 #endif
    32 
    32 
    33 #ifdef USE_CRYPT
       
    34 
       
    35 static const char passwd_table[65] =
       
    36 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
       
    37 
       
    38 bool crypt_cmp(const mystring& pass, const mystring& stored)
       
    39 {
       
    40   if(!stored || !pass)
       
    41     return false;
       
    42   const char* encrypted = crypt(pass.c_str(), stored.c_str());
       
    43   return stored == encrypted;
       
    44 }
       
    45 
       
    46 const char* pwcrypt(const mystring& pass)
       
    47 {
       
    48   char salt[2] = {
       
    49     passwd_table[random() % 64],
       
    50     passwd_table[random() % 64]
       
    51   };
       
    52   return crypt(pass.c_str(), salt);
       
    53 }
       
    54 
       
    55 #else // USE_CRYPT
       
    56 
       
    57 static const char passwd_table[65] =
       
    58 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
       
    59 
       
    60 extern "C"
    33 extern "C"
    61 {
    34 {
    62 #include "md5.h"
    35 #include "md5.h"
    63 };
    36 };
    64 
    37 
    83   return out;
    56   return out;
    84 }
    57 }
    85 
    58 
    86 extern "C" char *md5_crypt __P ((const char *key, const char *salt));
    59 extern "C" char *md5_crypt __P ((const char *key, const char *salt));
    87 
    60 
       
    61 const char* null_crypt(const mystring& pass)
       
    62 {
       
    63   static mystring s;
       
    64   s = "$0$" + pass;
       
    65   return s.c_str();
       
    66 }
       
    67 
    88 bool crypt_cmp(const mystring& pass, const mystring& stored)
    68 bool crypt_cmp(const mystring& pass, const mystring& stored)
    89 {
    69 {
    90   if(!stored || !pass)
    70   if(!stored || !pass)
    91     return false;
    71     return false;
    92   const char* encrypted;
    72   const char* encrypted;
    93   if(stored.length() == 33 && stored[0] == OLD_MD5_CODE)
    73   if(stored.length() == 33 && stored[0] == OLD_MD5_CODE)
    94     encrypted = encrypt_old_md5(pass.c_str());
    74     encrypted = encrypt_old_md5(pass.c_str());
    95   else if(stored[0] == '$' && stored[1] == '1' && stored[2] == '$')
    75   else if(stored[0] == '$' && stored[1] == '1' && stored[2] == '$')
    96     encrypted = md5_crypt(pass.c_str(), stored.c_str());
    76     encrypted = md5_crypt(pass.c_str(), stored.c_str());
       
    77   else if(stored[0] == '$' && stored[1] == '0' && stored[2] == '$')
       
    78     encrypted = null_crypt(pass);
    97   else
    79   else
    98     encrypted = crypt(pass.c_str(), stored.c_str());
    80     encrypted = crypt(pass.c_str(), stored.c_str());
    99   return stored == encrypted;
    81   return stored == encrypted;
   100 }
    82 }
       
    83 
       
    84 #ifdef USE_CRYPT
       
    85 
       
    86 static const char passwd_table[65] =
       
    87 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
       
    88 
       
    89 const char* pwcrypt(const mystring& pass)
       
    90 {
       
    91   char salt[2] = {
       
    92     passwd_table[random() % 64],
       
    93     passwd_table[random() % 64]
       
    94   };
       
    95   return crypt(pass.c_str(), salt);
       
    96 }
       
    97 
       
    98 #else
       
    99 
       
   100 static const char passwd_table[65] =
       
   101 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
   101 
   102 
   102 const char* pwcrypt(const mystring& pass)
   103 const char* pwcrypt(const mystring& pass)
   103 {
   104 {
   104   char salt[14] = "$1$";
   105   char salt[14] = "$1$";
   105   for(unsigned i = 3; i < 11; i++)
   106   for(unsigned i = 3; i < 11; i++)
   106     salt[i] = passwd_table[random() % 64];
   107     salt[i] = passwd_table[random() % 64];
   107   salt[12] = 0;
   108   salt[12] = 0;
   108   return md5_crypt(pass.c_str(), salt);
   109   return md5_crypt(pass.c_str(), salt);
   109 }
   110 }
   110 
   111 
   111 #endif // USE_MD5
   112 #endif