1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Crypto\Random; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
8
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; |
9
|
|
|
use BitWasp\Buffertools\Buffer; |
10
|
|
|
use BitWasp\Buffertools\BufferInterface; |
11
|
|
|
use Mdanter\Ecc\Crypto\Key\PrivateKey as MdPrivateKey; |
12
|
|
|
use Mdanter\Ecc\Random\RandomGeneratorFactory; |
13
|
|
|
use Mdanter\Ecc\Random\RandomNumberGeneratorInterface; |
14
|
|
|
|
15
|
|
|
class Rfc6979 implements RbgInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var EcAdapterInterface |
20
|
|
|
*/ |
21
|
|
|
private $ecAdapter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var RandomNumberGeneratorInterface |
25
|
|
|
*/ |
26
|
|
|
private $hmac; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param EcAdapterInterface $ecAdapter |
30
|
|
|
* @param PrivateKeyInterface $privateKey |
31
|
|
|
* @param BufferInterface $messageHash |
32
|
|
|
* @param string $algo |
33
|
|
|
*/ |
34
|
40 |
|
public function __construct( |
35
|
|
|
EcAdapterInterface $ecAdapter, |
36
|
|
|
PrivateKeyInterface $privateKey, |
37
|
|
|
BufferInterface $messageHash, |
38
|
|
|
string $algo = 'sha256' |
39
|
|
|
) { |
40
|
40 |
|
$mdPk = new MdPrivateKey($ecAdapter->getMath(), $ecAdapter->getGenerator(), gmp_init($privateKey->getInt(), 10)); |
|
|
|
|
41
|
40 |
|
$this->ecAdapter = $ecAdapter; |
42
|
40 |
|
$this->hmac = RandomGeneratorFactory::getHmacRandomGenerator($mdPk, gmp_init($messageHash->getInt(), 10), $algo); |
|
|
|
|
43
|
40 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param int $bytes |
47
|
|
|
* @return BufferInterface |
48
|
|
|
*/ |
49
|
36 |
|
public function bytes(int $bytes): BufferInterface |
50
|
|
|
{ |
51
|
36 |
|
$integer = $this->hmac->generate($this->ecAdapter->getOrder()); |
52
|
36 |
|
return Buffer::int(gmp_strval($integer, 10), $bytes); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|