Completed
Push — master ( 2cb166...4a378c )
by thomas
335:28 queued 265:27
created

Hash::ripemd160d()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Crypto;
4
5
use BitWasp\Buffertools\Buffer;
6
use BitWasp\Buffertools\BufferInterface;
7
8
class Hash
9
{
10
    /**
11
     * Calculate Sha256(RipeMd160()) on the given data
12
     *
13
     * @param BufferInterface $data
14
     * @return BufferInterface
15
     */
16 592
    public static function sha256ripe160(BufferInterface $data)
17
    {
18 592
        return new Buffer(hash('ripemd160', hash('sha256', $data->getBinary(), true), true), 20);
19
    }
20
21
    /**
22
     * Perform SHA256
23
     *
24
     * @param BufferInterface $data
25
     * @return BufferInterface
26
     */
27 408
    public static function sha256(BufferInterface $data)
28
    {
29 408
        return new Buffer(hash('sha256', $data->getBinary(), true), 32);
30
    }
31
32
    /**
33
     * Perform SHA256 twice
34
     *
35
     * @param BufferInterface $data
36
     * @return BufferInterface
37
     */
38 4090
    public static function sha256d(BufferInterface $data)
39
    {
40 4090
        return new Buffer(hash('sha256', hash('sha256', $data->getBinary(), true), true), 32);
41
    }
42
43
    /**
44
     * RIPEMD160
45
     *
46
     * @param BufferInterface $data
47
     * @return BufferInterface
48
     */
49 16
    public static function ripemd160(BufferInterface $data)
50
    {
51 16
        return new Buffer(hash('ripemd160', $data->getBinary(), true), 20);
52
    }
53
54
    /**
55
     * RIPEMD160 twice
56
     *
57
     * @param BufferInterface $data
58
     * @return BufferInterface
59
     */
60 6
    public static function ripemd160d(BufferInterface $data)
61
    {
62 6
        return new Buffer(hash('ripemd160', hash('ripemd160', $data->getBinary(), true), true), 20);
63
    }
64
65
    /**
66
     * Calculate a SHA1 hash
67
     *
68
     * @param BufferInterface $data
69
     * @return BufferInterface
70
     */
71 18
    public static function sha1(BufferInterface $data)
72
    {
73 18
        return new Buffer(hash('sha1', $data->getBinary(), true), 20);
74
    }
75
76
    /**
77
     * PBKDF2
78
     *
79
     * @param string $algorithm
80
     * @param BufferInterface $password
81
     * @param BufferInterface $salt
82
     * @param integer $count
83
     * @param integer $keyLength
84
     * @return BufferInterface
85
     * @throws \Exception
86
     */
87 162
    public static function pbkdf2($algorithm, BufferInterface $password, BufferInterface $salt, $count, $keyLength)
88
    {
89 162
        if ($keyLength < 0) {
90
            throw new \InvalidArgumentException('Cannot have a negative key-length for PBKDF2');
91 162
        }
92 6
93
        $algorithm  = strtolower($algorithm);
94
95 156
        if (!in_array($algorithm, hash_algos(), true)) {
96 6
            throw new \Exception('PBKDF2 ERROR: Invalid hash algorithm');
97
        }
98
99 150
        if ($count <= 0 || $keyLength <= 0) {
100
            throw new \Exception('PBKDF2 ERROR: Invalid parameters.');
101
        }
102
103
        return new Buffer(\hash_pbkdf2($algorithm, $password->getBinary(), $salt->getBinary(), $count, $keyLength, true), $keyLength);
104
    }
105
106
    /**
107 78
     * @param BufferInterface $data
108
     * @param int $seed
109 78
     * @return BufferInterface
110
     */
111
    public static function murmur3(BufferInterface $data, $seed)
112
    {
113
        return new Buffer(pack('N', murmurhash3_int($data->getBinary(), (int)$seed)), 4);
114
    }
115
116
    /**
117
     * Do HMAC hashing on $data and $salt
118
     *
119
     * @param string $algo
120 66
     * @param BufferInterface $data
121
     * @param BufferInterface $salt
122 66
     * @return BufferInterface
123
     */
124
    public static function hmac($algo, BufferInterface $data, BufferInterface $salt)
125
    {
126
        return new Buffer(hash_hmac($algo, $data->getBinary(), $salt->getBinary(), true));
127
    }
128
}
129