コード例 #1
0
 public static function Create($action, $expire = UL_NONCE_EXPIRE, $persistent = false)
 {
     $code = ulUtils::RandomBytes(16, true);
     $hashed_code = hash(UL_HMAC_FUNC, $code);
     if ($persistent === true) {
         self::StorePersistent($action, $hashed_code, $expire);
     } else {
         self::StoreVolatile($action, $hashed_code, $expire);
     }
     return $code;
 }
コード例 #2
0
function generate_keys()
{
    for ($i = 0; $i < 10; ++$i) {
        $key = ulUtils::RandomBytes(42, true);
        echo "{$key}<br>";
    }
}
コード例 #3
0
 public static function Hash($password, $salt = '')
 {
     if (ulUtils::BeginsWith($salt, '{SSHA}')) {
         $salt = substr($salt, 6);
         if ($salt == '') {
             $salt = ulUtils::RandomBytes(24, true);
         }
         return '{SSHA}' . base64_encode(pack('H*', sha1($password . $salt)) . $salt);
     } else {
         if (ulUtils::BeginsWith($salt, '{SHA}')) {
             return '{SHA}' . base64_encode(pack('H*', sha1($password)));
         } else {
             if (ulUtils::BeginsWith($salt, '{SMD5}')) {
                 $salt = substr($salt, 6);
                 if ($salt == '') {
                     $salt = ulUtils::RandomBytes(24, true);
                 }
                 return '{SMD5}' . base64_encode(pack('H*', md5($password . $salt)) . $salt);
             } else {
                 if (ulUtils::BeginsWith($salt, '{MD5}')) {
                     return '{MD5}' . base64_encode(pack('H*', md5($password)));
                 } else {
                     if (ulUtils::BeginsWith($salt, '{CRYPT}')) {
                         $salt = substr($salt, 7);
                         if ($salt == '') {
                             $salt = self::BCryptSalt();
                         }
                         // We must not preprocess here to stay compatible with other applications
                         return '{CRYPT}' . crypt($password, $salt);
                     } else {
                         if (ulUtils::BeginsWith($salt, '{PBKDF2}')) {
                             $hash_bytes = 96;
                             $hash_algo = UL_HMAC_FUNC;
                             $hash_rounds = pow(2, UL_PWD_ROUNDS);
                             $salt = substr($salt, 8);
                             if ($salt == '') {
                                 $salt = ulUtils::RandomBytes(16, true);
                             } else {
                                 $parts = explode(':', $salt);
                                 $hash_algo = $parts[0];
                                 $hash_rounds = $parts[1];
                                 $salt = $parts[2];
                             }
                             // We must not preprocess here to stay compatible with other applications
                             return '{PBKDF2}' . $hash_algo . ':' . $hash_rounds . ':' . $salt . ':' . base64_encode(pbkdf2($hash_algo, $password, $salt, $hash_rounds, $hash_bytes, true));
                         } else {
                             // For compatibility with older versions, an empty string is the same as '{BCRYPT}'
                             if (ulUtils::BeginsWith($salt, '{BCRYPT}')) {
                                 $salt = substr($salt, 8);
                             }
                             if ($salt == '') {
                                 $salt = self::BCryptSalt();
                             }
                             return crypt(self::PreProcess($password), $salt);
                         }
                     }
                 }
             }
         }
     }
 }