PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Crypt/HMACTest.php

http://github.com/zendframework/zf2
PHP | 218 lines | 153 code | 28 blank | 37 comment | 0 complexity | 50f19405ba8956d1731c022cb7993062 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Crypt
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\Crypt;
  25. use Zend\Crypt\Hmac as HMAC;
  26. /**
  27. * Outside the Internal Function tests, tests do not distinguish between hash and mhash
  28. * when available. All tests use Hashing algorithms both extensions implement.
  29. */
  30. /**
  31. * @category Zend
  32. * @package Zend_Crypt
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Crypt
  37. */
  38. class HmacTest extends \PHPUnit_Framework_TestCase
  39. {
  40. // MD5 tests taken from RFC 2202
  41. public function testHmacMD5_1()
  42. {
  43. $data = 'Hi There';
  44. $key = str_repeat("\x0b", 16);
  45. $hmac = HMAC::compute($key, 'MD5', $data);
  46. $this->assertEquals('9294727a3638bb1c13f48ef8158bfc9d', $hmac);
  47. }
  48. public function testHmacMD5_2()
  49. {
  50. $data = 'what do ya want for nothing?';
  51. $key = 'Jefe';
  52. $hmac = HMAC::compute($key, 'MD5', $data);
  53. $this->assertEquals('750c783e6ab0b503eaa86e310a5db738', $hmac);
  54. }
  55. public function testHmacMD5_3()
  56. {
  57. $data = str_repeat("\xdd",50);
  58. $key = str_repeat("\xaa", 16);
  59. $hmac = HMAC::compute($key, 'MD5', $data);
  60. $this->assertEquals('56be34521d144c88dbb8c733f0e8b3f6', $hmac);
  61. }
  62. public function testHmacMD5_4()
  63. {
  64. $data = str_repeat("\xcd",50);
  65. $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";
  66. $hmac = HMAC::compute($key, 'MD5', $data);
  67. $this->assertEquals('697eaf0aca3a3aea3a75164746ffaa79', $hmac);
  68. }
  69. public function testHmacMD5_5()
  70. {
  71. $data = 'Test With Truncation';
  72. $key = str_repeat("\x0c",16);
  73. $hmac = HMAC::compute($key, 'MD5', $data);
  74. $this->assertEquals('56461ef2342edc00f9bab995690efd4c', $hmac);
  75. }
  76. public function testHmacMD5_6()
  77. {
  78. $data = 'Test Using Larger Than Block-Size Key - Hash Key First';
  79. $key = str_repeat("\xaa",80);
  80. $hmac = HMAC::compute($key, 'MD5', $data);
  81. $this->assertEquals('6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd', $hmac);
  82. }
  83. public function testHmacMD5_7()
  84. {
  85. $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data';
  86. $key = str_repeat("\xaa",80);
  87. $hmac = HMAC::compute($key, 'MD5', $data);
  88. $this->assertEquals('6f630fad67cda0ee1fb1f562db3aa53e', $hmac);
  89. }
  90. // SHA1 tests taken from RFC 2202
  91. public function testHmacSHA1_1()
  92. {
  93. $data = 'Hi There';
  94. $key = str_repeat("\x0b", 20);
  95. $hmac = HMAC::compute($key, 'SHA1', $data);
  96. $this->assertEquals('b617318655057264e28bc0b6fb378c8ef146be00', $hmac);
  97. }
  98. public function testHmacSHA1_2()
  99. {
  100. $data = 'what do ya want for nothing?';
  101. $key = 'Jefe';
  102. $hmac = HMAC::compute($key, 'SHA1', $data);
  103. $this->assertEquals('effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', $hmac);
  104. }
  105. public function testHmacSHA1_3()
  106. {
  107. $data = str_repeat("\xdd",50);
  108. $key = str_repeat("\xaa", 20);
  109. $hmac = HMAC::compute($key, 'SHA1', $data);
  110. $this->assertEquals('125d7342b9ac11cd91a39af48aa17b4f63f175d3', $hmac);
  111. }
  112. public function testHmacSHA1_4()
  113. {
  114. $data = str_repeat("\xcd",50);
  115. $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";
  116. $hmac = HMAC::compute($key, 'SHA1', $data);
  117. $this->assertEquals('4c9007f4026250c6bc8414f9bf50c86c2d7235da', $hmac);
  118. }
  119. public function testHmacSHA1_5()
  120. {
  121. $data = 'Test With Truncation';
  122. $key = str_repeat("\x0c",20);
  123. $hmac = HMAC::compute($key, 'SHA1', $data);
  124. $this->assertEquals('4c1a03424b55e07fe7f27be1d58bb9324a9a5a04', $hmac);
  125. }
  126. public function testHmacSHA1_6()
  127. {
  128. $data = 'Test Using Larger Than Block-Size Key - Hash Key First';
  129. $key = str_repeat("\xaa",80);
  130. $hmac = HMAC::compute($key, 'SHA1', $data);
  131. $this->assertEquals('aa4ae5e15272d00e95705637ce8a3b55ed402112', $hmac);
  132. }
  133. public function testHmacSHA1_7()
  134. {
  135. $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data';
  136. $key = str_repeat("\xaa",80);
  137. $hmac = HMAC::compute($key, 'SHA1', $data);
  138. $this->assertEquals('e8e99d0f45237d786d6bbaa7965c7808bbff1a91', $hmac);
  139. }
  140. // RIPEMD160 tests taken from RFC 2286
  141. public function testHmacRIPEMD160_1()
  142. {
  143. $data = 'Hi There';
  144. $key = str_repeat("\x0b", 20);
  145. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  146. $this->assertEquals('24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668', $hmac);
  147. }
  148. public function testHmacRIPEMD160_2()
  149. {
  150. $data = 'what do ya want for nothing?';
  151. $key = 'Jefe';
  152. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  153. $this->assertEquals('dda6c0213a485a9e24f4742064a7f033b43c4069', $hmac);
  154. }
  155. public function testHmacRIPEMD160_3()
  156. {
  157. $data = str_repeat("\xdd",50);
  158. $key = str_repeat("\xaa", 20);
  159. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  160. $this->assertEquals('b0b105360de759960ab4f35298e116e295d8e7c1', $hmac);
  161. }
  162. public function testHmacRIPEMD160_4()
  163. {
  164. $data = str_repeat("\xcd",50);
  165. $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";
  166. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  167. $this->assertEquals('d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4', $hmac);
  168. }
  169. public function testHmacRIPEMD160_5()
  170. {
  171. $data = 'Test With Truncation';
  172. $key = str_repeat("\x0c",20);
  173. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  174. $this->assertEquals('7619693978f91d90539ae786500ff3d8e0518e39', $hmac);
  175. }
  176. public function testHmacRIPEMD160_6()
  177. {
  178. $data = 'Test Using Larger Than Block-Size Key - Hash Key First';
  179. $key = str_repeat("\xaa",80);
  180. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  181. $this->assertEquals('6466ca07ac5eac29e1bd523e5ada7605b791fd8b', $hmac);
  182. }
  183. public function testHmacRIPEMD160_7()
  184. {
  185. $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data';
  186. $key = str_repeat("\xaa",80);
  187. $hmac = HMAC::compute($key, 'RIPEMD160', $data);
  188. $this->assertEquals('69ea60798d71616cce5fd0871e23754cd75d5a0a', $hmac);
  189. }
  190. }