HashShell.php 3.6 KB

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