Skip to content

Commit

Permalink
feat: add new command: hash for gen md5, hash string
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 7, 2022
1 parent a71cc40 commit f51f2bf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/Console/Command/ToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +37,7 @@ protected function subCommands(): array
OpenCmd::class,
LnCommand::class,
HashHmacCommand::class,
HashCommand::class,
InstallCommand::class,
UpdateCommand::class,
ListToolCommand::class,
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,6 +89,7 @@ protected function subCommands(): array
{
return [
ParseUrlQueryCmd::class,
HashCommand::class,
];
}

Expand Down
62 changes: 62 additions & 0 deletions app/Console/SubCmd/ToolCmd/HashCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd\ToolCmd;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use function hash;
use function md5;
use function strtolower;
use function strtoupper;

/**
* class HashHmacCommand
*
* @author inhere
*/
class HashCommand extends Command
{
protected static string $name = 'hash';
protected static string $desc = 'Generate a keyed hash value using the given algo method';

protected function configure(): void
{
// $this->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);
}
}

0 comments on commit f51f2bf

Please sign in to comment.