(PHP 5 >= 5.5.0, PHP 7)
password_hash — Erstellt einen Passwort-Hash
$password
, integer $algo
[, array $options
] )password_hash() erstellt einen neuen Passwort-Hash und benutzt dabei einen starken Einweg-Hashing-Algorithmus.
Die folgenden Algorithmen werden zur Zeit unterstützt:
PASSWORD_DEFAULT
- Benutzt den bcrypt-Algorithmus (Standard in PHP 5.5.0).
Beachte, dass sich diese Konstante mit der Zeit ändern wird, wenn stärkere Algorithmen in PHP implementiert werden. Aus diesem Grund kann sich die Länge des zurückgegebenen Strings mit der Zeit ändern. Es wird deshalb empfohlen das Ergebnis in einem Datenbankfeld zu speichern, das mehr als 60 Zeichen speichern kann. (z.B. 255 Zeichen).
PASSWORD_BCRYPT
- Benutzt den CRYPT_BLOWFISH
-Algorithmus zum Erstellen des Hashes. Dies erstellt einen crypt()-kompatiblen Hash und benutzt die "$2y$"-Kennung. Es wird immer ein 60 Zeichen langer String zurückgegeben, Im Fehlerfall wird FALSE
zurückgegeben..
Unterstützte Optionen:
salt - to manually provide a salt to use when hashing the password. Note that this will override and prevent a salt from being automatically generated.
If omitted, a random salt will be generated by password_hash() for each password hashed. This is the intended mode of operation.
cost - which denotes the algorithmic cost that should be used. Examples of these values can be found on the crypt() page.
If omitted, a default value of 10 will be used. This is a good baseline cost, but you may want to consider increasing it depending on your hardware.
password
Das Passwort des Benutzers.
algo
Eine Konstante für den Passwort-Algorithmus, die den Algorithmus zum hashen des Passwortes angibt.
options
Ein assoziatives Array mit Optionen. Siehe auch Konstanten für Passwort-Algorithmen für Informationen zu den von dein jeweiligen Algorithmen unterstützten Optionen.
If omitted, a random salt will be created and the default cost will be used.
Returns the hashed password, Im Fehlerfall wird FALSE
zurückgegeben..
Beispiel #1 password_hash() example
<?php
/**
* We just want to hash our password using the current DEFAULT algorithm.
* This is presently BCRYPT, and will produce a 60 character result.
*
* Beware that DEFAULT may change over time, so you would want to prepare
* By allowing your storage to expand past 60 characters (255 would be good)
*/
echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT)."\n";
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
$2y$10$.vGA1O9wmRjrwAVXD98HNOgsNpDczlqm3Jq7KnEd1rVAGv3Fykk1a
Beispiel #2 password_hash() example setting cost manually
<?php
/**
* In this case, we want to increase the default cost for BCRYPT to 12.
* Note that we also switched to BCRYPT, which will always be 60 characters.
*/
$options = [
'cost' => 12,
];
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."\n";
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
$2y$12$QjSH496pcT5CEbzjD/vtVeH03tfHKFy36d4J0Ltp3lRtee9HDxY3K
Beispiel #3 password_hash() example setting salt manually
<?php
/**
* Note that the salt here is randomly generated.
* Never use a static salt or one that is not randomly generated.
*
* For the VAST majority of use-cases, let password_hash generate the salt randomly for you
*/
$options = [
'cost' => 11,
'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
];
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."\n";
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
$2y$11$q5MkhSBtlsJcNEVsYh64a.aCluzHnGog7TQAKVmQwO9C8xb.t89F.
Beispiel #4 password_hash() example finding a good cost
<?php
/**
* This code will benchmark your server to determine how high of a cost you can
* afford. You want to set the highest cost that you can without slowing down
* you server too much. 10 is a good baseline, and more is good if your servers
* are fast enough.
*/
$timeTarget = 0.2;
$cost = 9;
do {
$cost++;
$start = microtime(true);
password_hash("test", PASSWORD_BCRYPT, ["cost" => $cost]);
$end = microtime(true);
} while (($end - $start) < $timeTarget);
echo "Appropriate Cost Found: " . $cost . "\n";
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
Appropriate Cost Found: 11
It is strongly recommended that you do not generate your own salt for this function. It will create a secure salt automatically for you if you do not specify one.
Hinweis:
It is recommended that you should test this function on your servers, and adjust the cost parameter so that execution of the function takes approximately 0.1 to 0.5 seconds. The script in the above example will help you choose a good cost value for your hardware.
Hinweis: Updates to supported algorithms by this function (or changes to the default one) must follow the follwoing rules:
- Any new algorithm must be in core for at least 1 full release of PHP prior to becoming default. So if, for example, a new algorithm is added in 5.5.5, it would not be eligible for default until 5.7 (since 5.6 would be the first full release). But if a different algorithm was added in 5.6.0, it would also be eligible for default at 5.7.0.
- The default should only change on a full release (5.6.0, 6.0.0, etc) and not on a revision release. The only exception to this is in an emergency when a critical security flaw is found in the current default.