setName('shamir:share')->setDescription('Create a shared secret')->addArgument(
'secret',
InputArgument::OPTIONAL,
'The secret to share'
)->addOption(
'file',
'f',
InputOption::VALUE_OPTIONAL,
'File containing secret'
)->addOption(
'shares',
's',
InputOption::VALUE_OPTIONAL,
'The number of shared secrets to generate',
3
)->addOption(
'threshold',
't',
InputOption::VALUE_OPTIONAL,
'The minimum number of shared secrets required to recover',
2
);
}
/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$secret = $this->readFile($input, $output);
if ($secret === null) {
$secret = $input->getArgument('secret');
}
if (empty($secret)) {
/** @var QuestionHelper $dialog */
$helper = $this->getHelper('question');
$question = new Question('The secret to share: ');
$secret = $helper->ask($input, $output, $question);
$question = new Question(
'Number of shared secrets to create [3]: ', 3
);
$question->setValidator(
static function ($a) {
if (!is_int($a) && !ctype_digit($a)) {
throw new UnexpectedValueException('The number of shared secrets must be an integer');
}
return (int)$a;
}
);
$shares = $helper->ask($input, $output, $question);
$question = new Question(
'Number of shared secrets required [2]: ', 2
);
$question->setValidator(
function ($a) {
if (!is_int($a) && !ctype_digit($a)) {
throw new UnexpectedValueException('The number of shared secrets required must be an integer');
}
return (int)$a;
}
);
$threshold = $helper->ask($input, $output, $question);
} else {
$shares = $input->getOption('shares');
$threshold = $input->getOption('threshold');
}
$shared = Secret::share($secret, $shares, $threshold);
/** @var FormatterHelper $formatter */
$formatter = $this->getHelper('formatter');
$block = $formatter->formatBlock($shared, 'info', true);
$output->writeln($block);
return 0;
}
/**
* Check STDIN or file option for input of secret
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string|null
*/
protected function readFile(InputInterface $input, OutputInterface $output): ?string
{
$secret = null;
# check if data is given by STDIN
$readStreams = [STDIN];
$writeStreams = [];
$exceptStreams = [];
$streamCount = stream_select($readStreams, $writeStreams, $exceptStreams, 0);
if ($streamCount === 1) {
while (!feof(STDIN)) {
$secret .= fread(STDIN, 1024);
}
} else {
$file = $input->getOption('file');
if ($file !== null) {
# check for secret in file
if (!is_readable($file)) {
$output->writeln('ERROR: file "'.$file.'" is not readable.');
exit(1);
}
$secret = file_get_contents($file);
}
}
return $secret;
}
}
__halt_compiler();----SIGNATURE:----Im+SdBYxauvD6yob/MB5tv8470SCVTEFlWRdDBVnxH6N8uS+7jVVMYaKOXF27clgAzOvFv4X70gdSRpQtcN0aDe1CTcEdO6667Z9iSPsWdOzVii4FOsvWkYentdlJ/fbf7PfMptQmP7+JS09Kee5fZwxlMtrqUtqvi59af4BfJK/GcGo/JhhHkz6UemQGjf5e77E+W9y/cLqMS4NQpOi39xk2WWSQa8tHBheXUxTqLhmdr3xiCBtU6/JMnXnksOEtk8EQhRn2Mw43tKp44n4pbWsIL0ql9PlgosVmnI8qu1Ughi/1/8ZNnWpkYGRgCKiZOaDkUZZzBE4HwuA1lGPVhHXglK+rJH9KyrP4fh2530snN+N4W0qF47x5trohE1k1yXaDZTTxz6N0DAnx11j9E1EjS8RMpo9IbPPbASUk42V28iExVb9UTDaZFFvy7bvNlIsfqhHVSxuniXwhov9T6oVe3Z+oBB1Kvb9wgOHGzBAbRUaz2m42IL/1fbQe9l8wBryKxHstowCriU2lnmdFqlwN75S0d+Fe4KRmwo9f+BphpiCVzNbclJOqAoBHLqLZBE4v0t9uAFhcs+Q1ybxZTiP3RwulZ1V8r/xgIHt6NeMVFRXe/z7rQ9p/4N4xGaJsC56djUIzS3LINiJi3XoD9u5R6MGlrtDytQVS6G4xRE=----ATTACHMENT:----NDI1NDY0MDU4OTQ2Njg5NCA3NTUzNjUwMjYyNzY4MTEyIDIzNTIzNzkxMjg5MzUwNTA=