Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Hash Tool
  5. * =========
  6. *
  7. * Small helper script to PHP's hash functions.
  8. *
  9. * @author Fabio Ros
  10. *
  11. * Examples:
  12. *
  13. * ./phash --adler32 somefile.txt
  14. * ./phash --sha256 mystring
  15. * ./phash --list
  16. *
  17. */
  18.  
  19. /**
  20. * Class Hash
  21. */
  22. class Hash
  23. {
  24. public $arg;
  25. public $file;
  26.  
  27. private $checkFileSize = 0;
  28. private $stringIfNotFile = 1;
  29.  
  30. /**
  31. * Hash constructor.
  32. * @param $argv
  33. */
  34. public function __construct($argv)
  35. {
  36. if (!isset($argv[1])) {
  37. $this->outputHelp();
  38. } else {
  39. $this->parseOptions($argv);
  40. }
  41.  
  42. if (isset($this->help) || $this->arg === 'help') {
  43. $this->outputHelp();
  44. exit();
  45. }
  46.  
  47. if (isset($this->list)) {
  48. foreach (hash_algos() as $algo) {
  49. echo '* ' . $algo . "\n";
  50. }
  51. }
  52.  
  53. if (isset($this->arg)) {
  54. $this->processFile();
  55. } else {
  56. // echo 'Specify some input for the algorithm';
  57. exit();
  58. }
  59.  
  60. foreach (hash_algos() as $algo) {
  61. if (isset($this->$algo)) {
  62. $hash = hash($algo, $this->file);
  63. echo $hash;
  64.  
  65. }
  66. }
  67.  
  68. exit();
  69. }
  70.  
  71. /**
  72. * Output help
  73. */
  74. public function outputHelp()
  75. {
  76. echo "PHP Hash tool\n";
  77. echo "================\n";
  78. echo "Usage:\n";
  79. echo "./phash --[options] [file/string]\n\n";
  80. echo "--list\tshow available algo's\n";
  81. echo "--algo\tRun file against a hash algorithm\n";
  82. }
  83.  
  84. /**
  85. * Process the file with some validation
  86. */
  87. public function processFile()
  88. {
  89. if (substr($this->arg, 0, 2) === './') {
  90. $this->arg = str_replace('./', __DIR__ . DIRECTORY_SEPARATOR, $this->arg);
  91. }
  92. if (is_readable($this->arg)) {
  93. if (ini_get('memory_limit') !== '-1' && $this->checkFileSize) {
  94. $this->checkFileSizeWithMemory($this->arg);
  95. } else {
  96. $this->file = file_get_contents($this->arg);
  97. }
  98. } else {
  99. if ($this->stringIfNotFile) {
  100. $this->file = $this->arg;
  101. } else {
  102. echo 'File ' . $this->arg . 'is not readable.';
  103. exit();
  104. }
  105. }
  106. }
  107.  
  108. /**
  109. * Check filesize with memory
  110. * @todo wip
  111. * @param $file
  112. * @return bool
  113. */
  114. public function checkFileSizeWithMemory($file)
  115. {
  116. $size = filesize($file) + memory_get_usage();
  117. if ($size <= intval(str_replace('M', '000', ini_get('memory_limit')))) {
  118. return true;
  119. } else {
  120. echo "File $file too big!\n";
  121. echo "Filesize:" . filesize($file) . "\n";
  122. echo "Memory Usage:" . memory_get_usage() . "\n";
  123. echo "Memory limit:" . ini_get('memory_limit') . "\n";
  124. }
  125. }
  126.  
  127. /**
  128. * Parse arguments and options
  129. * @param $options
  130. */
  131. public function parseOptions($options)
  132. {
  133. array_shift($options);
  134. foreach ($options as $arg) {
  135. if (substr($arg, 0, 2) === '--') {
  136. if (strpos($arg, '=')) {
  137. $parsed = explode('=', str_replace('--', '', $arg))[0];
  138. $this->$parsed[0] = $parsed[1];
  139. } else {
  140. $parsed = str_replace('--', '', $arg);
  141. $this->$parsed = true;
  142. }
  143. } else {
  144. $this->arg = $arg;
  145. }
  146. }
  147. }
  148.  
  149. }
  150.  
  151. new Hash($argv);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement