Rfc6979::bytes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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));
0 ignored issues
show
Bug introduced by
It seems like gmp_init($privateKey->getInt(), 10) can also be of type resource; however, parameter $secretMultiplier of Mdanter\Ecc\Crypto\Key\PrivateKey::__construct() does only seem to accept GMP, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $mdPk = new MdPrivateKey($ecAdapter->getMath(), $ecAdapter->getGenerator(), /** @scrutinizer ignore-type */ gmp_init($privateKey->getInt(), 10));
Loading history...
41 40
        $this->ecAdapter = $ecAdapter;
42 40
        $this->hmac = RandomGeneratorFactory::getHmacRandomGenerator($mdPk, gmp_init($messageHash->getInt(), 10), $algo);
0 ignored issues
show
Bug introduced by
It seems like gmp_init($messageHash->getInt(), 10) can also be of type resource; however, parameter $messageHash of Mdanter\Ecc\Random\Rando...etHmacRandomGenerator() does only seem to accept GMP, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $this->hmac = RandomGeneratorFactory::getHmacRandomGenerator($mdPk, /** @scrutinizer ignore-type */ gmp_init($messageHash->getInt(), 10), $algo);
Loading history...
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