Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PasswordEncoder should support BCrypt #5932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fabpot opened this issue Nov 7, 2012 · 15 comments
Closed

PasswordEncoder should support BCrypt #5932

fabpot opened this issue Nov 7, 2012 · 15 comments
Labels

Comments

@fabpot
Copy link
Member

fabpot commented Nov 7, 2012

see discussion here:

https://groups.google.com/group/symfony-devs/browse_thread/thread/92ae24eafd13e29e?hl=en

@stof
Copy link
Member

stof commented Nov 7, 2012

@elnur maybe you should simply contribute your encoder to the core for 2.2 ?

@elnur
Copy link
Contributor

elnur commented Nov 8, 2012

Yea, no problem. I'll do it on this weekend.

@theoreticaLee
Copy link

@elnur you want to pair up on this one? I can tie up what we already have in a bow and submit the pull request.

Here is the port on your very nice code:
https://github.com/theoreticaLee/symfony/commit/41281dcddcaa717bf358bbc611a8d931d1f7fb25

I want to review it one more time, and get your go ahead and I'll submit a pull request

@elnur
Copy link
Contributor

elnur commented Nov 8, 2012

@theoreticaLee, thanks, but I better do it on my own.

First of all, I want to rename the encoder and at least one variable. Then, it's not just the encoder that needs to be ported, but the configuration stuff, tests, documentation and changelog need to be added as well.

So, just give me a couple of days and I'll do it. ;)

@theoreticaLee
Copy link

ok, removed commit.

@mvrhov
Copy link

mvrhov commented Nov 8, 2012

@bzikarsky
Copy link

If you convert the ElnurBlowfishPasswordEncoderBundle into a Symfony2 default, can you have a look at PERBILITY\BCryptBundle. It's pretty much the same as your bundle, but provides support for a) a global salt which can be defined at application level and b) the possibility to bind the hash to additional user-data (such as email).

@mvrhov
Copy link

mvrhov commented Nov 14, 2012

@TerjeBr: it was all already taken care of in my gist. Which is also faster as it uses standard base64.

@TerjeBr
Copy link

TerjeBr commented Nov 14, 2012

@mvrhov I can see at least 2 errors in your getSalt() function. First it requires 18 random bytes instead of 16.
Second, if the varible $this->useOpenSsl is false or the openssl_random_pseudo_bytes function fails to give a strong result, you fall back to using a base64 encoded sha256 hash. But here you have forgotten to replace the '+' character with '.' as you did further up in the function.

@mvrhov
Copy link

mvrhov commented Nov 14, 2012

Now the salt is indeed a bit longer. I'm just making sure that we are not short and at the same time too greedy.
The fallback is to hex encoded salt, so this is actually not a bug.

@TerjeBr
Copy link

TerjeBr commented Nov 14, 2012

@mvrhov Have you studied your own code?

return substr(hash('sha512', uniqid(mt_rand(), true), true), 0, $nbBytes);

When $nbBytes is 16, this will return 16 raw bytes not encoded as anything. That is one error.
The second error, is why on earth do you first get data from mt_rand, then run it through uniqid and then through hash_sha512? You are just wasting entropy everywhere here, and making the possible different retrunvalues less.

Your third error is in the above comment. You cannot return the value hex encoded. It will just be interpreted as base64 on the crypt routine all the same, but again you limit the entropy. Only those base64 values that also happen to be valid hex will be used.

If you are going to use mt_rand as a fallback, it is best to just use that value directly in your code, as I have done.
That will give the best entropy in the result.

Also, why not use /dev/urandom as a source if the system has an /dev/urandom? That is the first choice in my code.

(And as I said before, please feel free to copy any chunks of code from me.)

@stof
Copy link
Member

stof commented Nov 15, 2012

Well, when submitting the encoder, the generation of the salt should use the SecureRandom class anyway instead of trying to duplicate it (and it provides a secure fallback when SSL is disabled)

@TerjeBr
Copy link

TerjeBr commented Nov 15, 2012

Here is my latest version of the code

@stof Be aware that the random generation is only one part of my complaint with the code as it stands.
Please have another look at my implementation of the function gensalt_blowfish($random)

@Bilge
Copy link
Contributor

Bilge commented Jan 11, 2013

+1 This needs to be implemented as a matter of priority. Even Zend Framework supports this.

@stof
Copy link
Member

stof commented Jan 11, 2013

there is already a PR implementing it (#5974) but it is not ready to be merged yet

fabpot added a commit that referenced this issue Feb 5, 2013
This PR was submitted for the master branch but it was merged into the 2.2 branch instead (closes #6808).

Commits
-------

0cb74a2 Added BCrypt password encoder.

Discussion
----------

Bcrypt password encoder

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #5932
License of the code: MIT
Documentation PR: symfony/symfony-docs#1936

---------------------------------------------------------------------------

by TerjeBr at 2013-01-19T18:53:21Z

Finishing PR #5974

---------------------------------------------------------------------------

by jalliot at 2013-01-20T14:07:54Z

This looks very good! :)
But be careful to CS or this will not be merged.

---------------------------------------------------------------------------

by TerjeBr at 2013-01-20T14:17:35Z

I do not understand. What do you mean by "CS"?

---------------------------------------------------------------------------

by jalliot at 2013-01-20T14:20:33Z

[Coding standards](http://symfony.com/doc/current/contributing/code/standards.html).
You should run [PHP-CS-Fixer](https://github.com/fabpot/PHP-CS-Fixer) on your code to fix it.

---------------------------------------------------------------------------

by TerjeBr at 2013-01-20T14:47:23Z

The only thing php-cs-fixer.phar did was to realign some of the doc-block comments.
But thank you any way for pointing out the script to me.

---------------------------------------------------------------------------

by TerjeBr at 2013-01-20T15:52:07Z

Why does it look like @elnur added the commits? It was me.

---------------------------------------------------------------------------

by stof at 2013-01-20T16:32:12Z

@TerjeBr check your git configuration to be sure it uses your email address when committing

---------------------------------------------------------------------------

by TerjeBr at 2013-01-20T17:30:58Z

Now the commit is in my name.
But see what happens if I squash the commit wit git rebase ....

---------------------------------------------------------------------------

by TerjeBr at 2013-01-20T17:33:08Z

Now it looks like elnur added the commit.

---------------------------------------------------------------------------

by stof at 2013-01-26T15:57:59Z

@fabpot is there a chance to have this in 2.2 ?
@fabpot fabpot closed this as completed Feb 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants