DerivedKey   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 39
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPinEncryptionKey() 0 4 1
A getPinEncryptionKeyHexadecimal() 0 4 1
A validateKey() 0 10 2
1
<?php
2
/*
3
 * This file is part of the DUKPT package.
4
 *
5
 * Copyright (c) 2016-2017 Tonic Health <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace TonicForHealth\DUKPT\Key;
12
13
/**
14
 * Class DerivedKey
15
 *
16
 * @author Vitalii Ekert <[email protected]>
17
 */
18
class DerivedKey extends AbstractKey
19
{
20
    const KEY_SIZE_BYTES = 16;
21
    const KEY_PIN_ENCRYPTION_MASK = '00000000000000FF00000000000000FF';
22
23
    /**
24
     * Returns the PIN Encryption Key as a binary data string
25
     *
26
     * @return string
27
     */
28 7
    public function getPinEncryptionKey()
29
    {
30 7
        return $this->toBinary() ^ hex2bin(self::KEY_PIN_ENCRYPTION_MASK);
31
    }
32
33
    /**
34
     * Returns the hexadecimal representation of the PIN Encryption Key
35
     *
36
     * @return string
37
     */
38 6
    public function getPinEncryptionKeyHexadecimal()
39
    {
40 6
        return strtoupper(bin2hex($this->getPinEncryptionKey()));
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 14
    protected function validateKey($binKey)
47
    {
48 14
        if (self::KEY_SIZE_BYTES !== ($keySize = strlen($binKey))) {
49 2
            throw new \InvalidArgumentException(
50 2
                sprintf('Invalid Derived Key size (expected %d bytes, got %d).', self::KEY_SIZE_BYTES, $keySize)
51
            );
52
        }
53
54 12
        return $binKey;
55
    }
56
}
57