openssl_password_hash
OpenSSL の Argon2 実装を使ってパスワードハッシュを作る
説明
string openssl_password_hash(string $algo, string $password, array $options = [])
この関数は、PHP が Argon2 をサポートする OpenSSL
(HAVE_OPENSSL_ARGON2)
とともにコンパイルされている場合にのみ使用できます。
パラメータ
-
algo
-
パスワードハッシュアルゴリズム。サポートされている値は
"argon2id" と "argon2i" です。
-
password
-
ユーザーのパスワード。
-
options
-
オプションの連想配列 (array)。サポートされるキーは以下のとおりです:
-
memory_cost - ハッシュの計算に使用する
最大メモリ量 (KiB 単位)
-
time_cost - ハッシュの計算にかける
最大時間
-
threads - ハッシュの計算に使用する
スレッド数
戻り値
パスワードハッシュを文字列 (string) で返します。
エラー / 例外
algo がサポートされている値
("argon2i" または "argon2id")
のいずれでもない場合、ValueError をスローします。
不明な原因でハッシュ操作が失敗した場合、
Error をスローします。
例
例1 openssl_password_hash の例
<?php
$hash = openssl_password_hash('argon2id', 'my-secret-password');
echo $hash;
?>
$argon2id$v=19$m=65536,t=4,p=1$c29tZXNhbHR2YWx1ZQ$hashvalue...
例2 カスタムオプションを指定した openssl_password_hash の例
<?php
$hash = openssl_password_hash('argon2id', 'my-secret-password', [
'memory_cost' => 65536,
'time_cost' => 4,
'threads' => 1,
]);
?>
参考
- openssl_password_verify
- password_hash