2

Possible Duplicate:
Is “double hashing” a password less secure than just hashing it once?

What do I gain by doing it? It slows hashing... does it automatically reduce possibility to somehow find out password from hash?

$password = '123456';

$iterations = 8;

$is_first = true;
for ($i = 0; $i < $iterations; ++$i) {
    if ($is_first === true) {

        $hashed_password = hash('sha256', $password);

    } else {

        $hashed_password = hash('sha256', $hashed_password);

    }

    $is_first = false;
}

If the answer is yes... how many iterations would be optional?

What other options of improving passwords security would you recommend (except salt and peanuts)?

Community
  • 1
  • 1
daGrevis
  • 21,014
  • 37
  • 100
  • 139
  • 1
    You could simplify that code a lot by just setting `$hashed_password = $password` at the start and then leaving out the big if/else block... – Amber Jul 03 '11 at 20:34
  • 1
    Vote to close: "What other options of improving password security" is *far* too broad to be answered as a SO question. – Oliver Charlesworth Jul 03 '11 at 20:35
  • @Amber: Either way you're duplicating something. I prefer it this way as all the logic is then lexically nearby. – Lightness Races in Orbit Jul 03 '11 at 20:41
  • @Tomalak - you prefer 7 lines to 2? – Amber Jul 03 '11 at 20:42
  • @Amber: It doesn't need to be 7 lines. `$hashed_password = hash('sha256', $is_first ? $password : $hashed_password); $is_first = false;` is what I'd do. I count three lines required. And do you have some sort of line number limit? Copying `hash` logic to lexically far away reduces maintainability and legibility. – Lightness Races in Orbit Jul 03 '11 at 20:46
  • Who said anything about copying `hash` logic? Just initialize the variable before the loop without manipulating the value. `$p = $password; for($i=0; $i<$iterations; ++$i) { $p = hash('sha256', $p); }` – Amber Jul 03 '11 at 21:10
  • This is simplified version of my code. I did it like that by reason. https://github.com/daGrevis/Hurricane/blob/master/modules/hurricane/classes/model/authentication.php#L79 – daGrevis Jul 04 '11 at 16:26

1 Answers1

1

Slowing hashing is good.

If an enemy gets your password hashes, you want him to be forced to take a long time to try each hash.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    This is true even in a scenario where the attacker doesn't have your hashes, and is simply brute-forcing your security mechanism. – Oliver Charlesworth Jul 03 '11 at 20:40
  • 1
    @Oli: You should have better defences against that. – SLaks Jul 03 '11 at 20:41
  • I've always recommended multiple hashing techniques. For example, `hash('md5', $salt.$password).hash('sha256',$salt.$password)`. This in general, makes it harder to find duplicate password that matches the hash. We also force our users to use a "pass phrase" instead of a password. "My birthday is on January 1st" is a much harder password to brute force, and much easier for the user to remember. – Rahly Jul 03 '11 at 20:54
  • @Jeremy: And much harder to remember **correctly**. Did I add `st`? Did I write `is` or use a contraction? Did I write `on`? Did I include the year? – SLaks Jul 03 '11 at 21:00
  • https://github.com/daGrevis/Hurricane/blob/master/modules/hurricane/classes/model/authentication.php#L72 – daGrevis Jul 03 '11 at 21:04
  • As I was just giving an example, I'm not sure what our users pick, but they rarely have to have their password reset. Also, when they have to change their password every 45 days, it does make it easier for them to pick new ones. And a lot easier to remember correctly than Mx&3Gb0T6y. – Rahly Jul 03 '11 at 21:06
  • 1
    @Jeremy Actually, that provides only a (small) constant factor improvement, see: http://en.wikipedia.org/wiki/Cryptographic_hash_function#Concatenation_of_cryptographic_hash_functions . Instead, you should use a single well-selected hash algorithm in a construction designed to slow password hashing like PBKDF2. Don't roll your own! – Nick Johnson Jul 04 '11 at 01:00