PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/Crypt/HmacTest.php

https://github.com/bate/zf2
PHP | 222 lines | 170 code | 31 blank | 21 comment | 0 complexity | 2cec50f800e6a0ed16bbcb7c680c72ec 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 as 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. // MD5 tests taken from RFC 2202
  25. public function testHmacMD5_1()
  26. {
  27. $data = 'Hi There';
  28. $key = str_repeat("\x0b", 16);
  29. $hmac = HMAC::compute($key, 'MD5', $data);
  30. $this->assertEquals('9294727a3638bb1c13f48ef8158bfc9d', $hmac);
  31. }
  32. public function testHmacMD5_2()
  33. {
  34. $data = 'what do ya want for nothing?';
  35. $key = 'Jefe';
  36. $hmac = HMAC::compute($key, 'MD5', $data);
  37. $this->assertEquals('750c783e6ab0b503eaa86e310a5db738', $hmac);
  38. }
  39. public function testHmacMD5_3()
  40. {
  41. $data = str_repeat("\xdd", 50);
  42. $key = str_repeat("\xaa", 16);
  43. $hmac = HMAC::compute($key, 'MD5', $data);
  44. $this->assertEquals('56be34521d144c88dbb8c733f0e8b3f6', $hmac);
  45. }
  46. public function testHmacMD5_4()
  47. {
  48. $data = str_repeat("\xcd", 50);
  49. $key = "\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. $hmac = HMAC::compute($key, 'MD5', $data);
  51. $this->assertEquals('697eaf0aca3a3aea3a75164746ffaa79', $hmac);
  52. }
  53. public function testHmacMD5_5()
  54. {
  55. $data = 'Test With Truncation';
  56. $key = str_repeat("\x0c", 16);
  57. $hmac = HMAC::compute($key, 'MD5', $data);
  58. $this->assertEquals('56461ef2342edc00f9bab995690efd4c', $hmac);
  59. }
  60. public function testHmacMD5_6()
  61. {
  62. $data = 'Test Using Larger Than Block-Size Key - Hash Key First';
  63. $key = str_repeat("\xaa", 80);
  64. $hmac = HMAC::compute($key, 'MD5', $data);
  65. $this->assertEquals('6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd', $hmac);
  66. }
  67. public function testHmacMD5_7()
  68. {
  69. $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data';
  70. $key = str_repeat("\xaa", 80);
  71. $hmac = HMAC::compute($key, 'MD5', $data);
  72. $this->assertEquals('6f630fad67cda0ee1fb1f562db3aa53e', $hmac);
  73. }
  74. // SHA1 tests taken from RFC 2202
  75. public function testHmacSHA1_1()
  76. {
  77. $data = 'Hi There';
  78. $key = str_repeat("\x0b", 20);
  79. $hmac = HMAC::compute($key, 'SHA1', $data);
  80. $this->assertEquals('b617318655057264e28bc0b6fb378c8ef146be00', $hmac);
  81. }
  82. public function testHmacSHA1_2()
  83. {
  84. $data = 'what do ya want for nothing?';
  85. $key = 'Jefe';
  86. $hmac = HMAC::compute($key, 'SHA1', $data);
  87. $this->assertEquals('effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', $hmac);
  88. }
  89. public function testHmacSHA1_3()
  90. {
  91. $data = str_repeat("\xdd", 50);
  92. $key = str_repeat("\xaa", 20);
  93. $hmac = HMAC::compute($key, 'SHA1', $data);
  94. $this->assertEquals('125d7342b9ac11cd91a39af48aa17b4f63f175d3', $hmac);
  95. }
  96. public function testHmacSHA1_4()
  97. {
  98. $data = str_repeat("\xcd", 50);
  99. $key = "\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. $hmac = HMAC::compute($key, 'SHA1', $data);
  101. $this->assertEquals('4c9007f4026250c6bc8414f9bf50c86c2d7235da', $hmac);
  102. }
  103. public function testHmacSHA1_5()
  104. {
  105. $data = 'Test With Truncation';
  106. $key = str_repeat("\x0c", 20);
  107. $hmac = HMAC::compute($key, 'SHA1', $data);
  108. $this->assertEquals('4c1a03424b55e07fe7f27be1d58bb9324a9a5a04', $hmac);
  109. }
  110. public function testHmacSHA1_6()
  111. {
  112. $data = 'Test Using Larger Than Block-Size Key - Hash Key First';
  113. $key = str_repeat("\xaa", 80);
  114. $hmac = HMAC::compute($key, 'SHA1', $data);
  115. $this->assertEquals('aa4ae5e15272d00e95705637ce8a3b55ed402112', $hmac);
  116. }
  117. public function testHmacSHA1_7()
  118. {
  119. $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data';
  120. $key = str_repeat("\xaa", 80);
  121. $hmac = HMAC::compute($key, 'SHA1', $data);
  122. $this->assertEquals('e8e99d0f45237d786d6bbaa7965c7808bbff1a91', $hmac);
  123. }
  124. // RIPEMD160 tests taken from RFC 2286
  125. public function testHmacRIPEMD160_1()
  126. {
  127. $data = 'Hi There';
  128. $key = str_repeat("\x0b", 20);
  129. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  130. $this->assertEquals('24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668', $hmac);
  131. }
  132. public function testHmacRIPEMD160_2()
  133. {
  134. $data = 'what do ya want for nothing?';
  135. $key = 'Jefe';
  136. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  137. $this->assertEquals('dda6c0213a485a9e24f4742064a7f033b43c4069', $hmac);
  138. }
  139. public function testHmacRIPEMD160_3()
  140. {
  141. $data = str_repeat("\xdd", 50);
  142. $key = str_repeat("\xaa", 20);
  143. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  144. $this->assertEquals('b0b105360de759960ab4f35298e116e295d8e7c1', $hmac);
  145. }
  146. public function testHmacRIPEMD160_4()
  147. {
  148. $data = str_repeat("\xcd", 50);
  149. $key = "\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";
  150. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  151. $this->assertEquals('d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4', $hmac);
  152. }
  153. public function testHmacRIPEMD160_5()
  154. {
  155. $data = 'Test With Truncation';
  156. $key = str_repeat("\x0c", 20);
  157. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  158. $this->assertEquals('7619693978f91d90539ae786500ff3d8e0518e39', $hmac);
  159. }
  160. public function testHmacRIPEMD160_6()
  161. {
  162. $data = 'Test Using Larger Than Block-Size Key - Hash Key First';
  163. $key = str_repeat("\xaa", 80);
  164. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  165. $this->assertEquals('6466ca07ac5eac29e1bd523e5ada7605b791fd8b', $hmac);
  166. }
  167. public function testHmacRIPEMD160_7()
  168. {
  169. $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data';
  170. $key = str_repeat("\xaa", 80);
  171. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  172. $this->assertEquals('69ea60798d71616cce5fd0871e23754cd75d5a0a', $hmac);
  173. }
  174. public function testEmptyKey()
  175. {
  176. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  177. 'Provided key is null or empty');
  178. $hash = HMAC::compute(null, 'md5', 'test');
  179. }
  180. public function testWrongHashAlgorithm()
  181. {
  182. $this->setExpectedException('Zend\Crypt\Exception\InvalidArgumentException',
  183. 'Hash algorithm is not supported on this PHP installation');
  184. $hash = HMAC::compute('key', 'wrong', 'test');
  185. }
  186. public function testBinaryOutput()
  187. {
  188. $data = HMAC::compute('key', 'sha256', 'test', HMAC::OUTPUT_BINARY);
  189. $this->assertEquals('Aq+1YwSQLGVvy3N83QPeYgW7bUAdooEu/ZstNqCK8Vk=', base64_encode($data));
  190. }
  191. }