Completed
Pull Request — master (#285)
by thomas
21:56
created

Bip39SeedGenerator::normalize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 3
cts 6
cp 0.5
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 4.125
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 144
    private function normalize($string)
17
    {
18 144
        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 144
        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 144
    public function getSeed($mnemonic, $passphrase = '')
36
    {
37 144
        return Hash::pbkdf2(
38 144
            'sha512',
39 144
            $this->normalize($mnemonic),
40 144
            $this->normalize("mnemonic" . $passphrase),
41 144
            2048,
42
            64
43 144
        );
44
    }
45
}
46