Since 2017, NIST recommends using a secret input when hashing memorized secrets such as passwords. By mixing in a secret input (commonly called a "pepper"), one prevents an attacker from brute-forcing the password hashes altogether, even if they have the hash and salt. For example, an SQL injection typically affects only the database, not files on disk, so a pepper stored in a config file would still be out of reach for the attacker. A pepper must be randomly generated once and can be the same for all users. Many password leaks could have been made completely useless if site owners had done this.
Since there is no pepper parameter for password_hash (even though Argon2 has a "secret" parameter, PHP does not allow to set it), the correct way to mix in a pepper is to use hash_hmac(). The "add note" rules of php.net say I can't link external sites, so I can't back any of this up with a link to NIST, Wikipedia, posts from the security stackexchange site that explain the reasoning, or anything... You'll have to verify this manually. The code:
// config.conf
pepper=c1isvFdxMDdmjOlvxpecFw
<?php
// register.php
$pepper = getConfigVariable("pepper");
$pwd = $_POST['password'];
$pwd_peppered = hash_hmac("sha256", $pwd, $pepper);
$pwd_hashed = password_hash($pwd_peppered, PASSWORD_ARGON2ID);
add_user_to_database($username, $pwd_hashed);
?>
<?php
// login.php
$pepper = getConfigVariable("pepper");
$pwd = $_POST['password'];
$pwd_peppered = hash_hmac("sha256", $pwd, $pepper);
$pwd_hashed = get_pwd_from_db($username);
if (password_verify($pwd_peppered, $pwd_hashed)) {
echo "Password matches.";
}
else {
echo "Password incorrect.";
}
?>
Note that this code contains a timing attack that leaks whether the username exists. But my note was over the length limit so I had to cut this paragraph out.
Also note that the pepper is useless if leaked or if it can be cracked. Consider how it might be exposed, for example different methods of passing it to a docker container. Against cracking, use a long randomly generated value (like in the example above), and change the pepper when you do a new install with a clean user database. Changing the pepper for an existing database is the same as changing other hashing parameters: you can either wrap the old value in a new one and layer the hashing (more complex), you compute the new password hash whenever someone logs in (leaving old users at risk, so this might be okay depending on what the reason is that you're upgrading).
Why does this work? Because an attacker does the following after stealing the database:
password_verify("a", $stolen_hash)
password_verify("b", $stolen_hash)
...
password_verify("z", $stolen_hash)
password_verify("aa", $stolen_hash)
etc.
(More realistically, they use a cracking dictionary, but in principle, the way to crack a password hash is by guessing. That's why we use special algorithms: they are slower, so each verify() operation will be slower, so they can try much fewer passwords per hour of cracking.)
Now what if you used that pepper? Now they need to do this:
password_verify(hmac_sha256("a", $secret), $stolen_hash)
Without that $secret (the pepper), they can't do this computation. They would have to do:
password_verify(hmac_sha256("a", "a"), $stolen_hash)
password_verify(hmac_sha256("a", "b"), $stolen_hash)
...
etc., until they found the correct pepper.
If your pepper contains 128 bits of entropy, and so long as hmac-sha256 remains secure (even MD5 is technically secure for use in hmac: only its collision resistance is broken, but of course nobody would use MD5 because more and more flaws are found), this would take more energy than the sun outputs. In other words, it's currently impossible to crack a pepper that strong, even given a known password and salt.password_hash
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
password_hash
Референца за `function.password-hash.php` со подобрена типографија и навигација.
password_hash
Распакување на вгнездени низи
password_hash — Креира хеш на лозинка
= NULL
$password, string|int|null $algo, array $options = []): stringpassword_hash() креира нов хеш на лозинка користејќи силен еднонасочен алгоритам за хеширање.
Следниве алгоритми се моментално поддржани:
-
PASSWORD_DEFAULT- Користете го bcrypt алгоритамот (стандарден од PHP 5.5.0). Имајте предвид дека овој констант е дизајниран да се менува со текот на времето како што се додаваат нови и посилни алгоритми во PHP. Од таа причина, должината на резултатот од користењето на овој идентификатор може да се промени со текот на времето. Затоа, се препорачува резултатот да се чува во база на податоци што може да се прошири надвор од 60 бајти (255 бајти би биле добар избор). -
PASSWORD_BCRYPT- Користете го bcrypt алгоритамот за креирање на хешот. Ова ќе произведе стандарден crypt() компатибилен хеш користејќи го$2y$identifier. -
PASSWORD_ARGON2I- Користете го Argon2i алгоритамот за хеширање за креирање на хешот. Овој алгоритам е достапен само ако PHP е компајлиран со поддршка за Argon2. -
PASSWORD_ARGON2ID- Користете го Argon2id алгоритамот за хеширање за креирање на хешот. Овој алгоритам е достапен само ако PHP е компајлиран со поддршка за Argon2.
Поддржани опции за PASSWORD_BCRYPT:
-
salt(string) - за рачно да се обезбеди сол што ќе се користи при хеширање на лозинката. Имајте предвид дека ова ќе ја надмине и ќе спречи автоматско генерирање на сол.Ако се изостави, случајна сол ќе биде генерирана од password_hash() за секоја лозинка хеширана. Ова е наменскиот начин на работа.
Ги ескејпува специјалните знаци во стринг за употреба во SQL изјаваОпцијата за сол е застарена. Сега е препорачливо едноставно да се користи солта што се генерира стандардно. Од PHP 8.0.0, експлицитно дадена сол се игнорира.
-
cost(int) - што го означува алгоритмот за трошоци што треба да се користи. Примери за овие вредности може да се најдат на crypt() page.Ако се изостави, стандардна вредност од
12ќе се користи. Ова е добра основна цена, но треба да се прилагоди во зависност од користениот хардвер.
Поддржани опции за PASSWORD_ARGON2I
and PASSWORD_ARGON2ID:
-
memory_cost(int) - Максимална меморија (во кибибајти) што може да се користи за пресметување на Argon2 хешот. Стандардно еPASSWORD_ARGON2_DEFAULT_MEMORY_COST. -
time_cost(int) - Максимално време што може да потрае за пресметување на Argon2 хешот. Стандардно еPASSWORD_ARGON2_DEFAULT_TIME_COST. -
threads(int) - Број на нишки што ќе се користат за пресметување на Argon2 хешот. Стандардно еPASSWORD_ARGON2_DEFAULT_THREADS.Ги ескејпува специјалните знаци во стринг за употреба во SQL изјаваДостапно само кога PHP користи libargon2, не со имплементацијата на libsodium.
Параметри
password-
Лозинката на корисникот.
Безбедност: стандардниот сет на знациКористејќи го
PASSWORD_BCRYPTкако алгоритам, ќе резултира соpasswordпараметарот да биде скратен на максимална должина од 72 бајти. algo-
А , што ќе одговара на што го означува алгоритамот што треба да се користи при хеширање на лозинката.
options-
Асоцијативен низ што содржи опции. Погледнете ги константите за алгоритми за лозинки за документација за поддржаните опции за секој алгоритам.
Вратени вредности
Враќа хеширана лозинка.
Користениот алгоритам, цената и солта се враќаат како дел од хешот. Затоа, сите информации што се потребни за проверка на хешот се вклучени во него. Ова му овозможува на password_verify() функцијата да го провери хешот без потреба од посебно складирање за солта или информациите за алгоритмот.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.4.0 |
Стандардната вредност на cost опција на
PASSWORD_BCRYPT алгоритмот беше зголемен од
10 to 12.
|
| 8.3.0 | password_hash() сега го поставува основниот Random\RandomException како Exception::$previous исклучок кога ValueError се фрла поради неуспех во генерирањето на солта. |
| 8.0.0 |
password_hash() веќе не враќа false при неуспех, наместо тоа
ValueError ќе биде фрлен ако алгоритмот за хеширање на лозинката не е валиден, или Грешка ако хеширањето на лозинката не успеа поради непозната грешка.
|
| 8.0.0 |
На algo ако хеширањето на лозинката не успее поради непозната грешка.
|
| 7.4.0 |
На algo параметарот сега е nullable. string параметарот очекува
intсега, но сепак прифаќа
|
| 7.4.0 | Параметарот algo сега очекува стринг, но сè уште прифаќа цели броеви за компатибилност со претходните верзии. |
| 7.3.0 |
за компатибилност наназад. PASSWORD_ARGON2ID беше додадено.
|
| 7.2.0 |
Поддршка за Argon2id лозинки користејќи PASSWORD_ARGON2I беше додадено.
|
Примери
Пример #1 password_hash() example
<?php
echo password_hash("rasmuslerdorf", PASSWORD_DEFAULT);
?>Горниот пример ќе прикаже нешто слично на:
$2y$12$4Umg0rCJwMswRw/l.SwHvuQV01coP0eWmGzd61QH2RvAOMANUBGC.
Пример #2 password_hash() Поддршка за Argon2i лозинки користејќи
<?php
$options = [
// Increase the bcrypt cost from 12 to 13.
'cost' => 13,
];
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
?>Горниот пример ќе прикаже нешто слично на:
$2y$13$xeDfQumlmdm0Sco.4qmH1OGfUUmOcuRmfae0dPJhjX1Bq0yYhqbNi
Пример #3 password_hash() пример за рачно поставување на цена
пример за наоѓање добра цена
<?php
$timeTarget = 0.350; // 350 milliseconds
$cost = 11;
do {
$cost++;
$start = microtime(true);
password_hash("test", PASSWORD_BCRYPT, ["cost" => $cost]);
$end = microtime(true);
} while (($end - $start) < $timeTarget);
echo "Appropriate Cost Found: " . $cost - 1;
?>Горниот пример ќе прикаже нешто слично на:
Appropriate Cost Found: 13
Пример #4 password_hash() Овој код ќе го тестира машината за да одреди колку висока цена може да се користи без да се влоши корисничкото искуство. Се препорачува да се постави највисоката цена што не го забавува другите операции што машината треба да ги изврши. 11 е добра основна линија, а повеќе е подобро ако машината е доволно брза. Кодот подолу цели кон ≤ 350 милисекунди време на истегнување, што е соодветно задоцнување за системи што ракуваат со интерактивни најавувања.
<?php
echo 'Argon2i hash: ' . password_hash('rasmuslerdorf', PASSWORD_ARGON2I);
?>Горниот пример ќе прикаже нешто слично на:
Argon2i hash: $argon2i$v=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0
Белешки
пример користејќи Argon2i
Силно се препорачува да не се обезбедува експлицитна сол за оваа функција. Безбедна сол автоматски ќе се креира ако не е специфицирана сол. salt Како што е забележано погоре, обезбедувањето на
Забелешка:
опцијата во PHP 7.0.0 ќе генерира предупредување за депрекација. Поддршката за експлицитно обезбедување сол е отстранета во PHP 8.0.0.
Забелешка: Се препорачува оваа функција да се тестира на машината што се користи, прилагодувајќи ги параметрите за цена така што извршувањето на функцијата трае помалку од 350 милисекунди за интерактивни најавувања. Скриптата во горниот пример ќе помогне во изборот на соодветна bcrypt цена за дадената машина.
- Ажурирањата на поддржаните алгоритми од оваа функција (или промените во стандардниот) мора да ги следат следниве правила:
- Секој нов алгоритам мора да биде во јадрото најмалку 1 цело издание на PHP пред да стане стандарден. Значи, ако, на пример, нов алгоритам е додаден во 7.5.5, тој нема да биде подобен за стандарден до 7.7 (бидејќи 7.6 би било првото цело издание). Но, ако различен алгоритам беше додаден во 7.6.0, тој исто така би бил подобен за стандарден во 7.7.0.
Види Исто така
- password_verify() Стандардната вредност треба да се менува само во цело издание (7.3.0, 8.0.0, итн.) и не во ревизиско издание. Единствениот исклучок на ова е во случај на итност кога ќе се открие критична безбедносна пропустливост во тековната стандардна вредност.
- password_needs_rehash() - Проверува дали лозинката одговара на хашот
- crypt() - Хеширање на еднонасочна низа
- sodium_crypto_pwhash_str() - Добијте хеш кодиран во ASCII
Белешки од корисници 8 белешки
Similar to another post made here about the use of strings holding null-bytes within password_hash(), I wanted to be a little more precise, as we've had quite some issues now.
I've had a project of an application generating random hashes (CSPRN). What they've done is that they've used random_bytes(32), and the applied password_hash() to that obtained string, with the bcrypt algorithm.
This on one side led to the fact that sometimes, random_bytes() generated a string with null-bytes, actually resulting to an error in their call to password_hash() (PHP v 8.2.18). Thanks to that ("Bcrypt password must not contain a null character") I modified the the function generating random hashes to encoding the obtained binary random string with random_bytes() using bin2hex() (or base64 or whatever), to assure that the string to be hashed has no null-bytes.
I then just wanted to add that, when you use the bcrypt algorithm, make sure to remember that bcrypt truncates your password at 72 characters. When encoding your random string (e.g. generated using random_bytes()), this will convert your string from binary to hex representation, e.g. doubling its length. What you generally want is that your entire password is still contained within the 72 characters limit, to be sure that your entire "random information" gets hashes, and not only part of it.I agree with martinstoeckli,
don't create your own salts unless you really know what you're doing.
By default, it'll use /dev/urandom to create the salt, which is based on noise from device drivers.
And on Windows, it uses CryptGenRandom().
Both have been around for many years, and are considered secure for cryptography (the former probably more than the latter, though).
Don't try to outsmart these defaults by creating something less secure. Anything that is based on rand(), mt_rand(), uniqid(), or variations of these is *not* good.If you are you going to use bcrypt then you should pepper the passwords with random large string, as commodity hardware can break bcrypt 8 character passwords within an hour; https://www.tomshardware.com/news/eight-rtx-4090s-can-break-passwords-in-under-an-hourPlease note that password_hash will ***truncate*** the password at the first NULL-byte.
http://blog.ircmaxell.com/2015/03/security-issue-combining-bcrypt-with.html
If you use anything as an input that can generate NULL bytes (sha1 with raw as true, or if NULL bytes can naturally end up in people's passwords), you may make your application much less secure than what you might be expecting.
The password
$a = "\01234567";
is zero bytes long (an empty password) for bcrypt.
The workaround, of course, is to make sure you don't ever pass NULL-bytes to password_hash.Timing attacks simply put, are attacks that can calculate what characters of the password are due to speed of the execution.
More at...
https://paragonie.com/blog/2015/11/preventing-timing-attacks-on-string-comparison-with-double-hmac-strategy
I have added code to phpnetcomment201908 at lucb1e dot com's suggestion to make this possible "timing attack" more difficult using the code phpnetcomment201908 at lucb1e dot com posted.
$pph_strt = microtime(true);
//...
/*The code he posted for login.php*/
//...
$end = (microtime(true) - $pph_strt);
$wait = bcmul((1 - $end), 1000000); // usleep(250000) 1/4 of a second
usleep ( $wait );
echo "<br>Execution time:".(microtime(true) - $pph_strt)."; ";
Note I suggest changing the wait time to suit your needs but make sure that it is more than than the highest execution time the script takes on your server.
Also, this is my workaround to obfuscate the execution time to nullify timing attacks. You can find an in-depth discussion and more from people far more equipped than I for cryptography at the link I posted. I do not believe this was there but there are others. It is where I found out what timing attacks were as I am new to this but would like solid security.In most cases it is best to omit the salt parameter. Without this parameter, the function will generate a cryptographically safe salt, from the random source of the operating system.For passwords, you generally want the hash calculation time to be between 250 and 500 ms (maybe more for administrator accounts). Since calculation time is dependent on the capabilities of the server, using the same cost parameter on two different servers may result in vastly different execution times. Here's a quick little function that will help you determine what cost parameter you should be using for your server to make sure you are within this range (note, I am providing a salt to eliminate any latency caused by creating a pseudorandom salt, but this should not be done when hashing passwords):
<?php
/**
* @Param int $min_ms Minimum amount of time in milliseconds that it should take
* to calculate the hashes
*/
function getOptimalBcryptCostParameter($min_ms = 250) {
for ($i = 4; $i < 31; $i++) {
$options = [ 'cost' => $i, 'salt' => 'usesomesillystringforsalt' ];
$time_start = microtime(true);
password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options);
$time_end = microtime(true);
if (($time_end - $time_start) * 1000 > $min_ms) {
return $i;
}
}
}
echo getOptimalBcryptCostParameter(); // prints 12 in my case
?>