1
|
|
|
<?php namespace Limoncello\Crypt; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Generator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @package Limoncello\Crypt |
23
|
|
|
*/ |
24
|
|
|
abstract class BaseAsymmetricCrypt extends BaseCrypt |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var resource|null |
28
|
|
|
*/ |
29
|
|
|
private $key = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int|null |
33
|
|
|
*/ |
34
|
|
|
private $keyBytes = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Destructor. |
38
|
|
|
*/ |
39
|
3 |
|
public function __destruct() |
40
|
|
|
{ |
41
|
3 |
|
$this->closeKey(); |
42
|
3 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
3 |
|
public function closeKey() |
48
|
|
|
{ |
49
|
3 |
|
if ($this->key !== null) { |
50
|
3 |
|
openssl_pkey_free($this->key); |
51
|
3 |
|
$this->key = null; |
52
|
3 |
|
$this->keyBytes = null; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return resource|null |
60
|
|
|
*/ |
61
|
3 |
|
protected function getKey() |
62
|
|
|
{ |
63
|
3 |
|
return $this->key; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param resource $key |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
3 |
|
protected function setKey($key) |
72
|
|
|
{ |
73
|
3 |
|
assert(is_resource($key) === true); |
74
|
|
|
|
75
|
3 |
|
$this->closeKey(); |
76
|
3 |
|
$this->key = $key; |
77
|
|
|
|
78
|
3 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return int|null |
|
|
|
|
83
|
|
|
*/ |
84
|
3 |
|
protected function getKeyBytes() |
85
|
|
|
{ |
86
|
3 |
|
if ($this->keyBytes === null && $this->getKey() !== null) { |
87
|
3 |
|
$this->clearErrors(); |
88
|
3 |
|
$details = openssl_pkey_get_details($this->getKey()); |
89
|
3 |
|
$details !== false ?: $this->throwException(new CryptException($this->getErrorMessage())); |
90
|
3 |
|
$this->keyBytes = $details['bits'] / 8; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
return $this->keyBytes; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return int|null |
|
|
|
|
98
|
|
|
*/ |
99
|
3 |
View Code Duplication |
protected function getEncryptChunkSize() |
|
|
|
|
100
|
|
|
{ |
101
|
3 |
|
$keyBytes = $this->getKeyBytes(); |
102
|
|
|
|
103
|
|
|
// 11 is a kind of magic number related to padding. |
104
|
3 |
|
$result = $keyBytes === null ? null : $keyBytes - 11; |
105
|
|
|
|
106
|
3 |
|
return $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return int|null |
|
|
|
|
111
|
|
|
*/ |
112
|
3 |
View Code Duplication |
protected function getDecryptChunkSize() |
|
|
|
|
113
|
|
|
{ |
114
|
3 |
|
$keyBytes = $this->getKeyBytes(); |
115
|
3 |
|
$result = $keyBytes === null ? null : $keyBytes; |
116
|
|
|
|
117
|
3 |
|
return $result; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $value |
122
|
|
|
* @param int $maxSize |
123
|
|
|
* |
124
|
|
|
* @return Generator |
125
|
|
|
*/ |
126
|
3 |
|
protected function chunkString($value, $maxSize) |
127
|
|
|
{ |
128
|
3 |
|
$isValidInput = is_string($value) === true && is_int($maxSize) === true && $maxSize > 0; |
129
|
|
|
|
130
|
3 |
|
assert($isValidInput === true); |
131
|
|
|
|
132
|
3 |
|
if ($isValidInput === true) { |
133
|
3 |
|
$start = 0; |
134
|
3 |
|
$length = strlen($value); |
135
|
3 |
|
if ($length === 0) { |
136
|
1 |
|
yield $value; |
137
|
|
|
} |
138
|
3 |
|
while ($start < $length) { |
139
|
3 |
|
yield substr($value, $start, $maxSize); |
140
|
3 |
|
$start += $maxSize; |
141
|
|
|
} |
142
|
|
|
} |
143
|
3 |
|
} |
144
|
|
|
} |
145
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.