example.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php /* -*- coding: utf-8; indent-tabs-mode: t; tab-width: 4 -*-
  2. vim: ts=4 noet ai */
  3. /**
  4. Example for SHA-3 usage.
  5. Copyright © 2016 Desktopd Developers
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. @license GPL-3+
  17. @file
  18. */
  19. // The library is under GNU LGPL 3+
  20. require_once dirname (__FILE__) . '/SHA3.php'; // Oops, this is PHP 5.2+...
  21. /** Examples from https://en.wikipedia.org/wiki/SHA-3 */
  22. var_dump (bin2hex (SHA3::init (SHA3::SHA3_256)->absorb ('')->squeeze ()));
  23. var_dump (bin2hex (SHA3::init (SHA3::SHA3_512)->absorb ('')->squeeze ()));
  24. var_dump (bin2hex (SHA3::init (SHA3::SHA3_224)->absorb ('')->squeeze ()));
  25. var_dump (bin2hex (SHA3::init (SHA3::SHAKE128)
  26. ->absorb ("The quick brown fox jumps over the lazy dog")->squeeze (32)));
  27. var_dump (bin2hex (SHA3::init (SHA3::SHAKE128)
  28. ->absorb ("The quick brown fox jumps over the lazy dof")->squeeze (32)));
  29. /* Let's compare with non-PHP implementations! */
  30. var_dump (bin2hex (SHA3::init (SHA3::SHA3_256)->absorb (pack ('H*'
  31. , '648a0841e8a5afb3'))->squeeze ()));
  32. var_dump (bin2hex (SHA3::init (SHA3::SHA3_256)->absorb (pack ('H*'
  33. , '3e83e7c312587888af62f6cb7130ae96'))->squeeze ()));
  34. var_dump (bin2hex (SHA3::init (SHA3::SHA3_256)->absorb (pack ('H*'
  35. , '24557c1ae26fa5131c2ef0aa7f265b3b795839bb6460ba4fd51bae685285cc90'))
  36. ->squeeze ()));
  37. var_dump (bin2hex (SHA3::init (SHA3::SHA3_256)->absorb (pack ('H*'
  38. , '980bdf0b69eca4943f60f3408765048c9db6543d57f507960a5543a95e5f6338'
  39. . '2f1cb31138dae9b7341eb67ab242e0e905bd80b0786d5f4c2049d86791c49ae5'))
  40. ->squeeze ()));
  41. var_dump (bin2hex (SHA3::init (SHA3::SHA3_256)->absorb (pack ('H*'
  42. , '33739d1f4631596954127402df40baf971a792732ad8cbdceff1909e08636e07'
  43. . '717da79124a71c9b351f46e5c38b8cb3ca6a2f91496ac001532eb798fe3e5505'
  44. . '2ff32ae1239a089b77d4db4816135e71325eb0a3d38079760bd6d3d2fbbf9a43'
  45. . '91836b5f48d90f4a5678437a3b960a72642a0d3c092c4f75f03d386d8fa8f1d2'))
  46. ->squeeze ()));
  47. var_dump (bin2hex (SHA3::init (SHA3::SHA3_512)->absorb (pack ('H*'
  48. , '648a0841e8a5afb3'))->squeeze ()));
  49. var_dump (bin2hex (SHA3::init (SHA3::SHA3_512)->absorb (pack ('H*'
  50. , '3e83e7c312587888af62f6cb7130ae96'))->squeeze ()));
  51. var_dump (bin2hex (SHA3::init (SHA3::SHA3_512)->absorb (pack ('H*'
  52. , '24557c1ae26fa5131c2ef0aa7f265b3b795839bb6460ba4fd51bae685285cc90'))
  53. ->squeeze ()));
  54. var_dump (bin2hex (SHA3::init (SHA3::SHA3_512)->absorb (pack ('H*'
  55. , '980bdf0b69eca4943f60f3408765048c9db6543d57f507960a5543a95e5f6338'
  56. . '2f1cb31138dae9b7341eb67ab242e0e905bd80b0786d5f4c2049d86791c49ae5'))
  57. ->squeeze ()));
  58. var_dump (bin2hex (SHA3::init (SHA3::SHA3_512)->absorb (pack ('H*'
  59. , '33739d1f4631596954127402df40baf971a792732ad8cbdceff1909e08636e07'
  60. . '717da79124a71c9b351f46e5c38b8cb3ca6a2f91496ac001532eb798fe3e5505'
  61. . '2ff32ae1239a089b77d4db4816135e71325eb0a3d38079760bd6d3d2fbbf9a43'
  62. . '91836b5f48d90f4a5678437a3b960a72642a0d3c092c4f75f03d386d8fa8f1d2'))
  63. ->squeeze ()));