Util.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace NavOnlineInvoice;
  3. use Exception;
  4. class Util {
  5. public static $customSha3_512Function = null;
  6. public static function isHashAlgoSupported($algo) {
  7. $algos = hash_algos();
  8. return in_array($algo, $algos);
  9. }
  10. private static function desktopdSHA3_512($string) {
  11. $sponge = \desktopd\SHA3\Sponge::init(\desktopd\SHA3\Sponge::SHA3_512);
  12. $sponge->absorb($string);
  13. return strtoupper(bin2hex($sponge->squeeze()));
  14. }
  15. public static function sha3_512($string) {
  16. // Built-in SHA3-512 from PHP 7.1.0
  17. if (self::isHashAlgoSupported("sha3-512")) {
  18. return strtoupper(hash("sha3-512", $string));
  19. }
  20. // User provided function
  21. if (self::$customSha3_512Function and is_callable(self::$customSha3_512Function)) {
  22. return call_user_func(self::$customSha3_512Function, $string);
  23. }
  24. // https://packagist.org/packages/n-other/php-sha3
  25. if (class_exists("\\bb\Sha3\\Sha3")) {
  26. return strtoupper(\bb\Sha3\Sha3::hash($string, 512));
  27. }
  28. // Desktopd SHA3 (https://notabug.org/desktopd/PHP-SHA3-Streamable)
  29. if (class_exists("\\desktopd\\SHA3\\Sponge")) {
  30. return self::desktopdSHA3_512($string);
  31. }
  32. throw new Exception("SHA3-512 nem támogatott! Kérlek,\n- frissíts PHP 7.1.0 vagy e feletti verzióra;\n- vagy állíts be egy egyedi SHA3-512 függvényt;\n- vagy hivatkozd be az n-other/php-sha3 vagy desktopd/SHA könyvtárat.\nRészletekért lásd a nav-online-invoice README-t.");
  33. }
  34. public static function sha512($string) {
  35. return strtoupper(hash("sha512", $string));
  36. }
  37. public static function aes128_decrypt($string, $key) {
  38. return openssl_decrypt($string, "AES-128-ECB", $key);
  39. }
  40. }