Skip to content

Commit 4384a97

Browse files
author
栄茉
authored
Merge pull request #10 from MayMeow/allow-exporting-rsaparameters
Implement ability to export and import RSA Parameters
2 parents 75a28d6 + 1664309 commit 4384a97

18 files changed

Lines changed: 342 additions & 3 deletions

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Changelog
2+
3+
## [Unreleased](https://github.com/maymeow/php-cryptography/tree/HEAD)
4+
5+
[Full Changelog](https://github.com/maymeow/php-cryptography/compare/c0f4aded5b4196e7686f32ee84f6fd8651a753df...HEAD)
6+
7+
**Implemented enhancements:**
8+
9+
- Upgrade for PHP 8 [\#8](https://github.com/MayMeow/php-cryptography/issues/8)
10+
11+
**Fixed bugs:**
12+
13+
- RSAParameters keys cannot be string [\#2](https://github.com/MayMeow/php-cryptography/issues/2)
14+
15+
**Merged pull requests:**
16+
17+
- Update to using php 8 [\#9](https://github.com/MayMeow/php-cryptography/pull/9) ([MayMeow](https://github.com/MayMeow))
18+
- :wrench: add phpstan [\#6](https://github.com/MayMeow/php-cryptography/pull/6) ([MayMeow](https://github.com/MayMeow))
19+
- Update rsacsp [\#5](https://github.com/MayMeow/php-cryptography/pull/5) ([MayMeow](https://github.com/MayMeow))
20+
- Remove string type from keys in RSAParameters [\#3](https://github.com/MayMeow/php-cryptography/pull/3) ([MayMeow](https://github.com/MayMeow))
21+
- ADD RSA parameters WIP [\#1](https://github.com/MayMeow/php-cryptography/pull/1) ([MayMeow](https://github.com/MayMeow))
22+
23+
24+
25+
\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,45 @@ $rsa->setParameters($parameters);
7979
$encryptedTest = $rsa->encrypt($plainText);
8080

8181
$decryptedText = $rsa->decrypt($encryptedTest);
82+
```
83+
84+
### Exporting and importing keys
85+
86+
To use keys for later in case of encrypt/decrypt data is important to store them on some place. For this I created Readers
87+
and Writers. To export keys use Writer as example shows bellow:
88+
89+
```php
90+
$parameters = new RSAParameters();
91+
$parameters->generateKeys();
92+
$locator = new TestingParametersLocator();
8293

94+
$writer = new RsaParametersWriter($locator);
95+
$writer->write($parameters);
8396
```
97+
If you want implement own Writers they must implement `MayMeow\Cryptography\Tools\RsaParametersWriterInterface`.
98+
99+
Importing keys can be done as on example below:
100+
101+
```php
102+
$reader = new RsaParametersReader($locator);
103+
$parameters2 = $reader->read();
104+
105+
$csp2 = new RSACryptoServiceProvider();
106+
$csp2->setParameters($parameters2);
107+
```
108+
109+
Like on writers you can implement your own Readers too. If you do so your new reader have to implement
110+
`MayMeow\Cryptography\Tools\RsaParametersReaderInterface`
111+
112+
### Locators
113+
114+
Both reader and writer in above example is using Locator. Locators are classes which can return string representation
115+
of location where are stored RSAParameters parts. This can be database table, model, table field, path in filesystem
116+
and more. Interfaces for Reader and Writer not required to use one, but I recommend it.
117+
118+
If you want implement your own locator, this has to implement `MayMeow\Cryptography\Tools\RSAParametersLocatorInterface`.
119+
120+
As example, you can check Tools in test folder.
84121

85122
### Cryptographic key derivation
86123

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"scripts": {
3030
"test": "phpunit tests",
3131
"cs-check": "phpcs --standard=PSR2 src",
32+
"cs-fix": "phpcbf --standard=PSR2 src",
3233
"stan": "phpstan analyse"
3334
}
3435
}

generate-changelog.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
docker run -it --rm -v "$(pwd)":/usr/local/src/your-app githubchangeloggenerator/github-changelog-generator -u maymeow -p php-cryptography

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="tests/bootstrap.php" />

src/AESCryptoServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function setKey(string $key): AESCryptoServiceProvider
5858
/**
5959
* Generate key
6060
*
61+
* @todo Change return type to string only, throw exception instead
6162
* @return bool|string
6263
*/
6364
public function generateKey()
@@ -76,6 +77,7 @@ public function generateKey()
7677
/**
7778
* Generate IV
7879
*
80+
* @todo Change return type to string only, throw exception instead
7981
* @return bool|string
8082
*/
8183
public function generateIV()

src/CryptoKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function helloWorld() : string
1111
}
1212

1313
/**
14-
* Derivate cryptographic key from given password
14+
* Derive cryptographic key from given password
1515
*
1616
* @param string $password
1717
* @param string $salt
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace MayMeow\Cryptography\Exceptions;
4+
5+
use Throwable;
6+
7+
class FileReadException extends \Exception
8+
{
9+
/**
10+
* @param string $message
11+
* @param int $code
12+
* @param Throwable|null $previous
13+
*/
14+
public function __construct(string $message = "Cannot read file", int $code = 10, Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}

src/RSACryptoServiceProvider.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MayMeow\Cryptography;
44

55
use MayMeow\Cryptography\Exceptions\NotImplementedException;
6+
use MayMeow\Cryptography\Tools\RsaParametersReaderInterface;
67

78
class RSACryptoServiceProvider
89
{
@@ -42,6 +43,8 @@ public function decrypt(string $encryptedText) : string
4243
}
4344

4445
/**
46+
* Encrypt data with pricate key
47+
*
4548
* @param string $plainText
4649
* @return string
4750
*/
@@ -56,6 +59,8 @@ public function privateEncrypt(string $plainText) : string
5659
}
5760

5861
/**
62+
* Decrypt data with public key
63+
*
5964
* @param string $encryptedText
6065
* @return string
6166
*/
@@ -84,6 +89,8 @@ protected function open() : string
8489
}
8590

8691
/**
92+
* Sign data with key and return signature
93+
*
8794
* @param string $data
8895
* @return string
8996
*/
@@ -97,6 +104,8 @@ public function sign(string $data) : string
97104
}
98105

99106
/**
107+
* Verify if signed data are same as in time of create signature
108+
*
100109
* @param string $data
101110
* @param string $signature
102111
* @return bool
@@ -128,8 +137,13 @@ public function getFingerPrint(string $publicKey = null) : string
128137
}
129138

130139
/**
140+
* Returns private key
141+
*
131142
* @return \OpenSSLAsymmetricKey|string
132143
* @throws Exceptions\DecryptPrivateKeyException
144+
*
145+
* @deprecated Passphrase can be set with setting private key instead
146+
* @see RsaParameters::setPrivateKey()
133147
*/
134148
private function getPrivateKey() : \OpenSSLAsymmetricKey|string
135149
{

src/RSAParameters.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public function __construct()
2121
}
2222

2323
/**
24+
* Generate keypair and passphrase to decrypt private key
25+
*
2426
* @param string|null $passphrase
2527
* @param array|null $configArgs
2628
* @return $this
@@ -71,14 +73,20 @@ public function getPrivateKey() : \OpenSSLAsymmetricKey|string
7173
}
7274

7375
/**
76+
* Set private key from string representation and its passphrase
77+
*
7478
* @param string $privateKey
79+
* @param string $passphrase
7580
*/
76-
public function setPrivateKey(string $privateKey): void
81+
public function setPrivateKey(string $privateKey, string $passphrase): void
7782
{
83+
$this->passphrase = $passphrase;
7884
$this->privateKey = $privateKey;
7985
}
8086

8187
/**
88+
* Returns public key as string
89+
*
8290
* @return string
8391
*/
8492
public function getPublicKey() : string
@@ -87,6 +95,8 @@ public function getPublicKey() : string
8795
}
8896

8997
/**
98+
* Set public key from string representation
99+
*
90100
* @param string $publicKey
91101
*/
92102
public function setPublicKey(string $publicKey): void
@@ -95,6 +105,8 @@ public function setPublicKey(string $publicKey): void
95105
}
96106

97107
/**
108+
* Returns passphrase for private key decryption
109+
*
98110
* @return string
99111
*/
100112
public function getPassphrase(): string
@@ -103,6 +115,8 @@ public function getPassphrase(): string
103115
}
104116

105117
/**
118+
* Set passphrase for private key
119+
*
106120
* @param string $passphrase
107121
* @return $this
108122
*/

0 commit comments

Comments
 (0)