Subversion Repositories php_utils

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. define('PASSWORD_STD_DES',  'std_des');
  4. define('PASSWORD_EXT_DES',  'ext_des');
  5. define('PASSWORD_MD5',      'md5');
  6. define('PASSWORD_BLOWFISH', 'blowfish');
  7. define('PASSWORD_SHA256',   'sha256');
  8. define('PASSWORD_SHA512',   'sha512');
  9.  
  10. function des_compat_salt($salt_len) {
  11.         $characters = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  12.         $salt = '';
  13.         for ($i=0; $i<$salt_len; $i++) {
  14.                 $salt .= $characters[rand(0, strlen($characters)-1)]; // TODO: use rand() to make the RND cryptographical secure
  15.         }
  16.         return $salt;
  17. }
  18.  
  19. function base64_int_encode($num) {
  20.         // https://stackoverflow.com/questions/15534982/which-iteration-rules-apply-on-crypt-using-crypt-ext-des
  21.         $alphabet_raw='./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  22.         $alphabet=str_split($alphabet_raw);
  23.         $arr=array();
  24.         $base=sizeof($alphabet);
  25.         while($num) {
  26.                 $rem=$num % $base;
  27.                 $num=(int)($num / $base);
  28.                 $arr[]=$alphabet[$rem];
  29.         }
  30.         $string=implode($arr);
  31.         return str_pad($string, 4, '.', STR_PAD_RIGHT);
  32. }
  33.  
  34. /** This function extends password_hash() with the algorithms supported by crypt().
  35.  * The result can be verified using password_verify().
  36.  * @param string $password to be hashed
  37.  * @param mixed $algo algorithm
  38.  * @param array $options options for the hashing algorithm
  39.  * @return string Crypt compatible password hash
  40.  */
  41. function password_hash_ex($password, $algo, $options=array()) {
  42.         if (($algo === PASSWORD_STD_DES) && defined('CRYPT_STD_DES')) {
  43.                 // Standard DES-based hash with a two character salt from the alphabet "./0-9A-Za-z". Using invalid characters in the salt will cause crypt() to fail.
  44.                 $salt = des_compat_salt(2);
  45.                 return crypt($password, $salt);
  46.         } else if (($algo === PASSWORD_EXT_DES) && defined('CRYPT_EXT_DES')) {
  47.                 // Extended DES-based hash. The "salt" is a 9-character string consisting of an underscore followed by 4 characters of iteration count and 4 characters of salt. Each of these 4-character strings encode 24 bits, least significant character first. The values 0 to 63 are encoded as ./0-9A-Za-z. Using invalid characters in the salt will cause crypt() to fail.
  48.                 $iterations = isset($options['iterations']) ? $options['iterations'] : 725;
  49.                 $salt = '_' . base64_int_encode($iterations) . des_compat_salt(4);
  50.                 return crypt($password, $salt);
  51.         } else if (($algo === PASSWORD_MD5) && defined('CRYPT_MD5')) {
  52.                 // MD5 hashing with a twelve character salt starting with $1$
  53.                 $salt = '$1$'.des_compat_salt(12).'$';
  54.                 return crypt($password, $salt);
  55.         } else if (($algo === PASSWORD_BLOWFISH) && defined('CRYPT_BLOWFISH')) {
  56.                 // Blowfish hashing with a salt as follows: "$2a$", "$2x$" or "$2y$", a two digit cost parameter, "$", and 22 characters from the alphabet "./0-9A-Za-z". Using characters outside of this range in the salt will cause crypt() to return a zero-length string. The two digit cost parameter is the base-2 logarithm of the iteration count for the underlying Blowfish-based hashing algorithm and must be in range 04-31, values outside this range will cause crypt() to fail. "$2x$" hashes are potentially weak; "$2a$" hashes are compatible and mitigate this weakness. For new hashes, "$2y$" should be used.
  57.                 $algo = '$2y$'; // most secure
  58.                 $cost = isset($options['cost']) ? $options['cost'] : 10;
  59.                 $salt = $algo.str_pad($cost,2,'0',STR_PAD_LEFT).'$'.des_compat_salt(22).'$';
  60.                 return crypt($password, $salt);
  61.         } else if (($algo === PASSWORD_SHA256) && defined('CRYPT_SHA256')) {
  62.                 // SHA-256 hash with a sixteen character salt prefixed with $5$. If the salt string starts with 'rounds=<N>$', the numeric value of N is used to indicate how many times the hashing loop should be executed, much like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to the nearest limit.
  63.                 $algo = '$5$';
  64.                 $rounds = isset($options['rounds']) ? $options['rounds'] : 5000;
  65.                 $salt = $algo.'rounds='.$rounds.'$'.des_compat_salt(16).'$';
  66.                 return crypt($password, $salt);
  67.         } else if (($algo === PASSWORD_SHA512) && defined('CRYPT_SHA512')) {
  68.                 // SHA-512 hash with a sixteen character salt prefixed with $6$. If the salt string starts with 'rounds=<N>$', the numeric value of N is used to indicate how many times the hashing loop should be executed, much like the cost parameter on Blowfish. The default number of rounds is 5000, there is a minimum of 1000 and a maximum of 999,999,999. Any selection of N outside this range will be truncated to the nearest limit.
  69.                 $algo = '$6$';
  70.                 $rounds = isset($options['rounds']) ? $options['rounds'] : 5000;
  71.                 $salt = $algo.'rounds='.$rounds.'$'.des_compat_salt(16).'$';
  72.                 return crypt($password, $salt);
  73.         } else {
  74.                 // $algo === PASSWORD_DEFAULT
  75.                 // $algo === PASSWORD_BCRYPT
  76.                 // $algo === PASSWORD_ARGON2I
  77.                 // $algo === PASSWORD_ARGON2ID
  78.                 return password_hash($password, $algo, $options);
  79.         }
  80. }
  81.