forked from phpecc/phpecc
-
Notifications
You must be signed in to change notification settings - Fork 3
/
verify_signature.php
40 lines (31 loc) · 1.29 KB
/
verify_signature.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
require __DIR__ . "/../vendor/autoload.php";
use Mdanter\Ecc\Crypto\Signature\SignHasher;
use Mdanter\Ecc\EccFactory;
use Mdanter\Ecc\Crypto\Signature\Signer;
use Mdanter\Ecc\Serializer\PublicKey\PemPublicKeySerializer;
use Mdanter\Ecc\Serializer\PublicKey\DerPublicKeySerializer;
use Mdanter\Ecc\Serializer\Signature\DerSignatureSerializer;
# Same parameters as creating_signature.php
$adapter = EccFactory::getAdapter();
$generator = EccFactory::getNistCurves()->generator256(null, true);
$algorithm = 'sha256';
$sigData = base64_decode('MEQCIHK+HXgq0AjeKfmdI9l4uGBL0keIiZiQOCEyij25B/X/AiAQs++18Vhb0Q9tqWjzWUNTAMLEzUKF0XzKyHQ028/q4Q==');
$document = 'I am writing today...';
// Parse signature
$sigSerializer = new DerSignatureSerializer();
$sig = $sigSerializer->parse($sigData);
// Parse public key
$keyData = file_get_contents(__DIR__ . '/../tests/data/openssl-secp256r1.pub.pem');
$derSerializer = new DerPublicKeySerializer($adapter);
$pemSerializer = new PemPublicKeySerializer($derSerializer);
$key = $pemSerializer->parse($keyData);
$hasher = new SignHasher($algorithm);
$hash = $hasher->makeHash($document, $generator);
$signer = new Signer($adapter);
$check = $signer->verify($key, $sig, $hash);
if ($check) {
echo "Signature verified\n";
} else {
echo "Signature validation failed\n";
}