├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Exceptions │ ├── DecryptionException.php │ ├── EncryptionException.php │ ├── HashingException.php │ ├── InvalidKeyException.php │ └── MethodNotSupportedException.php ├── Hash.php ├── PrivateRsa.php ├── PublicRsa.php └── Symmetric.php └── tests ├── HashTest.php ├── RsaTest.php ├── SymmetricTest.php └── resources ├── test_private_key.pem └── test_public_key.pem /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | /composer.lock 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | language: php 4 | 5 | php: 6 | - 7.1 7 | - 7.2 8 | - 7.3 9 | - 7.4 10 | 11 | branches: 12 | only: 13 | - master 14 | 15 | before_script: 16 | - composer install --no-interaction --no-suggest 17 | - composer require php-coveralls/php-coveralls 18 | 19 | script: 20 | - mkdir -p build/logs 21 | - vendor/bin/phpunit --coverage-clover build/logs/clover.xml tests 22 | 23 | after_success: 24 | - travis_retry php vendor/bin/php-coveralls -v 25 | 26 | notifications: 27 | email: 28 | recipients: 29 | - info@miladrahimi.com 30 | on_success: change 31 | on_failure: always 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Milad Rahimi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/miladrahimi/phpcrypt/v/stable)](https://packagist.org/packages/miladrahimi/phpcrypt) 2 | [![Total Downloads](https://poser.pugx.org/miladrahimi/phpcrypt/downloads)](https://packagist.org/packages/miladrahimi/phpcrypt) 3 | [![Build Status](https://travis-ci.org/miladrahimi/phpcrypt.svg?branch=master)](https://travis-ci.org/miladrahimi/phpcrypt) 4 | [![Coverage Status](https://coveralls.io/repos/github/miladrahimi/phpcrypt/badge.svg?branch=master)](https://coveralls.io/github/miladrahimi/phpcrypt?branch=master) 5 | [![License](https://poser.pugx.org/miladrahimi/phpcrypt/license)](https://packagist.org/packages/miladrahimi/phpcrypt) 6 | 7 | # PhpCrypt 8 | 9 | PhpCrypt is a package for encryption, decryption, and hashing data in PHP projects. 10 | It provides an easy-to-use and fluent interface. 11 | 12 | Features: 13 | * Symmetric encryption/decryption using AES and other symmetric methods. 14 | * Asymmetric encryption/decryption using the RSA method. 15 | * Hashing and verifying data (e.g. passwords) using the BCrypt method. 16 | 17 | ## Versions 18 | 19 | * v5.x.x 20 | * v4.x.x 21 | * v3.x.x (Unsupported) 22 | * v2.x.x (Unsupported) 23 | * v1.x.x (Unsupported) 24 | 25 | ## Installation 26 | 27 | Install [Composer](https://getcomposer.org) and run the following command in your project's root directory: 28 | 29 | ```bash 30 | composer require miladrahimi/phpcrypt "5.*" 31 | ``` 32 | 33 | ## Symmetric Encryption 34 | 35 | This example shows how to encrypt and decrypt data using symmetric algorithms like AES. 36 | 37 | ```php 38 | use MiladRahimi\PhpCrypt\Symmetric; 39 | 40 | $symmetric = new Symmetric(); 41 | $encryptedData = $symmetric->encrypt('secret'); 42 | echo $symmetric->decrypt($encryptedData); // secret 43 | ``` 44 | 45 | It generates a random key and uses `aes-256-cbc` method for encrypting/decrypting data. 46 | 47 | ### Custom Key 48 | 49 | If you have already a key, you can use your own key like this: 50 | 51 | ```php 52 | use MiladRahimi\PhpCrypt\Symmetric; 53 | 54 | $key = '1234567890123456'; 55 | 56 | // Set the key using the constructor 57 | $symmetric = new Symmetric($key); 58 | 59 | // Or set the key using the setter 60 | $symmetric = new Symmetric(); 61 | $symmetric->setKey($key); 62 | 63 | // And get the key using the getter 64 | $myKey = $symmetric->getKey(); 65 | ``` 66 | 67 | The method `generateKey` can help you to generate a new random key. 68 | See the snippet below. 69 | 70 | ```php 71 | use MiladRahimi\PhpCrypt\Symmetric; 72 | 73 | $key = Symmetric::generateKey(); 74 | ``` 75 | 76 | ### Custom Methods 77 | 78 | In default, The `Symmetric` class uses `aes-256-cbc` method to encrypt/decrypt data. 79 | You can use your preferred method as well. 80 | See the following example. 81 | 82 | ```php 83 | use MiladRahimi\PhpCrypt\Exceptions\MethodNotSupportedException; 84 | use MiladRahimi\PhpCrypt\Symmetric; 85 | 86 | try { 87 | $symmetric = new Symmetric(); 88 | $symmetric->setMethod('aria-256-ctr'); 89 | // ... 90 | } catch (MethodNotSupportedException $e) { 91 | // The method is not supported. 92 | } 93 | ``` 94 | 95 | ### Supported Methods 96 | 97 | If you want to know which methods do your installed OpenSSL extension support, see the snippet below: 98 | 99 | ```php 100 | use MiladRahimi\PhpCrypt\Symmetric; 101 | 102 | print_r(Symmetric::supportedMethods()); 103 | ``` 104 | 105 | ## RSA Encryption 106 | 107 | RSA is a popular asymmetric encryption/decryption algorithm. 108 | The examples below illustrate how to encrypt/decrypt data using the RSA algorithm. 109 | 110 | ### Encryption with private key 111 | 112 | In this example, we encrypt data with a private key and decrypt it with the related public key. 113 | 114 | ```php 115 | use MiladRahimi\PhpCrypt\PrivateRsa; 116 | use MiladRahimi\PhpCrypt\PublicRsa; 117 | 118 | $privateRsa = new PrivateRsa('private_key.pem'); 119 | $publicRsa = new PublicRsa('public_key.pem'); 120 | 121 | $result = $privateRsa->encrypt('secret'); 122 | echo $publicRsa->decrypt($result); // secret 123 | ``` 124 | 125 | ### Encryption with public key 126 | 127 | In this example, we encrypt data with a public key and decrypt it with the related private key. 128 | 129 | ```php 130 | use MiladRahimi\PhpCrypt\PrivateRsa; 131 | use MiladRahimi\PhpCrypt\PublicRsa; 132 | 133 | $privateRsa = new PrivateRsa('private_key.pem'); 134 | $publicRsa = new PublicRsa('public_key.pem'); 135 | 136 | $result = $publicRsa->encrypt('secret'); 137 | echo $privateRsa->decrypt($result); // secret 138 | ``` 139 | 140 | ### Base64 Encoding 141 | 142 | In default, the encrypted data returned by `PrivateRsa::encrypt()` and `PublicRsa::encrypt()` methods will be Base64 encoded. 143 | You can disable this encoding like the example below. 144 | 145 | ```php 146 | use MiladRahimi\PhpCrypt\PrivateRsa; 147 | use MiladRahimi\PhpCrypt\PublicRsa; 148 | 149 | $privateRsa = new PrivateRsa('private_key.pem'); 150 | $publicRsa = new PublicRsa('public_key.pem'); 151 | 152 | // Disable Base64 encoding for public encryption 153 | $result = $publicRsa->encrypt('secret', false); 154 | 155 | // Disable Base64 encoding for private encryption 156 | $result = $privateRsa->encrypt('secret', false); 157 | ``` 158 | 159 | ## Hashing 160 | 161 | This example shows how to hash data and verify it. 162 | 163 | ```php 164 | use MiladRahimi\PhpCrypt\Hash; 165 | 166 | $hash = new Hash(); 167 | 168 | $hashedPassword = $hash->make('MyPassword'); 169 | echo $hash->verify('MyPassword', $hashedPassword); // true 170 | echo $hash->verify('AnotherPassword', $hashedPassword); // false 171 | ``` 172 | 173 | ## Error Handling 174 | 175 | The `Symmetric`, `PrivateRsa`, `PublicRsa`, and `Hash` classes may throw these exceptions: 176 | 177 | * `EncryptionException`: When it cannot encrypt data. 178 | * `DecryptionException`: When it cannot decrypt data. 179 | * `HashingException`: When it cannot hash data. 180 | * `MethodNotSupportedException`: When the passed encryption method to the `Symmetric` class is not supported. 181 | * `InvalidKeyException`: When the passed key to `PrivateRsa` or `PublicRsa` classes is not valid. 182 | 183 | ## License 184 | 185 | PhpCrypt is initially created by [Milad Rahimi](https://miladrahimi.com) and released under the [MIT License](http://opensource.org/licenses/mit-license.php). 186 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "miladrahimi/phpcrypt", 3 | "description": "Encryption, decryption, and hashing tools for PHP projects", 4 | "keywords": [ 5 | "Crypt", 6 | "Cryptography", 7 | "Encrypt", 8 | "Decrypt", 9 | "Hash", 10 | "Password", 11 | "Symmetric", 12 | "Asymmetric", 13 | "OpenSSL", 14 | "RSA", 15 | "AES" 16 | ], 17 | "homepage": "https://github.com/miladrahimi/phpcrypt", 18 | "type": "library", 19 | "license": "MIT", 20 | "authors": [ 21 | { 22 | "name": "Milad Rahimi", 23 | "email": "info@miladrahimi.com", 24 | "homepage": "http://miladrahimi.com", 25 | "role": "Developer" 26 | } 27 | ], 28 | "support": { 29 | "email": "info@miladrahimi.com", 30 | "source": "https://github.com/miladrahimi/phpcrypt/issues" 31 | }, 32 | "require": { 33 | "php": ">=7.1", 34 | "ext-openssl": "*" 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "MiladRahimi\\PhpCrypt\\": "src/", 39 | "MiladRahimi\\PhpCrypt\\Tests\\": "tests/" 40 | } 41 | }, 42 | "require-dev": { 43 | "phpunit/phpunit": "7.*" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | ./tests 8 | 9 | 10 | 11 | 12 | ./src 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Exceptions/DecryptionException.php: -------------------------------------------------------------------------------- 1 | setKey($key); 31 | } 32 | 33 | /** 34 | * Encrypt the given plain data 35 | * It uses a private key to encrypt the plain data. 36 | * The encrypted data will be decrypted only with the related public key. 37 | * 38 | * @param string $plain 39 | * @param bool $base64 40 | * @return string 41 | * @throws EncryptionException 42 | */ 43 | public function encrypt(string $plain, bool $base64 = true): string 44 | { 45 | $encrypted = ''; 46 | if (openssl_private_encrypt($plain, $encrypted, $this->key) == false) { 47 | throw new EncryptionException(openssl_error_string()); 48 | } 49 | 50 | return $base64 ? base64_encode($encrypted) : $encrypted; 51 | } 52 | 53 | /** 54 | * Decrypt the given encrypted data 55 | * It uses the private key to decrypt the given encrypted data. 56 | * 57 | * @param string $data 58 | * @param bool $base64 59 | * @return string 60 | * @throws DecryptionException 61 | */ 62 | public function decrypt(string $data, bool $base64 = true): string 63 | { 64 | $data = $base64 ? base64_decode($data) : $data; 65 | 66 | $decrypted = ''; 67 | if (openssl_private_decrypt($data, $decrypted, $this->key) == false) { 68 | throw new DecryptionException(openssl_error_string()); 69 | } 70 | 71 | return $decrypted; 72 | } 73 | 74 | /** 75 | * @param string $key 76 | * @param string $passphrase 77 | * @throws InvalidKeyException 78 | */ 79 | public function setKey(string $key, string $passphrase = ''): void 80 | { 81 | if (file_exists($key)) { 82 | if (!is_readable($key) || !($key = file_get_contents($key))) { 83 | throw new InvalidKeyException('The key file is not readable.'); 84 | } 85 | } 86 | 87 | $this->key = openssl_pkey_get_private($key, $passphrase); 88 | if ($this->key === false) { 89 | throw new InvalidKeyException(openssl_error_string()); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/PublicRsa.php: -------------------------------------------------------------------------------- 1 | setKey($key); 31 | } 32 | 33 | /** 34 | * Encrypt the given plain data 35 | * It uses a public key to encrypt the plain data. 36 | * The encrypted data will be decrypted only with the related private key. 37 | * 38 | * @param string $plain 39 | * @param bool $base64 40 | * @return string 41 | * @throws EncryptionException 42 | */ 43 | public function encrypt(string $plain, bool $base64 = true): string 44 | { 45 | $encrypted = ''; 46 | if (openssl_public_encrypt($plain, $encrypted, $this->key) == false) { 47 | throw new EncryptionException(openssl_error_string()); 48 | } 49 | 50 | return $base64 ? base64_encode($encrypted) : $encrypted; 51 | } 52 | 53 | /** 54 | * Decrypt the given encrypted data 55 | * It uses the public key to decrypt the given encrypted data. 56 | * 57 | * @param string $data 58 | * @param bool $base64 59 | * @return string 60 | * @throws DecryptionException 61 | */ 62 | public function decrypt(string $data, bool $base64 = true): string 63 | { 64 | $data = $base64 ? base64_decode($data) : $data; 65 | 66 | $decrypted = ''; 67 | if (openssl_public_decrypt($data, $decrypted, $this->key) == false) { 68 | throw new DecryptionException(openssl_error_string()); 69 | } 70 | 71 | return $decrypted; 72 | } 73 | 74 | /** 75 | * @param string $key The key file path or content 76 | * @throws InvalidKeyException 77 | */ 78 | public function setKey(string $key): void 79 | { 80 | if (file_exists($key)) { 81 | if (!is_readable($key) || !($key = file_get_contents($key))) { 82 | throw new InvalidKeyException('The key file is not readable.'); 83 | } 84 | } 85 | 86 | $this->key = openssl_pkey_get_public($key); 87 | if ($this->key === false) { 88 | throw new InvalidKeyException(openssl_error_string()); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/Symmetric.php: -------------------------------------------------------------------------------- 1 | setKey($key ?: static::generateKey()); 36 | } 37 | 38 | /** 39 | * Encrypt the given plain data 40 | * 41 | * @param string $plain 42 | * @return string 43 | * @throws EncryptionException 44 | */ 45 | public function encrypt(string $plain): string 46 | { 47 | $iv = $this->generateIv(); 48 | 49 | $encrypted = openssl_encrypt($plain, $this->method, $this->key, 0, $iv); 50 | if ($encrypted === false) { 51 | throw new EncryptionException(openssl_error_string()); 52 | } 53 | 54 | return join('.', [base64_encode($iv), base64_encode($encrypted)]); 55 | } 56 | 57 | /** 58 | * Decrypt the given encrypted data 59 | * 60 | * @param string $data 61 | * @return string 62 | * @throws DecryptionException 63 | */ 64 | public function decrypt(string $data): string 65 | { 66 | $parts = explode('.', $data); 67 | if (count($parts) != 2) { 68 | throw new DecryptionException('Encrypted data is in invalid format.'); 69 | } 70 | 71 | $iv = base64_decode($parts[0]); 72 | $encryptedPayload = base64_decode($parts[1]); 73 | 74 | $plain = openssl_decrypt($encryptedPayload, $this->method, $this->key, 0, $iv); 75 | if ($plain === false) { 76 | throw new DecryptionException(openssl_error_string()); 77 | } 78 | 79 | return $plain; 80 | } 81 | 82 | /** 83 | * Generate a random key 84 | * 85 | * @param int $size 86 | * @return string 87 | */ 88 | public static function generateKey(int $size = 256): string 89 | { 90 | return openssl_random_pseudo_bytes($size / 8); 91 | } 92 | 93 | /** 94 | * Generate a random IV 95 | * 96 | * @return string 97 | */ 98 | private function generateIv(): string 99 | { 100 | return openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->method)); 101 | } 102 | 103 | /** 104 | * Return all the supported cipher methods 105 | * 106 | * @return array 107 | */ 108 | public static function supportedMethods(): array 109 | { 110 | return openssl_get_cipher_methods(true); 111 | } 112 | 113 | /** 114 | * @return string|null 115 | */ 116 | public function getKey(): ?string 117 | { 118 | return $this->key; 119 | } 120 | 121 | /** 122 | * @param string|null $key 123 | */ 124 | public function setKey(?string $key): void 125 | { 126 | $this->key = $key; 127 | } 128 | 129 | /** 130 | * @return string 131 | */ 132 | public function getMethod(): string 133 | { 134 | return $this->method; 135 | } 136 | 137 | /** 138 | * @param string $method 139 | * @throws MethodNotSupportedException 140 | */ 141 | public function setMethod(string $method): void 142 | { 143 | if (in_array($method, openssl_get_cipher_methods()) == false) { 144 | throw new MethodNotSupportedException("The $method method is not supported."); 145 | } 146 | 147 | $this->method = $method; 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /tests/HashTest.php: -------------------------------------------------------------------------------- 1 | make('secret'); 18 | $this->assertTrue($hash->verify('secret', $hashed)); 19 | } 20 | 21 | /** 22 | * @throws HashingException 23 | */ 24 | public function test_making_a_hash_from_a_random_string_and_verifying_it() 25 | { 26 | $plain = md5(mt_rand(1, 999999)); 27 | 28 | $hash = new Hash(); 29 | $hashed = $hash->make($plain); 30 | $this->assertTrue($hash->verify($plain, $hashed)); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /tests/RsaTest.php: -------------------------------------------------------------------------------- 1 | private = new PrivateRsa(__DIR__ . '/resources/test_private_key.pem'); 32 | $this->public = new PublicRsa(__DIR__ . '/resources/test_public_key.pem'); 33 | } 34 | 35 | /** 36 | * @throws DecryptionException 37 | * @throws EncryptionException 38 | */ 39 | public function test_encrypting_with_the_private_key() 40 | { 41 | $encrypted = $this->private->encrypt('secret'); 42 | $this->assertEquals('secret', $this->public->decrypt($encrypted)); 43 | } 44 | 45 | /** 46 | * @throws DecryptionException 47 | * @throws EncryptionException 48 | */ 49 | public function test_encrypting_with_the_private_key_and_base64_off() 50 | { 51 | $encrypted = $this->private->encrypt('secret', false); 52 | $this->assertEquals('secret', $this->public->decrypt($encrypted, false)); 53 | } 54 | 55 | /** 56 | * @throws DecryptionException 57 | * @throws EncryptionException 58 | */ 59 | public function test_encrypting_with_the_public_key() 60 | { 61 | $encrypted = $this->public->encrypt('secret'); 62 | $this->assertEquals('secret', $this->private->decrypt($encrypted)); 63 | } 64 | 65 | /** 66 | * @throws DecryptionException 67 | * @throws EncryptionException 68 | */ 69 | public function test_encrypting_with_the_public_key_and_base64_off() 70 | { 71 | $encrypted = $this->public->encrypt('secret', false); 72 | $this->assertEquals('secret', $this->private->decrypt($encrypted, false)); 73 | } 74 | 75 | /** 76 | * @throws DecryptionException 77 | */ 78 | public function test_decrypting_an_invalid_cipher_with_the_public_key_it_should_fail() 79 | { 80 | $this->expectException(DecryptionException::class); 81 | $this->assertEquals('secret', $this->public->decrypt('INVALID-CIPHER')); 82 | } 83 | 84 | /** 85 | * @throws DecryptionException 86 | */ 87 | public function test_decrypting_an_invalid_cipher_with_the_private_key_it_should_fail() 88 | { 89 | $this->expectException(DecryptionException::class); 90 | $this->assertEquals('secret', $this->private->decrypt('INVALID-CIPHER')); 91 | } 92 | 93 | /** 94 | * @throws InvalidKeyException 95 | */ 96 | public function test_setting_a_public_key() 97 | { 98 | $key = file_get_contents(__DIR__ . '/resources/test_public_key.pem'); 99 | $this->public->setKey($key); 100 | $this->assertTrue(true); 101 | } 102 | 103 | /** 104 | * @throws InvalidKeyException 105 | */ 106 | public function test_setting_a_private_key() 107 | { 108 | $key = file_get_contents(__DIR__ . '/resources/test_private_key.pem'); 109 | $this->private->setKey($key); 110 | $this->assertTrue(true); 111 | } 112 | 113 | /** 114 | * @throws InvalidKeyException 115 | */ 116 | public function test_setting_an_invalid_public_key_file_it_should_fail() 117 | { 118 | $this->expectException(InvalidKeyException::class); 119 | $this->public->setKey('invalid.pem'); 120 | } 121 | 122 | /** 123 | * @throws InvalidKeyException 124 | */ 125 | public function test_setting_an_invalid_public_key_path_it_should_fail() 126 | { 127 | $this->expectException(InvalidKeyException::class); 128 | $this->expectExceptionMessage('The key file is not readable.'); 129 | $this->public->setKey('..'); 130 | } 131 | 132 | /** 133 | * @throws InvalidKeyException 134 | */ 135 | public function test_setting_an_invalid_private_key_file_it_should_fail() 136 | { 137 | $this->expectException(InvalidKeyException::class); 138 | $this->private->setKey('invalid.pem'); 139 | } 140 | 141 | /** 142 | * @throws InvalidKeyException 143 | */ 144 | public function test_setting_an_invalid_private_key_path_it_should_fail() 145 | { 146 | $this->expectException(InvalidKeyException::class); 147 | $this->expectExceptionMessage('The key file is not readable.'); 148 | $this->private->setKey('..'); 149 | } 150 | } -------------------------------------------------------------------------------- /tests/SymmetricTest.php: -------------------------------------------------------------------------------- 1 | encrypt('secret'); 23 | 24 | $this->assertEquals('secret', $symmetric->decrypt($result)); 25 | } 26 | 27 | /** 28 | * @throws DecryptionException 29 | * @throws EncryptionException 30 | */ 31 | public function test_encrypting_and_decrypting_with_a_given_key() 32 | { 33 | $symmetric = new Symmetric(); 34 | $symmetric->setKey(Symmetric::generateKey()); 35 | $result = $symmetric->encrypt('secret'); 36 | 37 | $this->assertEquals('secret', $symmetric->decrypt($result)); 38 | } 39 | 40 | /** 41 | * @throws DecryptionException 42 | */ 43 | public function test_decrypting_an_invalid_cipher_it_should_fail() 44 | { 45 | $symmetric = new Symmetric(); 46 | 47 | $this->expectException(DecryptionException::class); 48 | $this->expectExceptionMessage('Encrypted data is in invalid format.'); 49 | $symmetric->decrypt('Invalid Encrypted Data!'); 50 | } 51 | 52 | /** 53 | * @throws DecryptionException 54 | */ 55 | public function test_decrypting_another_invalid_cipher_it_should_fail() 56 | { 57 | $symmetric = new Symmetric(); 58 | 59 | $this->expectException(DecryptionException::class); 60 | $symmetric->decrypt('WRONG-IV.WRONG-DATE'); 61 | } 62 | 63 | public function test_supportedMethods() 64 | { 65 | $methods = Symmetric::supportedMethods(); 66 | $this->assertIsArray($methods); 67 | } 68 | 69 | public function test_setting_and_getting_a_key() 70 | { 71 | $symmetric = new Symmetric(); 72 | 73 | $symmetric->setKey('key'); 74 | $this->assertEquals('key', $symmetric->getKey()); 75 | } 76 | 77 | /** 78 | * @throws MethodNotSupportedException 79 | */ 80 | public function test_setting_and_getting_a_method() 81 | { 82 | $symmetric = new Symmetric(); 83 | 84 | $method = $symmetric->supportedMethods()[0]; 85 | $symmetric->setMethod($method); 86 | $this->assertEquals($method, $symmetric->getMethod()); 87 | } 88 | 89 | /** 90 | * @throws MethodNotSupportedException 91 | */ 92 | public function test_setting_an_invalid_method_it_should_fail() 93 | { 94 | $symmetric = new Symmetric(); 95 | $this->expectException(MethodNotSupportedException::class); 96 | $this->expectExceptionMessageRegExp('/^The (.+?) method is not supported.$/'); 97 | $symmetric->setMethod('Invalid Method'); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /tests/resources/test_private_key.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN PRIVATE KEY----- 2 | MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAMZWIboZcMz5IpjG 3 | f0ZPQocEKr+FA6ZJXPzn5KKU3i7m49oRsMWQGYrUvqrEF/6bxAraztyFxHkDDQR2 4 | NodVKhkuSm5uYt2GGGQYyGNSAcWpXqA9VS0huN7a1i74wxIMQG5Ah1kzxzyoYfs2 5 | kLWThPG3fXf+DVEq+4wAa+6Hpru1AgMBAAECgYEAmoPluKczQuzg8CbMZwk+qhMr 6 | /pLw+e5zvDAo5iSWR/HnFy/rCK8vxwHhHQayLtBJVa+Lsy+fliszTAOQjbRim2ZP 7 | r6JgcuMg59OILaRfINZVz4BO5jOkyZTPJM5crqXNNGgKCbdevjxDed6uzVW37eVq 8 | mGDrgTcsz31oNG0ECQECQQDmT7466B4gHj64TnoQHakDJwTYB4kJQaOgTbwN20yY 9 | V9AIbt0I16Nf2D/Twl1JI0ownfqsFC8Uf8zeJNmM9BtBAkEA3HVhmKL3+n4zNpv4 10 | bVBAviacQKTWRJqk5prfTYNpM+P+fJ1jL5d1phc7RHAXb9Mg1XwwLqPb+88Kem4u 11 | lveHdQJBALLPgsA1av5AvS1XMInGnWvnJWmcIBbhsWpKMBEKDGPS+Z8yg8XTtfI5 12 | 8QQ1yTt5fKZWYUKswQjHWIW4UnePJoECQQC1y0oGz2TEOKtMvgN3STWKWFaDbCqQ 13 | +iE/VLkNt27aboleA39WMITAyOmDvxdkjGrIARfObisqlAnk+dULguU9AkEAjEgc 14 | iMX8F8lNudUVk8LlocFFEBTO0rQ33o0RzFz9v6GFhcS0c8E7u7OslbEKz/NOET+5 15 | VOAEwUx7091+dhraNw== 16 | -----END PRIVATE KEY----- 17 | -------------------------------------------------------------------------------- /tests/resources/test_public_key.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN PUBLIC KEY----- 2 | MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGViG6GXDM+SKYxn9GT0KHBCq/ 3 | hQOmSVz85+SilN4u5uPaEbDFkBmK1L6qxBf+m8QK2s7chcR5Aw0EdjaHVSoZLkpu 4 | bmLdhhhkGMhjUgHFqV6gPVUtIbje2tYu+MMSDEBuQIdZM8c8qGH7NpC1k4Txt313 5 | /g1RKvuMAGvuh6a7tQIDAQAB 6 | -----END PUBLIC KEY----- 7 | --------------------------------------------------------------------------------