PHPGangsta / GoogleAuthenticator

PHP class to generate and verify Google Authenticator 2-factor authentication
http://phpgangsta.de/4376
BSD 2-Clause "Simplified" License
2.26k stars 698 forks source link

array_rand does not use secure random #10

Closed Sc00bz closed 8 years ago

PHPGangsta commented 9 years ago

Hi Steve,

One test is failing, because it tries to generate a secret with length 0, and mcrypt_create_iv() does not allow that: testCreateSecretLengthCanBeSpecified() See failed Travis build.

If you fix the test (it should start from 1 instead of 0), all tests should pass.

RobThree commented 9 years ago

A secret length of 0 (or 1 for that matter) makes no sense anyway; the (very) minimum value should be (around) 80 bits* and I would even consider throwing an exception on anything less. I also find it pretty arbitrary to test for values 0...100 (why 100 and not... 97? 109? 2091? 729183019?). It would make more sense to test for specific ("edge") cases and commonly used cases:

* Make sure you don't fall for the 1 character = 8 bits of entropy. Unless you use the entire 0..255 range of the byte (which you don't) you must count the actual bits of entropy (which is a nice round 5.0 at 32 characters available) per character. That is exactly why Google recommends at least 16 chars for the secret: 16 chars x 5 bits of entropy = 80 bits.

jippi commented 9 years ago

:+1:

Sc00bz commented 9 years ago

Umm I guess I can't just throw an exception because that causes the Travis CI build to failed. Will changing it to die(...) make it fail too?

RobThree commented 9 years ago

You could die or return an error code of some sort or whatever indicates that something is wrong. But having said that: exceptions can, ofcourse, also be unit-tested. In fact: you can indicate that you expect some method to throw an exception (or even more specific: you expect it to throw Exception of type X); the unit-test will PASS on that exception and FAIL if the exception isn't thrown. So throwing an exception doesn't automatically imply you can't unittest that code.

You can see here for example (noted by the @expectedException on several unittests) how to unittest code that is supposed to throw an exception.

Conver commented 8 years ago

+1