/** * @param OutputInterface $output * @param PrivateKeyInterface $key */ public static function dumpPrivateKey(OutputInterface $output, PrivateKeyInterface $key) { $output->writeln('<comment>Private key information</comment>'); $output->writeln(''); $output->writeln('<info>Curve type</info> : ' . $key->getCurve()->getName()); $output->writeln('<info>Secret</info> : ' . $key->getSecret()); }
/** * */ private function calculateKey() { $this->checkExchangeState(); if ($this->secretKey === null) { $this->secretKey = $this->recipientKey->getPoint()->mul($this->senderKey->getSecret()); } }
/** * @param PrivateKeyInterface $key * @param $hash * @param $randomK * @return Signature */ public function sign(PrivateKeyInterface $key, $hash, $randomK) { $math = $this->adapter; $generator = $key->getPoint(); $modMath = $math->getModularArithmetic($generator->getOrder()); $k = $math->mod($randomK, $generator->getOrder()); $p1 = $generator->mul($k); $r = $p1->getX(); if ($math->cmp($r, 0) == 0) { throw new \RuntimeException("Error: random number R = 0"); } $hash = $this->truncateHash($generator, $hash); $s = $modMath->div($modMath->add($hash, $math->mul($key->getSecret(), $r)), $k); if ($math->cmp($s, 0) == 0) { throw new \RuntimeException("Error: random number S = 0"); } return new Signature($r, $s); }
/** * Construct a HMAC deterministic byte generator. * * @param MathAdapterInterface $math * @param PrivateKeyInterface $privateKey * @param string $messageHash * @param $algo * @internal param string $personalString */ public function __construct(MathAdapterInterface $math, PrivateKeyInterface $privateKey, $messageHash, $algo) { if (!in_array($algo, hash_algos())) { throw new \RuntimeException('HMACDRGB: Hashing algorithm not found'); } $tempHash = hash($algo, 1, true); $vlen = NumberSize::getCeiledByteSize($math, $math->hexdec(bin2hex($tempHash), 1)); // Initialize deterministic vectors $this->V = str_pad('', $vlen, chr(0x1), STR_PAD_LEFT); $this->K = str_pad('', $vlen, chr(0x0), STR_PAD_LEFT); $this->math = $math; $this->algorithm = $algo; $this->generator = $privateKey->getPoint(); // Encode the private key and hash as binary, a seed for the DRBG $entropy = pack("H*", $this->int2octets($privateKey->getSecret()) . $this->int2octets($messageHash)); $this->update($entropy); return $this; }
/** * Pkcs8PrivateKey constructor. * @param GmpMathInterface $math * @param DigestParamsInterface $kdfParams * @param CipherParamsInterface $cipherParams * @param PrivateKeyInterface $privateKey */ public function __construct(GmpMathInterface $math, DigestParamsInterface $kdfParams, CipherParamsInterface $cipherParams, PrivateKeyInterface $privateKey) { $this->kdfParams = $kdfParams; $this->cipherParams = $cipherParams; parent::__construct($math, $privateKey->getPoint(), $privateKey->getSecret()); }
private static function getUncompressedKeys(PrivateKeyInterface $privateKeyObject) { $pointSerializer = new UncompressedPointSerializer(EccFactory::getAdapter()); $vapid['publicKey'] = base64_encode(hex2bin($pointSerializer->serialize($privateKeyObject->getPublicKey()->getPoint()))); $vapid['privateKey'] = base64_encode(hex2bin(str_pad(gmp_strval($privateKeyObject->getSecret(), 16), 2 * self::PRIVATE_KEY_LENGTH, '0', STR_PAD_LEFT))); return $vapid; }
/** * @param PrivateKeyInterface $key * @return int|mixed|string */ private function formatKey(PrivateKeyInterface $key) { return $this->adapter->decHex($key->getSecret()); }
/** * @param EcDomain $domain * @param CertificateSubject $subject * @param PrivateKeyInterface $privateKey * @return Csr */ public static function getCsr(EcDomain $domain, CertificateSubject $subject, PrivateKeyInterface $privateKey) { $subjectSerializer = new CertificateSubjectSerializer(); $serialized = $subjectSerializer->serialize($subject); return new Csr($domain, $subject, $privateKey->getPublicKey(), $domain->getSigner()->sign($privateKey, $domain->getHasher()->hashDec($serialized), RandomGeneratorFactory::getUrandomGenerator()->generate($domain->getGenerator()->getOrder()))); }