Encryption and Password Issues

Related questions:

Am I using the PHP crypt () function correctly?

Storage password hash with SHA-512 or crypt () with blowfish (bcrypt)

I am trying to figure out how I should safely store a password using PHP. After a little reading, I found that I should use crypt () instead of hash () and that I should use either the blowfish (bcrypt) algorithm or SHA-512, and I think bcrypt is recommended more often, although there is significant support for SHA-based algorithms -512.

There are also many suggestions that my salt should be as random as possible, with many suggestions for using openssl_random_pseudo_bytes() on the rand() and mt_rand() .

My main questions are:

  • If I decide to use bcrypt, what load factors should I use? I noticed that for PHP 5.5, the default load factor in the new password API is 10, so I would suggest that I need at least this value.

  • How does load factor correlate with password security? From what I understand, the algorithm will iterate 2^load_factor times, but I'm more interested in how this translates into a safe environment against brute force hacking methods. What does it mean to be "safe"? Does it take 10 years to crack? 5 years? 1 year?

  • Why should I choose bcrypt using the SHA-512 method (or vice versa)? I heard that SHA-512 is designed as a fast hashing method, so it will not be delayed over time, as well as bcrypt. It's true? Both methods have salt parameters that allow you to glue several times.

  • As far as I know, I applied the following test code that generates the bcrypt salt. Recommended Method? Is there a better way to do this?

_

 function gen_salt($cost) { return "$2y$" . $cost . "$" . str_replace('+', '.', base64_encode(openssl_random_pseudo_bytes(22))); } 
+4
source share
1 answer

So, based on the comments, I created a simple test to check how many different hashing methods are.

 function bcrypt_salt($cost) { return "$2y$" . $cost . "$" . str_replace('+', '.', base64_encode(openssl_random_pseudo_bytes(22))) . '$'; } function sha512_salt($cost) { return "\$6\$rounds=" . $cost . "\$" . openssl_random_pseudo_bytes(16) . '$'; } $password = "stackoverflow"; $times = 1; echo "<p>bcrypt method</p>"; for($iters = 10; $iters < 15; ++$iters) { $salt = bcrypt_salt(strval($iters)); $pword_crypt = crypt($password, $salt); $start_time = microtime(true); for($i = 0; $i < $times; ++$i) { crypt($password, $pword_crypt); } $end_time = microtime(true); echo "<p> cost = $iters: " . ($end_time - $start_time) . "</p>"; } echo "<p>SHA512 method</p>"; for($iters = 1024; $iters < 1000000; $iters *= 2) { $salt = sha512_salt(strval($iters)); $pword_crypt = crypt($password, $salt); $start_time = microtime(true); for($i = 0; $i < $times; ++$i) { crypt($password, $pword_crypt); } $end_time = microtime(true); echo "<p> log2(iters) = ". log($iters,2) . ": " . ($end_time - $start_time) . "</p>"; } 

Test results (time in seconds):

Ran on my laptop with i5-m430:

Bcrypt method

cost = 10: 0.11740303039551

cost = 11: 0.23875308036804

cost = 12: 0.46739792823792

cost = 13: 0.96053194999695

cost = 14: 1.8993430137634

SHA512 Method

log2 (iters) = 10: 0.0034840106964111

log2 (iters) = 11: 0.0077731609344482

log2 (iters) = 12: 0.014604806900024

log2 (iters) = 13: 0.02855396270752

log2 (iters) = 14: 0.068222999572754

log2 (iters) = 15: 0.12677311897278

log2 (iters) = 16: 0.24734497070312

log2 (iters) = 17: 0.54663610458374

log2 (iters) = 18: 1.0215079784393

log2 (iters) = 19: 2.0223300457001

If things are equal, more iterations are required for the SHA-512 and bcrypt methods to get the same amount of time. However, I assume that any method that takes at least one tenth of a second is more than sufficient.

+1
source

Source: https://habr.com/ru/post/1439340/


All Articles