PasswordTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\security;
  9. use lithium\security\Password;
  10. class PasswordTest extends \lithium\test\Unit {
  11. /**
  12. * The password to be encrypted.
  13. */
  14. protected $_password = 'lith1um';
  15. /**
  16. * Tests the `Password::hash()` method with both generated and
  17. * custom salts.
  18. */
  19. public function testHash() {
  20. $this->skipIf(!CRYPT_BLOWFISH, 'Blowfish is not supported.');
  21. $salt = '$2a$07$l1th1um1saw3some12345678$';
  22. $expected = '$2a$07$l1th1um1saw3some12345uDt5Wuw5uzI5lCIn3HM1QkB7cJLou4Hy';
  23. $result = Password::hash($this->_password, $salt);
  24. $this->assertEqual($expected, $result);
  25. $result = Password::hash($this->_password);
  26. $this->assertNotEqual($expected, $result);
  27. }
  28. /**
  29. * Tests the `Password::check()` method to make sure that it returns
  30. * either true or false, depending on the input.
  31. */
  32. public function testCheck() {
  33. $this->skipIf(!CRYPT_BLOWFISH, 'Blowfish is not supported.');
  34. $salt = '$2a$07$l1th1um1saw3some12345678$';
  35. $hash = Password::hash($this->_password, $salt);
  36. $this->assertTrue(Password::check($this->_password, $hash));
  37. $hash = Password::hash($this->_password);
  38. $this->assertTrue(Password::check($this->_password, $hash));
  39. $wrong = 'wr0ng';
  40. $this->assertFalse(Password::check($wrong, $hash));
  41. }
  42. /**
  43. * Tests salting passwords with the Blowfish algorithm.
  44. *
  45. * It also contains tests to prove that password longer than 72 characters
  46. * are translated into the same hash.
  47. */
  48. public function testSaltBlowfish() {
  49. $this->skipIf(!CRYPT_BLOWFISH, 'Blowfish is not supported.');
  50. $saltPattern = "{^\\$2a\\$06\\$[0-9A-Za-z./]{22}$}";
  51. $hashPattern = "{^\\$2a\\$06\\$[0-9A-Za-z./]{53}$}";
  52. $log2 = 6;
  53. $salt = Password::salt('bf', $log2);
  54. $this->assertPattern($saltPattern, $salt);
  55. $this->assertNotEqual($salt, Password::salt('bf', $log2));
  56. $hash = Password::hash($this->_password, $salt);
  57. $hash2 = Password::hash($this->_password, Password::salt('bf', $log2));
  58. $this->assertPattern($hashPattern, $hash);
  59. $this->assertNotEqual($hash, $hash2);
  60. $maxLength = 72;
  61. $salt = Password::salt('bf');
  62. $password = str_repeat('a', $maxLength);
  63. $expected = Password::hash($password, $salt);
  64. $result = Password::hash($password . 'a', $salt);
  65. $this->assertIdentical($expected, $result);
  66. }
  67. /**
  68. * Tests salting passwords with the Extended-DES algorithm.
  69. */
  70. public function testSaltXDES() {
  71. $this->skipIf(!CRYPT_EXT_DES, 'Extended-DES is not supported.');
  72. $saltPattern = "{^_[0-9A-Za-z./]{8}$}";
  73. $hashPattern = "{^_[0-9A-Za-z./]{19}$}";
  74. $log2 = 18;
  75. $salt = Password::salt('xdes', $log2);
  76. $this->assertPattern($saltPattern, $salt);
  77. $this->assertNotEqual($salt, Password::salt('xdes', $log2));
  78. $hash = Password::hash($this->_password, $salt);
  79. $hash2 = Password::hash($this->_password, Password::salt('xdes', $log2));
  80. $this->assertPattern($hashPattern, $hash);
  81. $this->assertNotEqual($hash, $hash2);
  82. }
  83. /**
  84. * Tests salting passwords with the MD5 algorithm.
  85. */
  86. public function testSaltMD5() {
  87. $this->skipIf(!CRYPT_MD5, 'MD5 is not supported.');
  88. $saltPattern = "{^\\$1\\$[0-9A-Za-z./]{8}$}";
  89. $hashPattern = "{^\\$1\\$[0-9A-Za-z./]{8}\\$[0-9A-Za-z./]{22}$}";
  90. $salt = Password::salt('md5', null);
  91. $this->assertPattern($saltPattern, $salt);
  92. $this->assertNotEqual($salt, Password::salt('md5', null));
  93. $hash = Password::hash($this->_password, $salt);
  94. $hash2 = Password::hash($this->_password, Password::salt('md5', null));
  95. $this->assertPattern($hashPattern, $hash);
  96. $this->assertNotEqual($hash, $hash2);
  97. }
  98. }
  99. ?>