Exemplo n.º 1
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);
 }
Exemplo n.º 2
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]);
});
Exemplo n.º 4
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);
 }