PHP - password_get_info() 函数



PHP 哈希password_get_info()函数用于检索给定哈希的信息。返回的信息将采用数组格式。

返回的数组包含三个元素“algo”、“algoName”和“options”,如下所述:

  • algo - 这是一个整数,表示用于创建哈希的算法,它与一个密码算法常量匹配。
  • algoName - 这是一个人类可读的字符串,描述用于哈希密码的算法。
  • options - 这包括调用password_hash()函数时提供的任何选项,例如成本参数或其他特定于算法的设置。

语法

以下是PHP 哈希password_get_info()函数的语法:

password_get_info(string $hash): array

参数

此函数接受一个名为“hash”的参数,如下所述:

  • hash - 由password_hash()函数创建的哈希。

返回值

此函数返回有关给定哈希的信息。

示例1

以下程序演示了PHP 哈希password_get_info()函数的用法:

<?php
   #password
   $passw01 = "53nh46u74m3nt3";
   $hashp01 = password_hash($passw01, PASSWORD_ARGON2ID);
   echo "The given password: $passw01";
   echo "\nPassword length: ".strlen($passw01);
   echo "\nThe hash is: ".$hashp01;
   echo "\nHash length: ".strlen($hashp01);

   $hashi01 = password_get_info($hashp01);
   echo "\nThe information about hash: ";
   echo "\n".$hashi01["algo"];
   echo "\n".$hashi01["algoName"];
   echo "\n".$hashi01["options"]["memory_cost"];
?>

输出

上述程序产生以下输出:

The given password: 53nh46u74m3nt3
Password length: 14
The hash is: $argon2id$v=19$m=65536,t=4,p=1$d1lQNFRXaHZ4c2svckVwSw$ycYE/MWu3Rc1oRa8VWV3ZqNS8i0QqWnnuWdQzjwsAZQ
Hash length: 97
The information about hash:
argon2id
argon2id
65536

示例2

以下是PHP 哈希password_get_info()函数的另一个示例。我们使用此函数来检索有关给定哈希的信息:

<?php
   $passw02 = "53nh46u74m3nt3"; 
   $opts02 = [ "memory_cost" => 512, "time_cost" => 3, "threads" => 3 ]; 
   $hashp02 = password_hash($passw02, PASSWORD_ARGON2ID, $opts02); 
   echo "The given password: ".$passw02;
   echo "\nThe password length: ".strlen($passw02);
   echo "\nHash: ".$hashp02;
   echo "\nHash length: ".strlen($hashp02);
   $hashi02 = password_get_info($hashp02);
   echo "\nAn information about hash: ";
   echo "\nAlgo: ".$hashi02["algo"];
   echo "\nAlgo Name: ".$hashi02["algoName"];
   echo "\nMemory cost: ".$hashi02["options"]["memory_cost"];
   echo "\nTime cost: ".$hashi02["options"]["time_cost"];
   echo "\nThreads: ".$hashi02["options"]["threads"];
?>

输出

执行上述程序后,将显示以下输出:

The given password: 53nh46u74m3nt3
The password length: 14
Hash: $argon2id$v=19$m=512,t=3,p=3$SC4yMWZ0TUIxaTJ3TnVBYQ$JCztOuIo34OX0OEmtHzSnKLN/Zugk5buGm0k/tTFpbs
Hash length: 95
An information about hash:
Algo: argon2id
Algo Name: argon2id
Memory cost: 512
Time cost: 3
Threads: 3
php_function_reference.htm
广告