KeccakTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace kornrunner;
  3. /**
  4. *@see https://gist.github.com/Souptacular/f50128d63b5188490fa2
  5. */
  6. use PHPUnit\Framework\TestCase;
  7. class KeccakTest extends TestCase
  8. {
  9. private const SHORT = "52A608AB21CCDD8A4457A57EDE782176";
  10. private const LONG = "3A3A819C48EFDE2AD914FBF00E18AB6BC4F14513AB27D0C178A188B61431E7F5623CB66B23346775D386B50E982C493ADBBFC54B9A3CD383382336A1A0B2150A15358F336D03AE18F666C7573D55C4FD181C29E6CCFDE63EA35F0ADF5885CFC0A3D84A2B2E4DD24496DB789E663170CEF74798AA1BBCD4574EA0BBA40489D764B2F83AADC66B148B4A0CD95246C127D5871C4F11418690A5DDF01246A0C80A43C70088B6183639DCFDA4125BD113A8F49EE23ED306FAAC576C3FB0C1E256671D817FC2534A52F5B439F72E424DE376F4C565CCA82307DD9EF76DA5B7C4EB7E085172E328807C02D011FFBF33785378D79DC266F6A5BE6BB0E4A92ECEEBAEB1";
  11. private static $x64;
  12. public static function setUpBeforeClass() {
  13. parent::setUpBeforeClass();
  14. $class = new \ReflectionClass(Keccak::class);
  15. self::$x64 = $class->getProperty('x64');
  16. self::$x64->setAccessible(true);
  17. }
  18. /**
  19. * @dataProvider hash
  20. */
  21. public function testHash($level, $tests) {
  22. $x64_values = [true, false];
  23. foreach ($x64_values as $x64_bit) {
  24. self::$x64->setValue($x64_bit);
  25. foreach($tests as $test) {
  26. $message = $test[0];
  27. $expected = $test[1];
  28. $this->assertEquals($expected, Keccak::hash($message, $level));
  29. $this->assertEquals(hex2bin($expected), Keccak::hash($message, $level, true));
  30. }
  31. }
  32. }
  33. public static function hash(): array {
  34. return [
  35. /**
  36. * @see https://emn178.github.io/online-tools/keccak_512.html
  37. */
  38. [512, [
  39. ['','0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'],
  40. ['testing', '9558a7ba9ac74b33b347703ffe33f8d561d86d9fcad1cfd63225fb55dfea50a0953a0efafd6072377f4c396e806d5bda0294cba28762740d8446fee45a276e4a'],
  41. ['The quick brown fox jumps over the lazy dog', 'd135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609'],
  42. ['The quick brown fox jumps over the lazy dog.','ab7192d2b11f51c7dd744e7b3441febf397ca07bf812cceae122ca4ded6387889064f8db9230f173f6d1ab6e24b6e50f065b039f799f5592360a6558eb52d760'],
  43. [hex2bin(self::SHORT), '4b39d3da5bcdf4d9b769015995644311c14c435bf72b1009d6dd71b01a63b97cfb596418e8e42342d117e07471a8914314ba7b0e264dadf0cea381868cbd43d1'],
  44. [hex2bin(self::LONG), '81950e7096d31d4f22e3db71cac725bf59e81af54c7ca9e6aeee71c010fc5467466312a01aa5c137cfb140646941556796f612c9351268737c7e9a2b9631d1fa']
  45. ]],
  46. /**
  47. * @see https://emn178.github.io/online-tools/keccak_384.html
  48. */
  49. [384, [
  50. ['', '2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff'],
  51. ['testing', '1020b1c91956efe79b89c387b54de4f7a9c187c3970552f9f48c0da176f6326b7aa694795d2c9adcf2bdd20aec605588'],
  52. ['The quick brown fox jumps over the lazy dog', '283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3'],
  53. ['The quick brown fox jumps over the lazy dog.', '9ad8e17325408eddb6edee6147f13856ad819bb7532668b605a24a2d958f88bd5c169e56dc4b2f89ffd325f6006d820b'],
  54. [hex2bin(self::SHORT), '18422ac1d3a1e54bad876883d2d6dd65f65c1d5f33a7125cc4c186405a12ed64ba96672eedda8c5a6331d28683f488eb'],
  55. [hex2bin(self::LONG), '6bff1c8405a3fe594e360e3bccea1ebcd509310dc79b9e45c263783d7a5dd662c6789b18bd567dbdda1554f5bee6a860'],
  56. [hex2bin('E35780EB9799AD4C77535D4DDB683CF33EF367715327CF4C4A58ED9CBDCDD486F669F80189D549A9364FA82A51A52654EC721BB3AAB95DCEB4A86A6AFA93826DB923517E928F33E3FBA850D45660EF83B9876ACCAFA2A9987A254B137C6E140A21691E1069413848'), '9fb5700502e01926824f46e9f61894f9487dbcf8ae6217203c85606f975566539376d6239db04aef9bf48ca4f191a90b'],
  57. ]],
  58. /**
  59. * @see https://emn178.github.io/online-tools/keccak_256.html
  60. */
  61. [256, [
  62. ['', 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'],
  63. ['testing', '5f16f4c7f149ac4f9510d9cf8cf384038ad348b3bcdc01915f95de12df9d1b02'],
  64. ['The quick brown fox jumps over the lazy dog', '4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15'],
  65. ['The quick brown fox jumps over the lazy dog.', '578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d'],
  66. [hex2bin(self::SHORT), '0e32defa2071f0b5ac0e6a108b842ed0f1d3249712f58ee0ddf956fe332a5f95'],
  67. [hex2bin(self::LONG), '348fb774adc970a16b1105669442625e6adaa8257a89effdb5a802f161b862ea'],
  68. [hex2bin('9F2FCC7C90DE090D6B87CD7E9718C1EA6CB21118FC2D5DE9F97E5DB6AC1E9C10'), '24dd2ee02482144f539f810d2caa8a7b75d0fa33657e47932122d273c3f6f6d1'],
  69. ]],
  70. /**
  71. * @see https://emn178.github.io/online-tools/keccak_224.html
  72. */
  73. [224, [
  74. ['', 'f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd'],
  75. ['testing', '7b77b0b01d9b669ec7637ae75fd2f0ce234c8c8c835723b6715f4b59'],
  76. ['The quick brown fox jumps over the lazy dog', '310aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe'],
  77. ['The quick brown fox jumps over the lazy dog.', 'c59d4eaeac728671c635ff645014e2afa935bebffdb5fbd207ffdeab'],
  78. [hex2bin(self::SHORT), '5679cd509c5120af54795cf477149641cf27b2ebb6a5f90340704e57'],
  79. [hex2bin(self::LONG), '5af56987ea9cf11fcd0eac5ebc14b037365e9b1123e31cb2dfc7929a'],
  80. ]],
  81. ];
  82. }
  83. /**
  84. * @dataProvider Shake
  85. */
  86. public function testShake($level, $size, $tests)
  87. {
  88. $x64_values = [true, false];
  89. foreach ($x64_values as $x64_bit) {
  90. self::$x64->setValue($x64_bit);
  91. foreach($tests as $test) {
  92. $message = $test[0];
  93. $expected = $test[1];
  94. $this->assertEquals($expected, Keccak::shake($message, $level, $size));
  95. $this->assertEquals(hex2bin($expected), Keccak::shake($message, $level, $size, true));
  96. }
  97. }
  98. }
  99. public static function shake(): array {
  100. return [
  101. [128, 256, [
  102. ['', '7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26'],
  103. [hex2bin(self::SHORT), '3a0faca70c9d2b81d1064d429ea3b05ad27366f64985379ddd75bc73d6a83810'],
  104. [hex2bin(self::LONG), '14236e75b9784df4f57935f945356cbe383fe513ed30286f91060759bcb0ef4b'],
  105. ['The quick brown fox jumps over the lazy dog', 'f4202e3c5852f9182a0430fd8144f0a74b95e7417ecae17db0f8cfeed0e3e66e'],
  106. ['The quick brown fox jumps over the lazy dof', '853f4538be0db9621a6cea659a06c1107b1f83f02b13d18297bd39d7411cf10c'],
  107. ]],
  108. [256, 512, [
  109. ['', '46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762fd75dc4ddd8c0f200cb05019d67b592f6fc821c49479ab48640292eacb3b7c4be'],
  110. [hex2bin(self::SHORT), '57119c4507f975ad0e9ea4f1166e5f9b590bf2671aaeb41d130d2c570bafc579b0b9ec485cc736a0a848bbc886cbaa79ffcd067ce64b3b410741ab011c544225'],
  111. [hex2bin(self::LONG), '8a5199b4a7e133e264a86202720655894d48cff344a928cf8347f48379cef347dfc5bcffab99b27b1f89aa2735e23d30088ffa03b9edb02b9635470ab9f10389'],
  112. ]],
  113. ];
  114. }
  115. public function testUnsupportedHashOutputSize()
  116. {
  117. $this->expectException(\Exception::class);
  118. $this->expectExceptionMessage('Unsupported Keccak Hash output size.');
  119. Keccak::hash('', 225);
  120. }
  121. public function testUnsupportedShakeSecurityLevel()
  122. {
  123. $this->expectException(\Exception::class);
  124. $this->expectExceptionMessage('Unsupported Keccak Shake security level.');
  125. Keccak::shake('', 129, 256);
  126. }
  127. }