HashTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils;
  4. use PHPUnit\Framework\TestCase;
  5. use function strlen;
  6. class HashTest extends TestCase
  7. {
  8. /**
  9. * @covers App\Utils\Hash::cookieHash
  10. */
  11. public function testCookieHash()
  12. {
  13. $_ENV['key'] = 'cookie_key';
  14. $passHash = 'password';
  15. $expire_in = 69420;
  16. $result = Hash::cookieHash($passHash, $expire_in);
  17. $this->assertIsString($result);
  18. $this->assertEquals(45, strlen($result));
  19. $this->assertEquals('e91053c4a7d6cc7fa5eb900b1ad96df484483ceace12a', $result);
  20. }
  21. /**
  22. * @covers App\Utils\Hash::ipHash
  23. */
  24. public function testIpHash()
  25. {
  26. $_ENV['key'] = 'cookie_key';
  27. $ip = '192.168.0.1';
  28. $uid = 69;
  29. $expire_in = 69420;
  30. $result = Hash::ipHash($ip, $uid, $expire_in);
  31. $this->assertIsString($result);
  32. $this->assertEquals(45, strlen($result));
  33. $this->assertEquals('522b51095b778f9f107153f75be554be1f8a8f2c1f4b4', $result);
  34. }
  35. /**
  36. * @covers App\Utils\Hash::deviceHash
  37. */
  38. public function testDeviceHash()
  39. {
  40. $_ENV['key'] = 'cookie_key';
  41. $device = 'Firefox/119.0';
  42. $uid = 69;
  43. $expire_in = 69420;
  44. $result = Hash::deviceHash($device, $uid, $expire_in);
  45. $this->assertIsString($result);
  46. $this->assertEquals(45, strlen($result));
  47. $this->assertEquals('1fd5a37cc8769c01a49f6eb9c167dc6ee6cc842913dba', $result);
  48. }
  49. /**
  50. * @covers App\Utils\Hash::checkPassword
  51. * @covers App\Utils\Hash::passwordHash
  52. * @covers App\Utils\Hash::sha256WithSalt
  53. * @covers App\Utils\Hash::sha3WithSalt
  54. */
  55. public function testPasswordFunctions()
  56. {
  57. $_ENV['salt'] = 'password_salt';
  58. $_ENV['pwdMethod'] = 'bcrypt';
  59. $password = 'password';
  60. $hashedPassword = Hash::passwordHash($password);
  61. $this->assertTrue(Hash::checkPassword($hashedPassword, $password));
  62. $this->assertFalse(Hash::checkPassword($hashedPassword, 'wrong_password'));
  63. $_ENV['pwdMethod'] = 'argon2i';
  64. $hashedPassword = Hash::passwordHash($password);
  65. $this->assertTrue(Hash::checkPassword($hashedPassword, $password));
  66. $this->assertFalse(Hash::checkPassword($hashedPassword, 'wrong_password'));
  67. $_ENV['pwdMethod'] = 'argon2id';
  68. $hashedPassword = Hash::passwordHash($password);
  69. $this->assertTrue(Hash::checkPassword($hashedPassword, $password));
  70. $this->assertFalse(Hash::checkPassword($hashedPassword, 'wrong_password'));
  71. $_ENV['pwdMethod'] = 'sha256';
  72. $hashedPassword = Hash::passwordHash($password);
  73. $this->assertTrue(Hash::checkPassword($hashedPassword, $password));
  74. $this->assertFalse(Hash::checkPassword($hashedPassword, 'wrong_password'));
  75. $_ENV['pwdMethod'] = 'sha3';
  76. $hashedPassword = Hash::passwordHash($password);
  77. $this->assertTrue(Hash::checkPassword($hashedPassword, $password));
  78. $this->assertFalse(Hash::checkPassword($hashedPassword, 'wrong_password'));
  79. }
  80. }