Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TatraBankaMailDecryptor | |
0.00% |
0 / 15 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| decrypt | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace Tomaj\BankMailsParser\Parser\TatraBanka; |
| 5 | |
| 6 | class TatraBankaMailDecryptor |
| 7 | { |
| 8 | private string $privateKeyPath; |
| 9 | |
| 10 | private string $passphrase; |
| 11 | |
| 12 | public function __construct( |
| 13 | string $privateKeyPath, |
| 14 | string $passphrase |
| 15 | ) { |
| 16 | $this->privateKeyPath = $privateKeyPath; |
| 17 | $this->passphrase = $passphrase; |
| 18 | } |
| 19 | |
| 20 | public function decrypt(string $contents): ?string |
| 21 | { |
| 22 | if (!$this->privateKeyPath || !file_exists($this->privateKeyPath)) { |
| 23 | throw new \Exception('missing path to TatraBanka PGP private key in config'); |
| 24 | } |
| 25 | |
| 26 | $privateKey = \OpenPGP_Message::parse(file_get_contents($this->privateKeyPath)); |
| 27 | foreach ($privateKey as $p) { |
| 28 | if (!($p instanceof \OpenPGP_SecretKeyPacket || $p instanceof \OpenPGP_SecretSubkeyPacket)) { |
| 29 | continue; |
| 30 | } |
| 31 | |
| 32 | $privateKey = \OpenPGP_Crypt_Symmetric::decryptSecretKey($this->passphrase, $p); |
| 33 | } |
| 34 | |
| 35 | $msg = \OpenPGP_Message::parse(\OpenPGP::unarmor($contents, 'PGP MESSAGE')); |
| 36 | |
| 37 | $decryptor = new \OpenPGP_Crypt_RSA($privateKey); |
| 38 | $decrypted = $decryptor->decrypt($msg); |
| 39 | |
| 40 | if ($decrypted) { |
| 41 | return $decrypted->packets[0]->data; |
| 42 | } |
| 43 | |
| 44 | return null; |
| 45 | } |
| 46 | } |