SignatureTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace kornrunner;
  3. use kornrunner\Serializer\HexPrivateKeySerializer;
  4. use kornrunner\Signature\Signer;
  5. use Mdanter\Ecc\Crypto\Signature\Signature;
  6. use Mdanter\Ecc\Curves\CurveFactory;
  7. use Mdanter\Ecc\Curves\SecgCurve;
  8. use Mdanter\Ecc\EccFactory;
  9. use Mdanter\Ecc\Random\RandomGeneratorFactory;
  10. class SignatureTest extends TestCase
  11. {
  12. public function testToHex() {
  13. $adapter = EccFactory::getAdapter();
  14. $generator = CurveFactory::getGeneratorByName(SecgCurve::NAME_SECP_256K1);
  15. $deserializer = new HexPrivateKeySerializer($generator);
  16. $key = $deserializer->parse($this->testPrivateKey);
  17. $hash = gmp_init('98d22cdb65bbf8a392180cd2ee892b0a971c47e7d29daf31a3286d006b9db4dc', 16);
  18. $random = RandomGeneratorFactory::getHmacRandomGenerator($key, $hash, 'sha256');
  19. $n = $generator->getOrder();
  20. $randomK = $random->generate($n);
  21. $options = [
  22. 'n' => $n,
  23. 'canonical' => true
  24. ];
  25. $signer = new Signer($adapter, $options);
  26. $signature = $signer->sign($key, $hash, $randomK);
  27. $this->assertTrue($signature instanceof Signature);
  28. $this->assertEquals('f67118680df5993e8efca4d3ecc4172ca4ac5e3e007ea774293e37386480970347427f3633371c1a30abbb2b717dbd78ef63d5b19b5a951f9d681cccdd520320', $signature->toHex());
  29. $this->assertEquals(0, $signature->getRecoveryParam());
  30. }
  31. }