HasherTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Illuminate\Tests\Hashing;
  3. use Illuminate\Hashing\Argon2IdHasher;
  4. use Illuminate\Hashing\ArgonHasher;
  5. use Illuminate\Hashing\BcryptHasher;
  6. use PHPUnit\Framework\TestCase;
  7. use RuntimeException;
  8. class HasherTest extends TestCase
  9. {
  10. public function testBasicBcryptHashing()
  11. {
  12. $hasher = new BcryptHasher;
  13. $value = $hasher->make('password');
  14. $this->assertNotSame('password', $value);
  15. $this->assertTrue($hasher->check('password', $value));
  16. $this->assertFalse($hasher->needsRehash($value));
  17. $this->assertTrue($hasher->needsRehash($value, ['rounds' => 1]));
  18. $this->assertSame('bcrypt', password_get_info($value)['algoName']);
  19. }
  20. public function testBasicArgon2iHashing()
  21. {
  22. $hasher = new ArgonHasher;
  23. $value = $hasher->make('password');
  24. $this->assertNotSame('password', $value);
  25. $this->assertTrue($hasher->check('password', $value));
  26. $this->assertFalse($hasher->needsRehash($value));
  27. $this->assertTrue($hasher->needsRehash($value, ['threads' => 1]));
  28. $this->assertSame('argon2i', password_get_info($value)['algoName']);
  29. }
  30. public function testBasicArgon2idHashing()
  31. {
  32. $hasher = new Argon2IdHasher;
  33. $value = $hasher->make('password');
  34. $this->assertNotSame('password', $value);
  35. $this->assertTrue($hasher->check('password', $value));
  36. $this->assertFalse($hasher->needsRehash($value));
  37. $this->assertTrue($hasher->needsRehash($value, ['threads' => 1]));
  38. $this->assertSame('argon2id', password_get_info($value)['algoName']);
  39. }
  40. /**
  41. * @depends testBasicBcryptHashing
  42. */
  43. public function testBasicBcryptVerification()
  44. {
  45. $this->expectException(RuntimeException::class);
  46. $argonHasher = new ArgonHasher(['verify' => true]);
  47. $argonHashed = $argonHasher->make('password');
  48. (new BcryptHasher(['verify' => true]))->check('password', $argonHashed);
  49. }
  50. /**
  51. * @depends testBasicArgon2iHashing
  52. */
  53. public function testBasicArgon2iVerification()
  54. {
  55. $this->expectException(RuntimeException::class);
  56. $bcryptHasher = new BcryptHasher(['verify' => true]);
  57. $bcryptHashed = $bcryptHasher->make('password');
  58. (new ArgonHasher(['verify' => true]))->check('password', $bcryptHashed);
  59. }
  60. /**
  61. * @depends testBasicArgon2idHashing
  62. */
  63. public function testBasicArgon2idVerification()
  64. {
  65. $this->expectException(RuntimeException::class);
  66. $bcryptHasher = new BcryptHasher(['verify' => true]);
  67. $bcryptHashed = $bcryptHasher->make('password');
  68. (new Argon2IdHasher(['verify' => true]))->check('password', $bcryptHashed);
  69. }
  70. }