Пример #1
0
 public function testGenerateAndSign()
 {
     $key = new KeyPair();
     $key->generate();
     $data_to_sign = 'The quick brown fox jumps over the lazy dog';
     $signature = $key->sign($data_to_sign);
     $this->assertTrue($key->verify($data_to_sign, $signature));
 }
Пример #2
0
 public function testSignAndVerify()
 {
     $key = new KeyPair();
     $key->generate();
     $key->store($this->pubkey, $this->privkey);
     $this->assertTrue(file_exists($this->pubkey));
     $this->assertTrue(file_exists($this->privkey));
     $client = new Client();
     $client->setPrivateKey($this->privkey);
     $data = ['fox' => 'quick', 'colour' => 'brown', 'dog' => 'lazy'];
     $signature = $client->createSignature($data);
     $this->assertNotEmpty($signature);
     $this->assertTrue(is_string($data['cnonce']));
     $this->assertTrue(is_string($data['sig']));
     $server = new Server();
     $server->setPublicKey($this->pubkey);
     $server->verifySignature($data);
     $this->assertTrue(true);
     // Can't verify without signature
     unset($data['sig']);
     try {
         $server->verifySignature($data);
         $this->assertTrue(false);
     } catch (SignatureException $e) {
         $this->assertTrue(true);
     }
     // Can't verify garbage signature
     $data['sig'] = 'Garbage';
     try {
         $server->verifySignature($data);
         $this->assertTrue(false);
     } catch (SignatureException $e) {
         $this->assertTrue(true);
     }
     // Can't verify without cnonce
     $data = ['fox' => 'quick', 'colour' => 'brown', 'dog' => 'lazy'];
     $signature = $client->createSignature($data);
     $this->assertNotEmpty($signature);
     unset($data['cnonce']);
     try {
         $server->verifySignature($data);
         $this->assertTrue(false);
     } catch (SignatureException $e) {
         $this->assertTrue(true);
     }
 }