diff --git a/app/Console/Command/ToolCommand.php b/app/Console/Command/ToolCommand.php index 939b2a4..43743f9 100644 --- a/app/Console/Command/ToolCommand.php +++ b/app/Console/Command/ToolCommand.php @@ -13,6 +13,7 @@ use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; use Inhere\Kite\Console\SubCmd\OpenCmd; +use Inhere\Kite\Console\SubCmd\ToolCmd\HashCommand; use Inhere\Kite\Console\SubCmd\ToolCmd\HashHmacCommand; use Inhere\Kite\Console\SubCmd\ToolCmd\InstallCommand; use Inhere\Kite\Console\SubCmd\ToolCmd\ListToolCommand; @@ -36,6 +37,7 @@ protected function subCommands(): array OpenCmd::class, LnCommand::class, HashHmacCommand::class, + HashCommand::class, InstallCommand::class, UpdateCommand::class, ListToolCommand::class, diff --git a/app/Console/Controller/StringController.php b/app/Console/Controller/StringController.php index 25f0b85..6fc12df 100644 --- a/app/Console/Controller/StringController.php +++ b/app/Console/Controller/StringController.php @@ -16,6 +16,7 @@ use Inhere\Kite\Console\Component\ContentsAutoReader; use Inhere\Kite\Console\Component\ContentsAutoWriter; use Inhere\Kite\Console\SubCmd\ParseUrlQueryCmd; +use Inhere\Kite\Console\SubCmd\ToolCmd\HashCommand; use Inhere\Kite\Helper\AppHelper; use Inhere\Kite\Helper\KiteUtil; use Inhere\Kite\Kite; @@ -88,6 +89,7 @@ protected function subCommands(): array { return [ ParseUrlQueryCmd::class, + HashCommand::class, ]; } diff --git a/app/Console/SubCmd/ToolCmd/HashCommand.php b/app/Console/SubCmd/ToolCmd/HashCommand.php new file mode 100644 index 0000000..52f21ec --- /dev/null +++ b/app/Console/SubCmd/ToolCmd/HashCommand.php @@ -0,0 +1,62 @@ +flags->addOptByRule($name, $rule); + $this->flags->addArg('str', 'want to signed string. allow: @c', 'string', true); + $this->flags->addOpt('algo', 'a', 'Name of selected hashing algorithm. eg: md5, sha256', 'string', false, 'md5'); + // $this->flags->addOpt( + // 'key', 'k', + // 'Shared secret key used for generating the HMAC variant of the message digest.', + // 'string', true); + } + + protected function execute(Input $input, Output $output) + { + $fs = $this->flags; + + // $secKey = $fs->getOpt('key'); + $rawStr = $fs->getArg('str'); + $rawStr = ContentsAutoReader::readFrom($rawStr); + + $algoName = strtolower($fs->getOpt('algo')); + $output->aList([ + // 'key' => $secKey, + 'algo' => $algoName, + ]); + $output->colored('RAW STRING:'); + $output->println($rawStr); + + if ($algoName === 'md5') { + $signStr = md5($rawStr); + } else { + $signStr = hash($algoName, $rawStr); + } + + $signStr = strtoupper($signStr); + + $output->colored('SIGN:'); + $output->println($signStr); + } +}