Completed
Push — master ( 77672b...fabe4e )
by Vitalii
17:58 queued 16:34
created

AbstractDevice::hasEvenLength()   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
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Device;
12
13
use TonicForHealth\DUKPT\DUKPTFactory;
14
use TonicForHealth\DUKPT\Helper\Encryption\EncryptionHelperInterface;
15
use TonicForHealth\DUKPT\Key\BaseDerivationKeyFactory;
16
use TonicForHealth\DUKPT\Key\DerivedKey;
17
use TonicForHealth\DUKPT\Key\DerivedKeyFactory;
18
use TonicForHealth\DUKPT\Key\InitialPinEncryptionKeyFactory;
19
use TonicForHealth\DUKPT\Key\KeySerialNumberFactory;
20
21
/**
22
 * Class AbstractDevice
23
 *
24
 * @author Vitalii Ekert <[email protected]>
25
 */
26
abstract class AbstractDevice implements DeviceInterface
27
{
28
    /**
29
     * @var BaseDerivationKeyFactory
30
     */
31
    protected $bdkFactory;
32
33
    /**
34
     * @var KeySerialNumberFactory
35
     */
36
    protected $ksnFactory;
37
38
    /**
39
     * @var InitialPinEncryptionKeyFactory
40
     */
41
    protected $ipekFactory;
42
43
    /**
44
     * @var DerivedKeyFactory
45
     */
46
    protected $derivedKeyFactory;
47
48
    /**
49
     * @var EncryptionHelperInterface
50
     */
51
    protected $encryptionHelper;
52
53
    /**
54
     * @var string
55
     */
56
    protected $sessionKey;
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public function __construct(DUKPTFactory $factory)
62
    {
63 1
        $this->bdkFactory = $factory->createBDKFactory();
64 1
        $this->ksnFactory = $factory->createKSNFactory();
65 1
        $this->ipekFactory = $factory->createIPEKFactory();
66 1
        $this->derivedKeyFactory = $factory->createDerivedKeyFactory();
67
68 1
        $this->encryptionHelper = $this->createEncryptionHelper();
69 1
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 1
    public function load($hexBDK, $hexKSN)
75
    {
76 1
        $bdk = $this->bdkFactory->createFromHexadecimal($hexBDK);
77 1
        $ksn = $this->ksnFactory->createFromHexadecimal($hexKSN);
78 1
        $ipek = $this->ipekFactory->create($bdk, $ksn);
79
80 1
        $derivedKey = $this->derivedKeyFactory->create($ksn, $ipek);
81 1
        $this->sessionKey = $this->createSessionKey($derivedKey);
82
83 1
        return $this;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function getSessionKey()
90
    {
91 1
        return $this->sessionKey;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 2
    public function decrypt($cipherText)
98
    {
99 2
        if (!$this->hasEvenLength($cipherText)) {
100 1
            throw new \InvalidArgumentException('Hexadecimal input string must have an even length.');
101
        }
102
103 1
        return $this->encryptionHelper->decrypt($this->sessionKey, hex2bin($cipherText));
104
    }
105
106
    /**
107
     * @return EncryptionHelperInterface
108
     */
109
    abstract protected function createEncryptionHelper();
110
111
    /**
112
     * @param DerivedKey $derivedKey
113
     *
114
     * @return string
115
     */
116
    abstract protected function createSessionKey(DerivedKey $derivedKey);
117
118
    /**
119
     * Returns TRUE if the string has an even length.
120
     *
121
     * @param string $string
122
     *
123
     * @return bool
124
     */
125 2
    private function hasEvenLength($string)
126
    {
127 2
        return 0 === strlen($string) %2;
128
    }
129
}
130