1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File AesEncryptionStrategy.php |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Tebru\AesEncryption\Strategy; |
7
|
|
|
|
8
|
|
|
use Tebru; |
9
|
|
|
use Tebru\AesEncryption\Enum\AesEnum; |
10
|
|
|
use Tebru\AesEncryption\Exception\InvalidNumberOfEncryptionPieces; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class AesEncryptionStrategy |
14
|
|
|
* |
15
|
|
|
* @author Nate Brunette <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class AesEncryptionStrategy |
18
|
|
|
{ |
19
|
|
|
/**#@+ |
20
|
|
|
* Encryption constants |
21
|
|
|
*/ |
22
|
|
|
const ENCRYPTION_CIPHER = MCRYPT_RIJNDAEL_128; |
23
|
|
|
const ENCRYPTION_MODE = MCRYPT_MODE_CBC; |
24
|
|
|
/**#@-*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A secret key |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $key; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $method; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param string $key The secret key |
42
|
|
|
* @param string $method |
43
|
|
|
*/ |
44
|
|
|
public function __construct($key, $method = AesEnum::METHOD_256) |
45
|
|
|
{ |
46
|
|
|
$this->key = $key; |
47
|
|
|
$this->method = $method; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create an initialization vector |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
abstract public function createIv(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the size of the IV |
59
|
|
|
* |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
abstract public function getIvSize(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Encrypt data |
66
|
|
|
* |
67
|
|
|
* @param mixed $data |
68
|
|
|
* @param string $iv |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
abstract public function encryptData($data, $iv); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Decrypt data |
75
|
|
|
* |
76
|
|
|
* @param $data |
77
|
|
|
* @param $iv |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
abstract public function decryptData($data, $iv); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Encode data/iv using base64 and pipe-delimited |
84
|
|
|
* |
85
|
|
|
* @param mixed $encryptedData |
86
|
|
|
* @param string $mac |
87
|
|
|
* @param string $iv |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function encodeData($encryptedData, $mac, $iv) |
91
|
|
|
{ |
92
|
|
|
return base64_encode($encryptedData) . '|' . base64_encode($mac) . '|' . base64_encode($iv); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Decodes data/iv and returns array |
97
|
|
|
* |
98
|
|
|
* @param string $data |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function decodeData($data) |
102
|
|
|
{ |
103
|
|
|
$decoded = explode('|', $data); |
104
|
|
|
|
105
|
|
|
Tebru\assert(3 === sizeof($decoded), new InvalidNumberOfEncryptionPieces('Encrypted string has been modified, wrong number of pieces found')); |
106
|
|
|
|
107
|
|
|
return [base64_decode($decoded[0]), base64_decode($decoded[1]), base64_decode($decoded[2])]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get hmac hash of data |
113
|
|
|
* |
114
|
|
|
* @param $data |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getMac($data) |
118
|
|
|
{ |
119
|
|
|
return hash_hmac('sha256', $data, $this->getKey()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the key as a packed binary string |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function getKey() |
128
|
|
|
{ |
129
|
|
|
// first create a sha256 hash of the key |
130
|
|
|
$hash = hash('sha256', $this->key); |
131
|
|
|
|
132
|
|
|
// create a binary string from the hash |
133
|
|
|
$binary = hex2bin($hash); |
134
|
|
|
|
135
|
|
|
// limit the key size based on our encryption method |
136
|
|
|
$keySize = AesEnum::getKeySize($this->method); |
137
|
|
|
$key = substr($binary, 0, $keySize); |
138
|
|
|
|
139
|
|
|
return $key; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get the encryption method |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
protected function getMethod() |
148
|
|
|
{ |
149
|
|
|
return $this->method; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|