HashShell.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. App::uses('AppShell', 'Console/Command');
  3. //include_once('files/sha256.inc');
  4. class HashShell extends AppShell {
  5. const DEFAULT_HASH_ALG = 4; # sha1
  6. public $active = ['md5', 'sha1', 'sha256', 'sha512'];
  7. public $tasks = [];
  8. /**
  9. * Override main() for help message hook
  10. *
  11. * @return void
  12. */
  13. public function main() {
  14. $this->out($this->OptionParser->help());
  15. }
  16. public function string() {
  17. $this->out('Hash Strings...');
  18. $hashAlgos = hash_algos();
  19. $types = array_merge(array_keys($hashAlgos), ['q']);
  20. foreach ($hashAlgos as $key => $t) {
  21. $this->out(($key + 1) . ': ' . $t . (in_array($t, $this->active) ? ' (!)' : ''));
  22. }
  23. while (!isset($type) || !in_array($type - 1, $types)) {
  24. $type = $this->in('Select hashType - or [q] to quit', null, static::DEFAULT_HASH_ALG);
  25. if ($type === 'q') {
  26. return $this->error('Aborted!');
  27. }
  28. }
  29. $type--;
  30. while (empty($pwToHash) || mb_strlen($pwToHash) < 2) {
  31. $pwToHash = $this->in('String to Hash (2 characters at least)');
  32. }
  33. $pw = $this->_hash($hashAlgos[$type], $pwToHash);
  34. $this->out('type: ' . strtoupper($hashAlgos[$type]) . ' (length: ' . mb_strlen($pw) . ')');
  35. $this->hr();
  36. echo $pw;
  37. }
  38. /**
  39. * List all available
  40. */
  41. public function available() {
  42. $hashAlgos = hash_algos();
  43. foreach ($hashAlgos as $hashAlgo) {
  44. $this->out('- ' . $hashAlgo);
  45. }
  46. }
  47. public function compare() {
  48. $algos = hash_algos();
  49. $data = 'hello';
  50. foreach ($algos as $v) {
  51. $res = hash($v, $data, false);
  52. $r = str_split($res, 50);
  53. printf("%-12s %3d %s\n", $v, strlen($res), array_shift($r));
  54. while (!empty($r)) {
  55. printf(" %s\n", array_shift($r));
  56. }
  57. }
  58. }
  59. public function time() {
  60. $data = '';
  61. for ($i = 0; $i < 64000; $i++) {
  62. $data .= hash('md5', rand(), true);
  63. }
  64. echo strlen($data) . ' bytes of random data built !' . PHP_EOL . PHP_EOL . 'Testing hash algorithms ...' . PHP_EOL;
  65. $results = [];
  66. foreach (hash_algos() as $v) {
  67. echo $v . PHP_EOL;
  68. $time = microtime(true);
  69. hash($v, $data, false);
  70. $time = microtime(true) - $time;
  71. $results[$time * 1000000000][] = "$v (hex)";
  72. $time = microtime(true);
  73. hash($v, $data, true);
  74. $time = microtime(true) - $time;
  75. $results[$time * 1000000000][] = "$v (raw)";
  76. }
  77. ksort($results);
  78. echo PHP_EOL . PHP_EOL . 'Results: ' . PHP_EOL;
  79. $i = 1;
  80. foreach ($results as $k => $v) {
  81. foreach ($v as $k1 => $v1) {
  82. echo ' ' . str_pad($i++ . '.', 4, ' ', STR_PAD_LEFT) . ' ' . str_pad($v1, 30, ' ') . ($k / 1000000) . ' ms' . PHP_EOL;
  83. }
  84. }
  85. }
  86. public function help() {
  87. $this->out('-- Hash Strings --');
  88. $this->out('-- cake Tools.Hash [method]');
  89. $this->out('---- for custom hashing of pwd strings (method name optional)');
  90. $this->out('-- cake Tools.Hash compare');
  91. $this->out('---- to list all available methods and their lenghts');
  92. }
  93. public function getOptionParser() {
  94. $parser = parent::getOptionParser();
  95. $parser->description('A console tool for hashing strings')
  96. ->addSubcommand('string', [
  97. 'help' => 'Hash a string',
  98. 'parser' => [
  99. 'description' => 'hash',
  100. ]
  101. ])->addSubcommand('compare', [
  102. 'help' => 'Compare algs',
  103. 'parser' => [
  104. 'description' => 'Compare algs',
  105. ]
  106. ])->addSubcommand('time', [
  107. 'help' => 'Measure alg times',
  108. 'parser' => [
  109. 'description' => 'Measure alg times',
  110. ]
  111. ])->epilog(
  112. [
  113. 'sha1 is the default algorithm'
  114. ]
  115. );
  116. return $parser;
  117. }
  118. protected function _hash($type, $string) {
  119. if (in_array(strtolower($type), hash_algos())) {
  120. return hash($type, $string);
  121. }
  122. return $string;
  123. }
  124. }