PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/tests/ZendTest/Crypt/HashTest.php

https://bitbucket.org/pcelta/zf2
PHP | 145 lines | 92 code | 19 blank | 34 comment | 0 complexity | c44fbd8df1f03c02d736b45701501fc4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Crypt
  9. */
  10. namespace ZendTest\Crypt;
  11. use Zend\Crypt\Hash;
  12. /**
  13. * Outside the Internal Function tests, tests do not distinguish between hash and mhash
  14. * when available. All tests use Hashing algorithms both extensions implement.
  15. */
  16. /**
  17. * @category Zend
  18. * @package Zend_Crypt
  19. * @subpackage UnitTests
  20. * @group Zend_Crypt
  21. */
  22. class HashTest extends \PHPUnit_Framework_TestCase
  23. {
  24. public function testIsSupportedAndCache()
  25. {
  26. Hash::clearLastAlgorithmCache();
  27. $this->assertAttributeEquals(null, 'lastAlgorithmSupported', 'Zend\Crypt\Hash');
  28. $algorithm = 'sha512';
  29. // cache value must be exactly equal to the original input
  30. $this->assertTrue(Hash::isSupported($algorithm));
  31. $this->assertAttributeEquals($algorithm, 'lastAlgorithmSupported', 'Zend\Crypt\Hash');
  32. $this->assertAttributeNotEquals('sHa512', 'lastAlgorithmSupported', 'Zend\Crypt\Hash');
  33. // cache value must be exactly equal to the first input (cache hit)
  34. Hash::isSupported('sha512');
  35. $this->assertAttributeEquals($algorithm, 'lastAlgorithmSupported', 'Zend\Crypt\Hash');
  36. // cache changes with a new algorithm
  37. $this->assertTrue(Hash::isSupported('sha1'));
  38. $this->assertAttributeEquals('sha1', 'lastAlgorithmSupported', 'Zend\Crypt\Hash');
  39. // cache don't change due wrong algorithm
  40. $this->assertFalse(Hash::isSupported('wrong'));
  41. $this->assertAttributeEquals('sha1', 'lastAlgorithmSupported', 'Zend\Crypt\Hash');
  42. Hash::clearLastAlgorithmCache();
  43. $this->assertAttributeEquals(null, 'lastAlgorithmSupported', 'Zend\Crypt\Hash');
  44. }
  45. // SHA1 tests taken from RFC 3174
  46. public function provideSha1Data()
  47. {
  48. return array(
  49. array('abc',
  50. strtolower('A9993E364706816ABA3E25717850C26C9CD0D89D')),
  51. array('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
  52. strtolower('84983E441C3BD26EBAAE4AA1F95129E5E54670F1')),
  53. array(str_repeat('a', 1000000),
  54. strtolower('34AA973CD4C4DAA4F61EEB2BDBAD27316534016F')),
  55. array(str_repeat('01234567', 80),
  56. strtolower('DEA356A2CDDD90C7A7ECEDC5EBB563934F460452'))
  57. );
  58. }
  59. /**
  60. * @dataProvider provideSha1Data
  61. */
  62. public function testSha1($data, $output)
  63. {
  64. $hash = Hash::compute('sha1', $data);
  65. $this->assertEquals($output, $hash);
  66. }
  67. // SHA-224 tests taken from RFC 3874
  68. public function provideSha224Data()
  69. {
  70. return array(
  71. array('abc', '23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7'),
  72. array('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
  73. '75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525'),
  74. array(str_repeat('a', 1000000),
  75. '20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67')
  76. );
  77. }
  78. /**
  79. * @dataProvider provideSha224Data
  80. */
  81. public function testSha224($data, $output)
  82. {
  83. $hash = Hash::compute('sha224', $data);
  84. $this->assertEquals($output, $hash);
  85. }
  86. // MD5 test suite taken from RFC 1321
  87. public function provideMd5Data()
  88. {
  89. return array(
  90. array('', 'd41d8cd98f00b204e9800998ecf8427e'),
  91. array('a', '0cc175b9c0f1b6a831c399e269772661'),
  92. array('abc', '900150983cd24fb0d6963f7d28e17f72'),
  93. array('message digest', 'f96b697d7cb7938d525a2f31aaf161d0'),
  94. array('abcdefghijklmnopqrstuvwxyz', 'c3fcd3d76192e4007dfb496cca67e13b'),
  95. array('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  96. 'd174ab98d277d9f5a5611c2c9f419d9f'),
  97. array(str_repeat('1234567890', 8), '57edf4a22be3c955ac49da2e2107b67a')
  98. );
  99. }
  100. /**
  101. * @dataProvider provideMd5Data
  102. */
  103. public function testMd5($data, $output)
  104. {
  105. $hash = Hash::compute('md5', $data);
  106. $this->assertEquals($output, $hash);
  107. }
  108. public function testNullHashAlgorithm()
  109. {
  110. Hash::clearLastAlgorithmCache();
  111. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  112. 'Hash algorithm provided is not supported on this PHP installation');
  113. Hash::compute(null, 'test');
  114. }
  115. public function testWrongHashAlgorithm()
  116. {
  117. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  118. 'Hash algorithm provided is not supported on this PHP installation');
  119. Hash::compute('wrong', 'test');
  120. }
  121. public function testBinaryOutput()
  122. {
  123. $hash = Hash::compute('sha1', 'test', Hash::OUTPUT_BINARY);
  124. $this->assertEquals('qUqP5cyxm6YcTAhz05Hph5gvu9M=', base64_encode($hash));
  125. }
  126. }