Live data from Hacker News

Creating and Verifying Hashes in PHP 5.5

jcurcio.com

11–20 of 44 posts

Re: Creating and Verifying Hashes in PHP 5.5

#11
post #9

> 'salt' => password_hash("MySalt",PASSWORD_BCRYPT)] This is really, really dumb and pointless. In fact it makes absolutely no sense, it tells password_hash to use a bcrypted "MySalt" as a salt. Not only is there no reason to explicitly provide a salt unless you already have bcrypted passwords in a non-standard format, (in which case you'd pass the existing $salt directly, you wouldn't bcrypt it) this is an inane way…

Noted, and edited. Thanks for the suggestion.

Re: Creating and Verifying Hashes in PHP 5.5

#12
post #9

> 'salt' => password_hash("MySalt",PASSWORD_BCRYPT)] This is really, really dumb and pointless. In fact it makes absolutely no sense, it tells password_hash to use a bcrypted "MySalt" as a salt. Not only is there no reason to explicitly provide a salt unless you already have bcrypted passwords in a non-standard format, (in which case you'd pass the existing $salt directly, you wouldn't bcrypt it) this is an inane way…

Noted, and edited. Thanks for the suggestion.

You should have removed the salt entirely, after all the article is supposed to be "The easy way".

Edit: also the default cost is already 10, you should offer the $options as an aside and not suggest using them as standard practice.

Re: Creating and Verifying Hashes in PHP 5.5

#13
post #9

> 'salt' => password_hash("MySalt",PASSWORD_BCRYPT)] This is really, really dumb and pointless. In fact it makes absolutely no sense, it tells password_hash to use a bcrypted "MySalt" as a salt. Not only is there no reason to explicitly provide a salt unless you already have bcrypted passwords in a non-standard format, (in which case you'd pass the existing $salt directly, you wouldn't bcrypt it) this is an inane way…

Adding that salt is optional. I'm pretty sure the author just added it to the post so people know it exists.

I don't think it's dumb and pointless at all, really. Generally you wouldn't generate a salt yourself if you're using bcrypt however I'm sure there would be cases where it would be needed or even preferred for some reason. I would advocate for flexibility and education rather than rigidity and simplicity. That is to say, rather than make the function useless to those who'd need their own salt I think it's a better idea to allow these options while making sure developers know that they should only be used in certain situations. A function like this isn't bad because it can be misused.

Re: Creating and Verifying Hashes in PHP 5.5

#14
post #6

There's also a library for PHP >= 5.3.7 that provides the same API if PHP 5.5 isn't an option yet. https://github.com/ircmaxell/password_compat Written by Anthony Ferrara, the same guy behind the `password_*` API in PHP 5.5

There's also PHPass from OpenWall: http://www.openwall.com/phpass/

If you use PHPass, remember to check your hashes to make sure they're actually hashed with bcrypt. PHPass falls back to a less secure algorithm if bcrypt is not available in your PHP version. Most of the CMS's that claim to use PHPass actually use the "portable" option, which is based on MD5, because they want to remain compatible with PHP versions lower than 5.3 (the first version that is guaranteed to support bcrypt).

Re: Creating and Verifying Hashes in PHP 5.5

#15

This is a great addition to PHP. Before, most new devs would either skip hashing passwords altogether or use `md5()` or `sha1()`. So you'd end up with databases with passwords in plaintext or easily cracked. Bcrypt frameworks for PHP were too confusing for beginners. If you were lucky you were using a framework that took care of hashing and salting passwords and if you were extremely lucky you had bcrypt available on…

I remember using BCrypt in PHP a few years ago. It was the first time I'd ever used BCrypt but it wasn't easy to find any resources on using it with PHP. Well at least not in the correct way.

Re: Creating and Verifying Hashes in PHP 5.5

#16

This is a great addition to PHP. Before, most new devs would either skip hashing passwords altogether or use `md5()` or `sha1()`. So you'd end up with databases with passwords in plaintext or easily cracked. Bcrypt frameworks for PHP were too confusing for beginners. If you were lucky you were using a framework that took care of hashing and salting passwords and if you were extremely lucky you had bcrypt available on…

>There do exist experienced PHP devs who would do password storage the right way but PHP up until now neither made it easy nor encourage these practices as they relate to passwords.

I'm happy to be one of them! The app I'm currently building uses this bcrypt library, and I can only highly recommend it:

http://www.openwall.com/phpass/

Re: Creating and Verifying Hashes in PHP 5.5

#18
post #9

> 'salt' => password_hash("MySalt",PASSWORD_BCRYPT)] This is really, really dumb and pointless. In fact it makes absolutely no sense, it tells password_hash to use a bcrypted "MySalt" as a salt. Not only is there no reason to explicitly provide a salt unless you already have bcrypted passwords in a non-standard format, (in which case you'd pass the existing $salt directly, you wouldn't bcrypt it) this is an inane way…

Why don't you generate your salt by hand?

Re: Creating and Verifying Hashes in PHP 5.5

#19
post #17

Can someone explain to me what the "cost" option is?

The cost option is how algorithmically complex the resulting hash will be. The greater the cost, the longer it takes to create the final hash, but the more secure it is. Lower cost = lower secure.

Basically you want to use as high of a cost as you can on your servers hardware, without producing a noticeable slowdown to the end user.

Post reply on HN