ECDHTest.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. require_once __DIR__ . "/../vendor/autoload.php";
  3. class ECDHTest extends \PHPUnit\Framework\TestCase {
  4. public function test_should_work_with_secp256k1_curve() {
  5. $this->doTest('secp256k1');
  6. }
  7. public function test_should_work_with_p256_curve() {
  8. $this->doTest('p256');
  9. }
  10. public function test_should_work_with_curve25519_curve() {
  11. $this->doTest('curve25519');
  12. }
  13. public function test_should_work_with_ed25519_curve() {
  14. $this->doTest('ed25519');
  15. }
  16. function doTest($name) {
  17. $ecdh = new \Elliptic\EC($name);
  18. $s1 = $ecdh->genKeyPair();
  19. $s2 = $ecdh->genKeyPair();
  20. $sh1 = $s1->derive($s2->getPublic());
  21. $sh2 = $s2->derive($s1->getPublic());
  22. $this->assertEquals($sh1->toString(16), $sh2->toString(16));
  23. $sh1 = $s1->derive($ecdh->keyFromPublic($s2->getPublic('hex'), 'hex')
  24. ->getPublic());
  25. $sh2 = $s2->derive($ecdh->keyFromPublic($s1->getPublic('hex'), 'hex')
  26. ->getPublic());
  27. $this->assertEquals($sh1->toString(16), $sh2->toString(16));
  28. }
  29. }