PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/ZendTest/Crypt/HmacTest.php

https://bitbucket.org/gencer/zf2
PHP | 161 lines | 110 code | 21 blank | 30 comment | 0 complexity | 0c82e1dc1122e8976ef2fa60f91a40d2 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-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Crypt;
  10. use Zend\Crypt\Hmac;
  11. /**
  12. * Outside the Internal Function tests, tests do not distinguish between hash and mhash
  13. * when available. All tests use Hashing algorithms both extensions implement.
  14. */
  15. /**
  16. * @group Zend_Crypt
  17. */
  18. class HmacTest extends \PHPUnit_Framework_TestCase
  19. {
  20. public function testIsSupportedAndCache()
  21. {
  22. Hmac::clearLastAlgorithmCache();
  23. $this->assertAttributeEquals(null, 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  24. $algorithm = 'sha512';
  25. // cache value must be exactly equal to the original input
  26. $this->assertTrue(Hmac::isSupported($algorithm));
  27. $this->assertAttributeEquals($algorithm, 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  28. $this->assertAttributeNotEquals('sHa512', 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  29. // cache value must be exactly equal to the first input (cache hit)
  30. Hmac::isSupported('sha512');
  31. $this->assertAttributeEquals($algorithm, 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  32. // cache changes with a new algorithm
  33. $this->assertTrue(Hmac::isSupported('MD5'));
  34. $this->assertAttributeEquals('MD5', 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  35. // cache don't change due wrong algorithm
  36. $this->assertFalse(Hmac::isSupported('wrong'));
  37. $this->assertAttributeEquals('MD5', 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  38. Hmac::clearLastAlgorithmCache();
  39. $this->assertAttributeEquals(null, 'lastAlgorithmSupported', 'Zend\Crypt\Hmac');
  40. }
  41. // MD5 tests taken from RFC 2202
  42. public function provideMd5Data()
  43. {
  44. return array(
  45. array('Hi There', str_repeat("\x0b", 16), '9294727a3638bb1c13f48ef8158bfc9d'),
  46. array('what do ya want for nothing?', 'Jefe', '750c783e6ab0b503eaa86e310a5db738'),
  47. array(str_repeat("\xdd", 50), str_repeat("\xaa", 16), '56be34521d144c88dbb8c733f0e8b3f6'),
  48. array(str_repeat("\xcd", 50),
  49. "\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",
  50. '697eaf0aca3a3aea3a75164746ffaa79'),
  51. array('Test With Truncation', str_repeat("\x0c", 16), '56461ef2342edc00f9bab995690efd4c'),
  52. array('Test Using Larger Than Block-Size Key - Hash Key First', str_repeat("\xaa", 80),
  53. '6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd'),
  54. array('Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data',
  55. str_repeat("\xaa", 80), '6f630fad67cda0ee1fb1f562db3aa53e'),
  56. );
  57. }
  58. /**
  59. * @dataProvider provideMd5Data
  60. */
  61. public function testMd5($data, $key, $output)
  62. {
  63. $hash = Hmac::compute($key, 'MD5', $data);
  64. $this->assertEquals($output, $hash);
  65. }
  66. // SHA1 tests taken from RFC 2202
  67. public function provideSha1Data()
  68. {
  69. return array(
  70. array('Hi There', str_repeat("\x0b", 20), 'b617318655057264e28bc0b6fb378c8ef146be00'),
  71. array('what do ya want for nothing?', 'Jefe', 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79'),
  72. array(str_repeat("\xdd", 50), str_repeat("\xaa", 20), '125d7342b9ac11cd91a39af48aa17b4f63f175d3'),
  73. array(str_repeat("\xcd", 50),
  74. "\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",
  75. '4c9007f4026250c6bc8414f9bf50c86c2d7235da'),
  76. array('Test With Truncation', str_repeat("\x0c", 20), '4c1a03424b55e07fe7f27be1d58bb9324a9a5a04'),
  77. array('Test Using Larger Than Block-Size Key - Hash Key First', str_repeat("\xaa", 80),
  78. 'aa4ae5e15272d00e95705637ce8a3b55ed402112'),
  79. array('Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data',
  80. str_repeat("\xaa", 80), 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91'),
  81. );
  82. }
  83. /**
  84. * @dataProvider provideSha1Data
  85. */
  86. public function testSha1($data, $key, $output)
  87. {
  88. $hash = Hmac::compute($key, 'SHA1', $data);
  89. $this->assertEquals($output, $hash);
  90. }
  91. // RIPEMD160 tests taken from RFC 2286
  92. public function provideRipemd160Data()
  93. {
  94. return array(
  95. array('Hi There', str_repeat("\x0b", 20), '24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668'),
  96. array('what do ya want for nothing?', 'Jefe', 'dda6c0213a485a9e24f4742064a7f033b43c4069'),
  97. array(str_repeat("\xdd", 50), str_repeat("\xaa", 20), 'b0b105360de759960ab4f35298e116e295d8e7c1'),
  98. array(str_repeat("\xcd", 50),
  99. "\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",
  100. 'd5ca862f4d21d5e610e18b4cf1beb97a4365ecf4'),
  101. array('Test With Truncation', str_repeat("\x0c", 20), '7619693978f91d90539ae786500ff3d8e0518e39'),
  102. array('Test Using Larger Than Block-Size Key - Hash Key First', str_repeat("\xaa", 80),
  103. '6466ca07ac5eac29e1bd523e5ada7605b791fd8b'),
  104. array('Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data',
  105. str_repeat("\xaa", 80), '69ea60798d71616cce5fd0871e23754cd75d5a0a'),
  106. );
  107. }
  108. /**
  109. * @dataProvider provideRipemd160Data
  110. */
  111. public function testRipemd160($data, $key, $output)
  112. {
  113. $hash = Hmac::compute($key, 'RIPEMD160', $data);
  114. $this->assertEquals($output, $hash);
  115. }
  116. public function testEmptyKey()
  117. {
  118. Hmac::clearLastAlgorithmCache();
  119. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  120. 'Provided key is null or empty');
  121. Hmac::compute(null, 'md5', 'test');
  122. }
  123. public function testNullHashAlgorithm()
  124. {
  125. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  126. 'Hash algorithm is not supported on this PHP installation');
  127. Hmac::compute('key', null, 'test');
  128. }
  129. public function testWrongHashAlgorithm()
  130. {
  131. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  132. 'Hash algorithm is not supported on this PHP installation');
  133. Hmac::compute('key', 'wrong', 'test');
  134. }
  135. public function testBinaryOutput()
  136. {
  137. $data = Hmac::compute('key', 'sha256', 'test', Hmac::OUTPUT_BINARY);
  138. $this->assertEquals('Aq+1YwSQLGVvy3N83QPeYgW7bUAdooEu/ZstNqCK8Vk=', base64_encode($data));
  139. }
  140. }