Пример #1
0
 /**
  * @inheritdoc
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $secret = $input->getArgument('secret');
     if (empty($secret)) {
         /** @var QuestionHelper $dialog */
         $helper = $this->getHelper('question');
         $question = new Question('<question>The secret to share</question>: ');
         $secret = $helper->ask($input, $output, $question);
         $question = new Question('<question>Number of shared secrets to create</question> <comment>[3]</comment>: ', 3);
         $question->setValidator(function ($a) {
             if (!is_int($a) && !ctype_digit($a)) {
                 throw new \Exception('The number of shared secrets must be an integer');
             }
             return (int) $a;
         });
         $shares = $helper->ask($input, $output, $question);
         $question = new Question('<question>Number of shared secrets required</question> <comment>[2]</comment>: ', 2);
         $question->setValidator(function ($a) {
             if (!is_int($a) && !ctype_digit($a)) {
                 throw new \Exception('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);
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var array $shares */
     $shares = $input->getArgument('shares');
     if (empty($shares)) {
         /** @var QuestionHelper $dialog */
         $helper = $this->getHelper('question');
         $question = new Question('<question>Shared secret</question> <comment>[empty to stop]</comment>: ');
         $shares = [];
         while (($share = trim($helper->ask($input, $output, $question))) != '') {
             $shares[] = $share;
         }
     }
     $shared = Secret::recover($shares);
     /** @var FormatterHelper $formatter */
     $formatter = $this->getHelper('formatter');
     $block = $formatter->formatBlock($shared, 'info', true);
     $output->writeln($block);
 }
Пример #3
0
#!/usr/bin/env php
<?php 
require_once __DIR__ . '/../vendor/autoload.php';
use TQ\Shamir\Secret;
// create 5 shares with a threshold of 2, so you will need a minimum
// of 2 shares to recover the secret.
$shares = Secret::share('Shamir\'s Shared Secret Implementation in PHP', 5, 2);
var_dump($shares);
// we can use different keys to recover the data, but we need at least 2 of them
var_dump(Secret::recover(array_slice($shares, 0, 2)));
var_dump(Secret::recover(array_slice($shares, 1, 2)));
                if (!isset($fieldValue) || trim($fieldValue) == '') {
                    throw new \Exception('All of the fields are required.');
                }
            }
            if (!filter_var($sharesThreshold, FILTER_VALIDATE_INT) || !filter_var($sharesAmount, FILTER_VALIDATE_INT)) {
                throw new \Exception('The threshold and amount of shares must be integers.');
            }
            if ($sharesThreshold > $sharesAmount) {
                throw new \Exception('The threshold must be lower than or equal to the amount of shares.');
            }
            $status = 'success';
            $response = Secret::share($secret, $sharesAmount, $sharesThreshold);
        } catch (Exception $exception) {
            $status = 'error';
            $response = $exception->getMessage();
        }
    }
    // Recover
    if ($action === 'recover') {
        $shares = $request->input('shares');
        try {
            $status = 'success';
            $response = Secret::recover(array_filter($shares));
        } catch (Exception $exception) {
            $status = 'error';
            $response = $exception->getMessage();
        }
    }
    // Return response
    return view('index', ['version' => $app->version(), 'action' => $action, 'status' => empty($status) ? null : $status, 'response' => json_encode($response), 'shareAmount' => empty($sharesAmount) ? null : $sharesAmount, 'shareThreshold' => empty($sharesThreshold) ? null : $sharesThreshold]);
});
Пример #5
0
 public function testShareAndRecoverThreeBytes()
 {
     $secret = 'abc ABC 123 !@# ,./ \'"\\ <>?';
     $shares = Secret::share($secret, 75000, 2);
     $recover = Secret::recover(array_slice($shares, 0, 2));
     $this->assertSame($secret, $recover);
 }