Completed
Pull Request — master (#306)
by thomas
32:45 queued 29:33
created

Bip39SeedGenerator::getSeed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Mnemonic\Bip39;
4
5
use BitWasp\Bitcoin\Crypto\Hash;
6
use BitWasp\Buffertools\Buffer;
7
use BitWasp\Buffertools\BufferInterface;
8
9
class Bip39SeedGenerator
10
{
11
    /**
12
     * @param string $string
13
     * @return BufferInterface
14
     * @throws \Exception
15
     */
16 96
    private function normalize($string)
17
    {
18 96
        if (!class_exists('Normalizer')) {
19
            if (mb_detect_encoding($string) === 'UTF-8') {
20
                throw new \Exception('UTF-8 passphrase is not supported without the PECL intl extension installed.');
21
            } else {
22
                return new Buffer($string);
23
            }
24
        }
25
26 96
        return new Buffer(\Normalizer::normalize($string, \Normalizer::FORM_KD));
27
    }
28
29
    /**
30
     * @param string $mnemonic
31
     * @param string $passphrase
32
     * @return \BitWasp\Buffertools\BufferInterface
33
     * @throws \Exception
34
     */
35 96
    public function getSeed($mnemonic, $passphrase = '')
36
    {
37 96
        return Hash::pbkdf2(
38 96
            'sha512',
39 96
            $this->normalize($mnemonic),
40 96
            $this->normalize("mnemonic" . $passphrase),
41 96
            2048,
42 24
            64
43 72
        );
44
    }
45
}
46