Beispiel #1
0
 /**
  * @test
  *
  * @uses Lcobucci\JWT\Signer\Ecdsa::__construct
  * @uses Lcobucci\JWT\Signer\Key
  *
  * @covers Lcobucci\JWT\Signer\Ecdsa::createHash
  * @covers Lcobucci\JWT\Signer\Ecdsa::createSigningHash
  * @covers Lcobucci\JWT\Signer\Ecdsa::createSignatureHash
  */
 public function createHashShouldReturnAHashUsingPrivateKey()
 {
     $signer = $this->getSigner();
     $key = new Key('testing');
     $privateKey = $this->getMock(PrivateKeyInterface::class);
     $point = $this->getMock(PointInterface::class);
     $privateKey->method('getPoint')->willReturn($point);
     $point->method('getOrder')->willReturn('1');
     $this->parser->expects($this->once())->method('getPrivateKey')->with($key)->willReturn($privateKey);
     $this->randomGenerator->expects($this->once())->method('generate')->with('1')->willReturn('123');
     $this->adapter->expects($this->once())->method('hexDec')->willReturn('123');
     $this->adapter->expects($this->exactly(2))->method('decHex')->willReturn('123');
     $this->signer->expects($this->once())->method('sign')->with($privateKey, $this->isType('string'), $this->isType('string'))->willReturn(new Signature('1234', '456'));
     $this->assertInternalType('string', $signer->createHash('testing', $key, $this->randomGenerator));
 }
Beispiel #2
0
 /**
  * @test
  *
  * @covers \Lcobucci\JWT\Signer\Ecdsa\EccAdapter::createHash
  *
  * @uses \Lcobucci\JWT\Signer\Ecdsa\EccAdapter::__construct
  */
 public function createHashShouldReturnASerializedSignature()
 {
     $key = $this->createMock(PrivateKeyInterface::class);
     $point = $this->createMock(GeneratorPoint::class);
     $signature = $this->createMock(SignatureInterface::class);
     $order = gmp_init(1, 10);
     $randomK = gmp_init(2, 10);
     $signingHash = gmp_init(3, 10);
     $key->method('getPoint')->willReturn($point);
     $point->method('getOrder')->willReturn($order);
     $this->numberGenerator->expects($this->once())->method('generate')->with($order)->willReturn($randomK);
     $this->signer->expects($this->once())->method('sign')->with($key, $signingHash, $randomK)->willReturn($signature);
     $this->serializer->expects($this->once())->method('serialize')->with($signature, 'sha256')->willReturn('serialized_signature');
     $adapter = $this->createAdapter();
     self::assertEquals('serialized_signature', $adapter->createHash($key, $signingHash, 'sha256'));
 }
Beispiel #3
0
 /**
  * @return PrivateKey
  */
 public function createPrivateKey()
 {
     $secret = $this->generator->generate($this->getOrder());
     return new PrivateKey($this->getAdapter(), $this, $secret);
 }
Beispiel #4
0
 /**
  * @dataProvider getAdaptersWithRand
  */
 public function testSignatureValidityWithGeneratedKeys(MathAdapterInterface $math, RandomNumberGeneratorInterface $rng)
 {
     $generator = EccFactory::getNistCurves($math)->generator192();
     $signer = new Signer($math);
     $privateKey = $generator->createPrivateKey();
     $publicKey = $privateKey->getPublicKey();
     $randomK = $rng->generate($privateKey->getPoint()->getOrder());
     $hash = $rng->generate($generator->getOrder());
     $signature = $signer->sign($privateKey, $hash, $randomK);
     $this->assertTrue($signer->verify($publicKey, $signature, $hash), 'Correctly validates valid hash.');
     $this->assertFalse($signer->verify($publicKey, $signature, $math->sub($hash, 1)), 'Correctly rejects tampered hash.');
 }