Created
March 10, 2017 02:54
-
-
Save Kruithne/e2d35e7510d2c9590c5033f0dcfa5e10 to your computer and use it in GitHub Desktop.
Hashing Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo PHP_EOL; | |
// Command information, used by showHelp(). | |
$commands = [ | |
'-h' => 'Display this list of parameters.', | |
'-l' => 'List available hashing methods.', | |
'-m' => 'Select hashing method (Required).', | |
'-f' => 'Treat input as a file path.', | |
'-n' => 'Split input by line-end, hashing each line.', | |
'-o' => 'File to dump output.' | |
]; | |
// List available hashing methods, then terminate script. | |
function listMethods() { | |
echo 'Available hashing methods:' . PHP_EOL; | |
foreach (hash_algos() as $method) | |
echo "\t" . $method . PHP_EOL; | |
die(PHP_EOL); | |
} | |
// Display script help, then terminate script. | |
function showHelp() { | |
global $commands; | |
echo 'Usage: hash [parameters] input'. PHP_EOL; | |
echo 'Available parameters:' . PHP_EOL . PHP_EOL; | |
foreach ($commands as $command => $commandInfo) | |
echo "\t" . $command . ' -- ' . $commandInfo . PHP_EOL; | |
die(PHP_EOL); | |
} | |
// Get command-line options. | |
$options = getopt('hlfm:no:', [], $out); | |
// Check if -h is set, if so, show help and terminate. | |
if (array_key_exists('h', $options)) | |
showHelp(); | |
// Check if -l is set, if so, list hashing methods and terminate. | |
if (array_key_exists('l', $options)) | |
listMethods(); | |
// Check if the method was set via -m. | |
$method = $options['m']; | |
if ($method === null) | |
showHelp(); // No method provided. | |
$method = strtolower($method); | |
if (!in_array($method, hash_algos())) { | |
echo 'Invalid hashing method: "' . $method . '"' . PHP_EOL; | |
listMethods(); // Invalid method provided. | |
} | |
echo 'Method: ' . $method . PHP_EOL; | |
// Input | |
$input = implode(' ', array_slice($argv, $out)); | |
if (array_key_exists('f', $options)) { | |
if (!file_exists($input)) | |
die('Input file does not exist: ' . $input); | |
if (!is_file($input)) | |
die('Input file is not a file: ' . $input); | |
$data = file_get_contents($input); | |
if ($data === false) | |
die('Input file cannot be read: ' . $input); | |
$input = $data; | |
} | |
$nodes = []; | |
if (array_key_exists('n', $options)) // Split input by line-end. | |
$nodes = preg_split ('/$\R?^/m', $input); | |
else | |
$nodes[] = $input; // Hash all input as one. | |
$outputFile = $options['o'] ?? null; | |
foreach ($nodes as $node) { | |
$result = hash($method, $node); | |
if ($outputFile === null) { | |
echo 'Input: (' . strlen($node) . ') "' . $node . '"' . PHP_EOL; | |
// Compute | |
echo 'Result: (' . strlen($result) . ') "' . $result . '"' . PHP_EOL . PHP_EOL; | |
} else { | |
file_put_contents($outputFile, $result . "\n", FILE_APPEND); | |
} | |
} | |
if ($outputFile !== null) | |
echo 'Done!' . PHP_EOL . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment