Completed
Push — master ( b2abd4...891c12 )
by thomas
22:03
created

Bip39SeedGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 78.56%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 37
ccs 11
cts 14
cp 0.7856
rs 10
c 3
b 0
f 0
wmc 4
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 12 3
A getSeed() 0 10 1
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