PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/pcelta/zf2
PHP | 165 lines | 110 code | 21 blank | 34 comment | 0 complexity | 040842b76dbc84539ccc89c9564640c6 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\Hmac;
  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 HmacTest extends \PHPUnit_Framework_TestCase
  23. {
  24. public function testIsSupportedAndCache()
  25. {
  26. Hmac::clearLastAlgorithmCache();
  27. $this->assertAttributeEquals(null, 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  28. $algorithm = 'sha512';
  29. // cache value must be exactly equal to the original input
  30. $this->assertTrue(Hmac::isSupported($algorithm));
  31. $this->assertAttributeEquals($algorithm, 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  32. $this->assertAttributeNotEquals('sHa512', 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  33. // cache value must be exactly equal to the first input (cache hit)
  34. Hmac::isSupported('sha512');
  35. $this->assertAttributeEquals($algorithm, 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  36. // cache changes with a new algorithm
  37. $this->assertTrue(Hmac::isSupported('MD5'));
  38. $this->assertAttributeEquals('MD5', 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  39. // cache don't change due wrong algorithm
  40. $this->assertFalse(Hmac::isSupported('wrong'));
  41. $this->assertAttributeEquals('MD5', 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  42. Hmac::clearLastAlgorithmCache();
  43. $this->assertAttributeEquals(null, 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  44. }
  45. // MD5 tests taken from RFC 2202
  46. public function provideMd5Data()
  47. {
  48. return array(
  49. array('Hi There', str_repeat("\x0b", 16), '9294727a3638bb1c13f48ef8158bfc9d'),
  50. array('what do ya want for nothing?', 'Jefe', '750c783e6ab0b503eaa86e310a5db738'),
  51. array(str_repeat("\xdd", 50), str_repeat("\xaa", 16), '56be34521d144c88dbb8c733f0e8b3f6'),
  52. array(str_repeat("\xcd", 50),
  53. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
  54. '697eaf0aca3a3aea3a75164746ffaa79'),
  55. array('Test With Truncation', str_repeat("\x0c", 16), '56461ef2342edc00f9bab995690efd4c'),
  56. array('Test Using Larger Than Block-Size Key - Hash Key First', str_repeat("\xaa", 80),
  57. '6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd'),
  58. array('Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data',
  59. str_repeat("\xaa", 80), '6f630fad67cda0ee1fb1f562db3aa53e'),
  60. );
  61. }
  62. /**
  63. * @dataProvider provideMd5Data
  64. */
  65. public function testMd5($data, $key, $output)
  66. {
  67. $hash = Hmac::compute($key, 'MD5', $data);
  68. $this->assertEquals($output, $hash);
  69. }
  70. // SHA1 tests taken from RFC 2202
  71. public function provideSha1Data()
  72. {
  73. return array(
  74. array('Hi There', str_repeat("\x0b", 20), 'b617318655057264e28bc0b6fb378c8ef146be00'),
  75. array('what do ya want for nothing?', 'Jefe', 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79'),
  76. array(str_repeat("\xdd", 50), str_repeat("\xaa", 20), '125d7342b9ac11cd91a39af48aa17b4f63f175d3'),
  77. array(str_repeat("\xcd", 50),
  78. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
  79. '4c9007f4026250c6bc8414f9bf50c86c2d7235da'),
  80. array('Test With Truncation', str_repeat("\x0c", 20), '4c1a03424b55e07fe7f27be1d58bb9324a9a5a04'),
  81. array('Test Using Larger Than Block-Size Key - Hash Key First', str_repeat("\xaa", 80),
  82. 'aa4ae5e15272d00e95705637ce8a3b55ed402112'),
  83. array('Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data',
  84. str_repeat("\xaa", 80), 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91'),
  85. );
  86. }
  87. /**
  88. * @dataProvider provideSha1Data
  89. */
  90. public function testSha1($data, $key, $output)
  91. {
  92. $hash = Hmac::compute($key, 'SHA1', $data);
  93. $this->assertEquals($output, $hash);
  94. }
  95. // RIPEMD160 tests taken from RFC 2286
  96. public function provideRipemd160Data()
  97. {
  98. return array(
  99. array('Hi There', str_repeat("\x0b", 20), '24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668'),
  100. array('what do ya want for nothing?', 'Jefe', 'dda6c0213a485a9e24f4742064a7f033b43c4069'),
  101. array(str_repeat("\xdd", 50), str_repeat("\xaa", 20), 'b0b105360de759960ab4f35298e116e295d8e7c1'),
  102. array(str_repeat("\xcd", 50),
  103. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
  104. 'd5ca862f4d21d5e610e18b4cf1beb97a4365ecf4'),
  105. array('Test With Truncation', str_repeat("\x0c", 20), '7619693978f91d90539ae786500ff3d8e0518e39'),
  106. array('Test Using Larger Than Block-Size Key - Hash Key First', str_repeat("\xaa", 80),
  107. '6466ca07ac5eac29e1bd523e5ada7605b791fd8b'),
  108. array('Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data',
  109. str_repeat("\xaa", 80), '69ea60798d71616cce5fd0871e23754cd75d5a0a'),
  110. );
  111. }
  112. /**
  113. * @dataProvider provideRipemd160Data
  114. */
  115. public function testRipemd160($data, $key, $output)
  116. {
  117. $hash = Hmac::compute($key, 'RIPEMD160', $data);
  118. $this->assertEquals($output, $hash);
  119. }
  120. public function testEmptyKey()
  121. {
  122. Hmac::clearLastAlgorithmCache();
  123. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  124. 'Provided key is null or empty');
  125. Hmac::compute(null, 'md5', 'test');
  126. }
  127. public function testNullHashAlgorithm()
  128. {
  129. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  130. 'Hash algorithm is not supported on this PHP installation');
  131. Hmac::compute('key', null, 'test');
  132. }
  133. public function testWrongHashAlgorithm()
  134. {
  135. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  136. 'Hash algorithm is not supported on this PHP installation');
  137. Hmac::compute('key', 'wrong', 'test');
  138. }
  139. public function testBinaryOutput()
  140. {
  141. $data = Hmac::compute('key', 'sha256', 'test', Hmac::OUTPUT_BINARY);
  142. $this->assertEquals('Aq+1YwSQLGVvy3N83QPeYgW7bUAdooEu/ZstNqCK8Vk=', base64_encode($data));
  143. }
  144. }