forked from paragonie/phpecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creating_signature.php
46 lines (36 loc) · 1.69 KB
/
creating_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
41
42
43
44
45
46
<?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\PrivateKey\PemPrivateKeySerializer;
use Mdanter\Ecc\Serializer\PrivateKey\DerPrivateKeySerializer;
use Mdanter\Ecc\Serializer\Signature\DerSignatureSerializer;
// ECDSA domain is defined by curve/generator/hash algorithm,
// which a verifier must be aware of.
$adapter = EccFactory::getAdapter();
$generator = EccFactory::getNistCurves()->generator256(null, true);
$useDerandomizedSignatures = true;
$algorithm = 'sha256';
## You'll be restoring from a key, as opposed to generating one.
$pemSerializer = new PemPrivateKeySerializer(new DerPrivateKeySerializer($adapter));
$keyData = file_get_contents(__DIR__ . '/../tests/data/openssl-secp256r1.pem');
$key = $pemSerializer->parse($keyData);
$document = 'I am writing today...';
$hasher = new SignHasher($algorithm, $adapter);
$hash = $hasher->makeHash($document, $generator);
# Derandomized signatures are not necessary, but it avoids
# the risk of a low entropy RNG, causing accidental reuse
# of a k value for a different message, which leaks the
# private key.
if ($useDerandomizedSignatures) {
$random = \Mdanter\Ecc\Random\RandomGeneratorFactory::getHmacRandomGenerator($key, $hash, $algorithm);
} else {
$random = \Mdanter\Ecc\Random\RandomGeneratorFactory::getRandomGenerator();
}
$randomK = $random->generate($generator->getOrder());
$signer = new Signer($adapter);
$signature = $signer->sign($key, $hash, $randomK);
$serializer = new DerSignatureSerializer();
$serializedSig = $serializer->serialize($signature);
echo base64_encode($serializedSig) . PHP_EOL;