sha3-512sum.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env php
  2. <?php /* -*- coding: utf-8; indent-tabs-mode: t; tab-width: 4 -*-
  3. vim: ts=4 noet ai */
  4. /**
  5. sha3-512sum - compute SHA3-512 message digest (command-line tool)
  6. Copyright © 2018 Desktopd Developers
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. @license GPL-3+
  18. @file
  19. */
  20. use desktopd\SHA3\Sponge as SHA3;
  21. require_once __DIR__ . '/namespaced/desktopd/SHA3/Sponge.php';
  22. $progName = 'sha3-512sum';
  23. $version = '1.0';
  24. $copyrightYear = '2018';
  25. $digestName = 'SHA3-512';
  26. $bitNum = '512';
  27. $args = isset ($argv) ? array_slice ($argv, 1) : array ();
  28. $binary = false;
  29. //require_once dirname (__FILE__) . '/SHA3.php';
  30. function hashFile ($path = null) {
  31. global $progName;
  32. global $binary;
  33. if (!$path) {
  34. $path = 'php://stdin';
  35. } elseif (!is_file ($path)) {
  36. fprintf (STDERR, "%s: %s: No such file\n", $progName, $path);
  37. exit (1);
  38. } elseif (!is_readable ($path)) {
  39. fprintf (STDERR, "%s: %s: Permission denied\n", $progName, $path);
  40. exit (1);
  41. }
  42. $fp = fopen ($path, $binary ? 'rb' : 'r');
  43. $sponge = SHA3::init (SHA3::SHA3_512);
  44. while (!feof ($fp)) {
  45. $sponge->absorb (fread ($fp, 1024));
  46. }
  47. fclose ($fp);
  48. if ('php://stdin' === $path) $path = '-';
  49. printf ("%s %s%s\n", bin2hex ($sponge->squeeze ()), $binary ? '*' : ' '
  50. , $path);
  51. }
  52. $noArgs = true;
  53. while (count ($args)) {
  54. $arg = array_shift ($args);
  55. switch ($arg) {
  56. case '--help':
  57. echo <<<HELP
  58. Usage: $progName [OPTION]... [FILE]...
  59. Print $digestName ($bitNum-bit) checksums.
  60. With no FILE, or when FILE is -, read standard input
  61. -b, --binary read in binary mode
  62. -t, --text read in text mode (default)
  63. --help display this help and exit.
  64. --version output version information and exit.
  65. The sums are calculated according to FIPS-202. Checking
  66. mode is not yet implemented. See GNU Coreutils for
  67. general ideas of sha*sum utilities.
  68. Binary flag has an effect on DOS-based systems (i.e. Microsoft
  69. Windows Computers (WCs))
  70. The Desktopd project: <https://notabug.org/org/desktopd>
  71. HELP;
  72. exit (0);
  73. case '--version':
  74. echo <<<HELP
  75. $progName (PHP-SHA3-Streamable) $version
  76. Copyright (C) $copyrightYear Desktopd Developers
  77. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
  78. This is free software: you are free to change and redistribute it.
  79. There is NO WARRANTY, to the extent permitted by law.
  80. Uses the PHP-SHA3-Streamable library, which is under LGPLv3+.
  81. Modeled after GNU Coreutils' sha*sums utilities.
  82. The Desktopd project: <https://notabug.org/org/desktopd>
  83. HELP;
  84. exit (0);
  85. case '-b':
  86. case '--binary':
  87. $binary = true;
  88. break;
  89. case '-t':
  90. case '--text':
  91. $binary = false;
  92. break;
  93. default:
  94. hashFile ('-' === $arg ? null : $arg);
  95. $noArgs = false;
  96. break;
  97. }
  98. }
  99. if ($noArgs) {
  100. hashFile ();
  101. }