/** * @dataProvider getVectors * @param string $curveName * @param string|int $multiplier * @param string $expectedPub */ public function testSerialize($curveName, $multiplier, $expectedPub) { /** @var GeneratorPoint $generator */ $generator = Curves::generator($curveName); $privateKey = $generator->getPrivateKeyFrom(gmp_init($multiplier, 10)); $public = $privateKey->getPublicKey(); $adapter = EccFactory::getAdapter(); $serializer = new SshPublicKeySerializer($adapter, new UncompressedPointSerializer($adapter)); $serialized = $serializer->serialize($curveName, $public); $this->assertEquals($expectedPub, $serialized); list($curve, $publicKey) = $serializer->unserialize($serialized); $this->assertTrue($public->getPoint()->equals($publicKey->getPoint())); }
/** * @param InputInterface $input * @param OutputInterface $output * @param QuestionHelper $helper * @param string $curveName * @param bool $useEncryption * @return array */ public function generateKeyData(InputInterface $input, OutputInterface $output, QuestionHelper $helper, $curveName, $useEncryption) { if (!is_bool($useEncryption)) { throw new \InvalidArgumentException('useEncryption parameter must be a boolean'); } /** * @var GeneratorPoint $generator */ list(, $generator) = Curves::load($curveName); $key = $generator->createPrivateKey(); if ($useEncryption) { $password = $this->promptForPassword($input, $output, $helper); $encrypted = new EncryptedPrivateKey($key, 'AES-128-CBC', random_bytes(16)); $serializer = new EncryptedPrivateKeySerializer(new DerPrivateKeySerializer()); $keyData = $serializer->serialize($encrypted, $password); } else { $serializer = new PemPrivateKeySerializer(new DerPrivateKeySerializer()); $keyData = $serializer->serialize($key); } $adapter = EccFactory::getAdapter(); $publicKey = $key->getPublicKey(); $publicSerializer = new SshPublicKeySerializer($adapter, new UncompressedPointSerializer($adapter)); $publicData = $publicSerializer->serialize($curveName, $publicKey); $localUser = posix_getpwuid(posix_geteuid()); $localHost = gethostname(); $publicData = sprintf("ecdsa-sha2-%s %s %s@%s\n", $curveName, $publicData, $localUser['name'], $localHost); return [$keyData, $publicData]; }