PageRenderTime 33ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Crypt/HmacTest.php

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