Exemple #1
0
 /**
  * PKCS8Serializer constructor.
  * @param DerPrivateKeySerializer|null $serializer
  * @param GmpMathInterface|null $adapter
  */
 public function __construct(DerPrivateKeySerializer $serializer = null, GmpMathInterface $adapter = null)
 {
     $this->serializer = $serializer ?: new DerPrivateKeySerializer();
     $this->adapter = $adapter ?: MathAdapterFactory::getAdapter();
     $this->crypter = new Crypter();
     $this->digester = new Digester();
     $this->pkcs5 = new Pkcs5v2Serializer();
 }
 /**
  * @dataProvider getAdapters
  */
 public function testOutputIsCompatibleWithOpenSSLOutput(MathAdapterInterface $adapter)
 {
     MathAdapterFactory::forceAdapter($adapter);
     $commandTester = $this->getCommandTester(new GeneratePublicKeyCommand(), 'encode-pubkey');
     $data = file_get_contents(__DIR__ . '/../../../data/openssl-priv.key');
     $expected = file_get_contents(__DIR__ . '/../../../data/openssl-pub.key');
     $commandTester->execute(array('data' => $data));
     $this->assertEquals($this->normalize($expected), $this->normalize($commandTester->getDisplay()));
     MathAdapterFactory::forceAdapter(null);
 }
 /**
  * @dataProvider getAdapters
  */
 public function testGenerateKeyPairWithPredefinedSecret(MathAdapterInterface $adapter)
 {
     $commandTester = $this->getCommandTester(new GenerateKeyPairCommand(), 'genkey');
     $expected = file_get_contents(__DIR__ . '/../../../data/generated-keypair.pem');
     $secret = '105886814118965842118146815191867355142743831281343651404754056074495577342758';
     $randomGenerator = $this->getMock($this->classRngInterface);
     $randomGenerator->expects($this->once())->method('generate')->willReturn($secret);
     MathAdapterFactory::forceAdapter($adapter);
     RandomGeneratorFactory::forceGenerator($randomGenerator);
     $commandTester->execute(array('--curve' => 'nist-p256'));
     $this->assertEquals($this->normalize($expected), $this->normalize($commandTester->getDisplay()));
     MathAdapterFactory::forceAdapter(null);
     RandomGeneratorFactory::forceGenerator(null);
 }
 /**
  * @param array $extra
  * @return array
  */
 protected function _getAdapters(array $extra = null)
 {
     if (!defined('PHPUNIT_DEBUG')) {
         define('PHPUNIT_DEBUG', false);
     }
     switch (MATH_LIB) {
         case 'gmp':
         default:
             $adapter = MathAdapterFactory::getGmpAdapter(PHPUNIT_DEBUG);
     }
     if ($extra == null) {
         return array(array($adapter));
     }
     $adapters = $this->_getAdapters(null);
     $result = [];
     foreach ($adapters as $adapter) {
         foreach ($extra as $value) {
             $result[] = array_merge($adapter, $value);
         }
     }
     return $result;
 }
 /**
  * @param PrivateKeyInterface $privateKey
  * @param $messageHash
  * @param $algo
  * @param bool                $debug
  * @return DebugDecorator|RandomNumberGeneratorInterface
  */
 public static function getHmacRandomGenerator(PrivateKeyInterface $privateKey, $messageHash, $algo, $debug = false)
 {
     return self::wrapAdapter(new HmacRandomNumberGenerator(MathAdapterFactory::getAdapter($debug), $privateKey, $messageHash, $algo), 'rfc6979', $debug);
 }
 public function getVectors()
 {
     $math = MathAdapterFactory::getAdapter();
     return [[$math, 1, '04'], [$math, 1, '41'], [$math, 4, '0488b21e']];
 }
Exemple #7
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $hex = $input->getArgument('dec');
     $adapter = MathAdapterFactory::getAdapter();
     $output->writeln($adapter->decHex($hex));
 }
 /**
  * @param MathAdapterInterface   $adapter
  * @param PemPublicKeySerializer $pubKeySerializer
  */
 public function __construct(MathAdapterInterface $adapter = null, PemPublicKeySerializer $pubKeySerializer = null)
 {
     $this->adapter = $adapter ?: MathAdapterFactory::getAdapter();
     $this->pubKeySerializer = $pubKeySerializer ?: new DerPublicKeySerializer($this->adapter);
 }
 /**
  *
  * @param MathAdapterInterface $adapter
  */
 public function __construct(MathAdapterInterface $adapter = null)
 {
     $this->adapter = $adapter ?: MathAdapterFactory::getAdapter();
     $this->formatter = new Formatter($this->adapter);
     $this->parser = new Parser($this->adapter);
 }
Exemple #10
0
 /**
  * @return SecgCurve
  */
 private static function getSecpFactory()
 {
     return new SecgCurve(MathAdapterFactory::getAdapter());
 }
Exemple #11
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $hex = str_replace(' ', '', $input->getArgument('hex'));
     $adapter = MathAdapterFactory::getAdapter();
     $output->writeln($adapter->hexDec($hex));
 }
Exemple #12
0
 /**
  * Selects and creates the most appropriate adapter for the running environment.
  *
  * @param $debug [optional] Set to true to get a trace of all mathematical operations
  *
  * @throws \RuntimeException
  * @return MathAdapterInterface
  */
 public static function getAdapter($debug = false)
 {
     return MathAdapterFactory::getAdapter($debug);
 }
Exemple #13
0
 /**
  * @param $content
  * @return int|string
  */
 private function hash($content)
 {
     $messages = new MessageFactory(MathAdapterFactory::getAdapter());
     return $messages->plaintext($content, 'sha256')->getHash();
 }