PasswordEncoderTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * This file is part of PHPOffice Common
  4. *
  5. * PHPOffice Common is free software distributed under the terms of the GNU Lesser
  6. * General Public License version 3 as published by the Free Software Foundation.
  7. *
  8. * For the full copyright and license information, please read the LICENSE
  9. * file that was distributed with this source code. For the full list of
  10. * contributors, visit https://github.com/PHPOffice/Common/contributors.
  11. *
  12. * @link https://github.com/PHPOffice/Common
  13. * @copyright 2009-2016 PHPOffice Common contributors
  14. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  15. */
  16. namespace PhpOffice\Common\Tests\Microsoft;
  17. use PhpOffice\Common\Microsoft\PasswordEncoder;
  18. /**
  19. * Test class for PhpOffice\Common\PasswordEncoder
  20. * @coversDefaultClass \PhpOffice\Common\PasswordEncoder
  21. */
  22. class PasswordEncoderTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /**
  25. * Test that a password can be hashed without specifying any additional parameters
  26. */
  27. public function testEncodePassword()
  28. {
  29. //given
  30. $password = 'test';
  31. //when
  32. $hashPassword = PasswordEncoder::hashPassword($password);
  33. //then
  34. $this->assertEquals('M795/MAlmGU8RIsY9Q9uDLHC7bk=', $hashPassword);
  35. }
  36. /**
  37. * Test that a password can be hashed with a custom salt
  38. */
  39. public function testEncodePasswordWithSalt()
  40. {
  41. //given
  42. $password = 'test';
  43. $salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA==');
  44. //when
  45. $hashPassword = PasswordEncoder::hashPassword($password, PasswordEncoder::ALGORITHM_SHA_1, $salt);
  46. //then
  47. $this->assertEquals('QiDOcpia1YzSVJPiKPwWebl9p/0=', $hashPassword);
  48. }
  49. /**
  50. * Test that the encoder falls back on SHA-1 if a non supported algorithm is given
  51. */
  52. public function testDefaultsToSha1IfUnsupportedAlgorithm()
  53. {
  54. //given
  55. $password = 'test';
  56. $salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA==');
  57. //when
  58. $hashPassword = PasswordEncoder::hashPassword($password, PasswordEncoder::ALGORITHM_MAC, $salt);
  59. //then
  60. $this->assertEquals('QiDOcpia1YzSVJPiKPwWebl9p/0=', $hashPassword);
  61. }
  62. /**
  63. * Test that the encoder falls back on SHA-1 if a non supported algorithm is given
  64. */
  65. public function testEncodePasswordWithNullAsciiCodeInPassword()
  66. {
  67. //given
  68. $password = 'test' . chr(0);
  69. $salt = base64_decode('uq81pJRRGFIY5U+E9gt8tA==');
  70. //when
  71. $hashPassword = PasswordEncoder::hashPassword($password, PasswordEncoder::ALGORITHM_MAC, $salt, 1);
  72. //then
  73. $this->assertEquals('rDV9sgdDsztoCQlvRCb1lF2wxNg=', $hashPassword);
  74. }
  75. }