|
@@ 185-203 (lines=19) @@
|
| 182 |
|
* |
| 183 |
|
* @return string |
| 184 |
|
*/ |
| 185 |
|
function private_encrypt($data, $key, $passphrase = null, $padding = OPENSSL_PKCS1_PADDING) |
| 186 |
|
{ |
| 187 |
|
$privKey = openssl_get_privatekey($key, $passphrase); |
| 188 |
|
|
| 189 |
|
if (!is_resource($privKey)) { |
| 190 |
|
$type = gettype($privKey); |
| 191 |
|
throw new \Exception("Expected private key to be resource, got: '$type'. Check if the given private key is in correct syntax."); |
| 192 |
|
} |
| 193 |
|
|
| 194 |
|
openssl_private_encrypt($data, $result, $privKey, $padding); |
| 195 |
|
|
| 196 |
|
if (!is_string($result)) { |
| 197 |
|
throw new \Exception("Failed to encrypt data. Result: '$result'"); |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
$base64 = base64_encode($result); |
| 201 |
|
|
| 202 |
|
return $base64; |
| 203 |
|
} |
| 204 |
|
|
| 205 |
|
/** |
| 206 |
|
* Decrypts data with public key, that was encrypted with private key |
|
@@ 268-286 (lines=19) @@
|
| 265 |
|
* |
| 266 |
|
* @return string |
| 267 |
|
*/ |
| 268 |
|
function generate_signature($data, $key, $passphrase = null, $signature_alg = OPENSSL_ALGO_SHA1) |
| 269 |
|
{ |
| 270 |
|
$privKey = openssl_get_privatekey($key, $passphrase); |
| 271 |
|
|
| 272 |
|
if (!is_resource($privKey)) { |
| 273 |
|
$type = gettype($privKey); |
| 274 |
|
throw new \Exception("Expected private key to be resource, got: '$type'. Check if the given private key is in correct syntax."); |
| 275 |
|
} |
| 276 |
|
|
| 277 |
|
openssl_sign($data, $result, $privKey, $signature_alg); |
| 278 |
|
|
| 279 |
|
if (!is_string($result)) { |
| 280 |
|
throw new \Exception("Failed to encrypt data. Result: '$result'"); |
| 281 |
|
} |
| 282 |
|
|
| 283 |
|
$base64 = base64_encode($result); |
| 284 |
|
|
| 285 |
|
return $base64; |
| 286 |
|
} |
| 287 |
|
|
| 288 |
|
/** |
| 289 |
|
* Verify signature that was encrypted with private key through public key |