Completed
Pull Request — master (#758)
by thomas
22:04
created

Hash::sha256ripe160()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
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;
6
7
use BitWasp\Buffertools\Buffer;
8
use BitWasp\Buffertools\BufferInterface;
9
use lastguest\Murmur;
10
11
class Hash
12
{
13
    /**
14
     * Calculate Sha256(RipeMd160()) on the given data
15
     *
16
     * @param BufferInterface $data
17
     * @return BufferInterface
18
     */
19 492
    public static function sha256ripe160(BufferInterface $data): BufferInterface
20
    {
21 492
        return new Buffer(hash('ripemd160', hash('sha256', $data->getBinary(), true), true), 20);
22
    }
23
24
    /**
25
     * Perform SHA256
26
     *
27
     * @param BufferInterface $data
28
     * @return BufferInterface
29
     */
30 362
    public static function sha256(BufferInterface $data): BufferInterface
31
    {
32 362
        return new Buffer(hash('sha256', $data->getBinary(), true), 32);
33
    }
34
35
    /**
36
     * Perform SHA256 twice
37
     *
38
     * @param BufferInterface $data
39
     * @return BufferInterface
40
     */
41 5524
    public static function sha256d(BufferInterface $data): BufferInterface
42
    {
43 5524
        return new Buffer(hash('sha256', hash('sha256', $data->getBinary(), true), true), 32);
44
    }
45
46
    /**
47
     * RIPEMD160
48
     *
49
     * @param BufferInterface $data
50
     * @return BufferInterface
51
     */
52 21
    public static function ripemd160(BufferInterface $data): BufferInterface
53
    {
54 21
        return new Buffer(hash('ripemd160', $data->getBinary(), true), 20);
55
    }
56
57
    /**
58
     * RIPEMD160 twice
59
     *
60
     * @param BufferInterface $data
61
     * @return BufferInterface
62
     */
63 1
    public static function ripemd160d(BufferInterface $data): BufferInterface
64
    {
65 1
        return new Buffer(hash('ripemd160', hash('ripemd160', $data->getBinary(), true), true), 20);
66
    }
67
68
    /**
69
     * Calculate a SHA1 hash
70
     *
71
     * @param BufferInterface $data
72
     * @return BufferInterface
73
     */
74 25
    public static function sha1(BufferInterface $data): BufferInterface
75
    {
76 25
        return new Buffer(hash('sha1', $data->getBinary(), true), 20);
77
    }
78
79
    /**
80
     * PBKDF2
81
     *
82
     * @param string $algorithm
83
     * @param BufferInterface $password
84
     * @param BufferInterface $salt
85
     * @param integer $count
86
     * @param integer $keyLength
87
     * @return BufferInterface
88
     * @throws \Exception
89
     */
90 42
    public static function pbkdf2(string $algorithm, BufferInterface $password, BufferInterface $salt, int $count, int $keyLength): BufferInterface
91
    {
92 42
        if ($keyLength < 0) {
93
            throw new \InvalidArgumentException('Cannot have a negative key-length for PBKDF2');
94
        }
95
96 42
        $algorithm  = strtolower($algorithm);
97
98 42
        if (!in_array($algorithm, hash_algos(), true)) {
99 1
            throw new \Exception('PBKDF2 ERROR: Invalid hash algorithm');
100
        }
101
102 41
        if ($count <= 0 || $keyLength <= 0) {
103 1
            throw new \Exception('PBKDF2 ERROR: Invalid parameters.');
104
        }
105
106 40
        return new Buffer(\hash_pbkdf2($algorithm, $password->getBinary(), $salt->getBinary(), $count, $keyLength, true), $keyLength);
107
    }
108
109
    /**
110
     * @param BufferInterface $data
111
     * @param int $seed
112
     * @return BufferInterface
113
     */
114 13
    public static function murmur3(BufferInterface $data, int $seed): BufferInterface
115
    {
116 13
        return new Buffer(pack('N', Murmur::hash3_int($data->getBinary(), $seed)), 4);
117
    }
118
119
    /**
120
     * Do HMAC hashing on $data and $salt
121
     *
122
     * @param string $algo
123
     * @param BufferInterface $data
124
     * @param BufferInterface $salt
125
     * @return BufferInterface
126
     */
127 40
    public static function hmac(string $algo, BufferInterface $data, BufferInterface $salt): BufferInterface
128
    {
129 40
        return new Buffer(hash_hmac($algo, $data->getBinary(), $salt->getBinary(), true));
130
    }
131
132
    /**
133
     * Creates a tagged sha256 hash per bip-schnorr
134
     *
135
     * @param string $tag
136
     * @param BufferInterface $data
137
     * @return BufferInterface
138
     * @throws \Exception
139
     */
140
    public static function taggedSha256(string $tag, BufferInterface $data): BufferInterface
141
    {
142
        $taghash = hash('sha256', $tag, true);
143
        $ctx = hash_init('sha256');
144
        hash_update($ctx, $taghash);
145
        hash_update($ctx, $taghash);
146
        hash_update($ctx, $data->getBinary());
147
        return new Buffer(hash_final($ctx, true));
148
    }
149
}
150