|
| 1 | +# crypto() |
| 2 | +A two-way encryption method/class for PHP. |
| 3 | + |
| 4 | + |
| 5 | +## Requirements: |
| 6 | + |
| 7 | +This function requires that your server has PHP _(7.2.0+)_ and that you have [sodium](https://www.php.net/manual/en/sodium.installation.php) installed and enabled on your server. |
| 8 | + |
| 9 | +## Versions: |
| 10 | + |
| 11 | +July 29, 2019 - Version `1.0.0` is [released](https://github.com/4cm/crypto/blob/master/src/crypto.php). |
| 12 | + |
| 13 | +## Installation: |
| 14 | + |
| 15 | +### With Composer: |
| 16 | + |
| 17 | +``` |
| 18 | +$ composer require 4cm/crypto |
| 19 | +``` |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "require": { |
| 24 | + "4cm/crypto": "*" |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### Without Composer: |
| 30 | + |
| 31 | +Why are you not using [composer](http://getcomposer.org/)? You can [directly download](https://github.com/4cm/crypto/blob/master/src/crypto.php) the php file and upload it to your server and include the file however it is you normally include php files. |
| 32 | + |
| 33 | +```php |
| 34 | +<?php |
| 35 | +require 'path/to/crypto.php'; |
| 36 | +``` |
| 37 | + |
| 38 | +## Key Generation: |
| 39 | + |
| 40 | +For each user of your website/service you should generate a key that is stored somewhere on your server _(best to do so in a sub root directory.)_ |
| 41 | + |
| 42 | +If you use a KMS, just make the necessary changes to not use local paths and rather the paths to your KMS api. That could be your own local KMS hardware or a KMS service such as what AWS and Google Cloud and other KMS providers offer. |
| 43 | + |
| 44 | +You should wrap your call to generate a new key in a try/catch in order to handle `Exception` messages. |
| 45 | + |
| 46 | +An example would be something along the lines of this, handling the `Exception` error messages in whatever way you prefer. |
| 47 | + |
| 48 | +```php |
| 49 | +try { |
| 50 | + // |
| 51 | + (new crypto($keyPath))->generateKey(); |
| 52 | + // |
| 53 | +} catch (Exception $e) { |
| 54 | + // |
| 55 | + die($e->getMessage()); |
| 56 | + // |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Encryption/Decryption Function Variables: |
| 61 | + |
| 62 | +The `crypto()` class has three variables that need to be passed for encryption and decryption: |
| 63 | + |
| 64 | +1. `$keyPath` = the path to an individual users cryptoKey, generated by `(new crypto($keyPath))->generateKey();` and stored somewhere on your server, preferably sub-root. |
| 65 | +2. `$Content` = The content that you want to encrypt or decrypt. |
| 66 | +3. `e` or `d` = The direction of action, either `e` for encryption, or `d` for decryption. |
| 67 | + |
| 68 | + |
| 69 | +## Encryption Example: |
| 70 | + |
| 71 | +The following example will show you how to encrypt a message. |
| 72 | + |
| 73 | +You should wrap your call to generate a new key in a try/catch in order to handle `Exception` messages. |
| 74 | + |
| 75 | +An example would be something along the lines of this, handling the `Exception` error messages in whatever way you prefer. |
| 76 | + |
| 77 | +```php |
| 78 | +// |
| 79 | +$keyPath = '/path/to/subrootfolder/userid.key'; |
| 80 | +$Content = 'This is a message that we want to encrypt'; |
| 81 | +// |
| 82 | +try { |
| 83 | + // |
| 84 | + $Content = (new crypto($keyPath, $Content, 'e'))->crypto(); |
| 85 | + // |
| 86 | +} catch (Exception $e) { |
| 87 | + // |
| 88 | + die('Encryption Error: ' . $e->getMessage()); |
| 89 | + // |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Decryption Example: |
| 94 | + |
| 95 | +The following example will show you how to encrypt a message. |
| 96 | + |
| 97 | +Notice that the difference in this example is the `'d'` being passed, instead of `'e'` for the direction variable. |
| 98 | + |
| 99 | +You should wrap your call to generate a new key in a try/catch in order to handle `Exception` messages. |
| 100 | + |
| 101 | +An example would be something along the lines of this, handling the `Exception` error messages in whatever way you prefer. |
| 102 | + |
| 103 | +```php |
| 104 | +// |
| 105 | +$keyPath = '/path/to/subrootfolder/kms/userid.key'; |
| 106 | +$EncryptedContent = 'XyjE80p/QF72xwHx6HSNJt8WKxodx0nKhDaNeCe0koxvQ=='; // just an example of encrypted content |
| 107 | +// |
| 108 | +try { |
| 109 | + // |
| 110 | + $Content = (new crypto($keyPath, $EncryptedContent, 'd'))->crypto(); |
| 111 | + // |
| 112 | +} catch (Exception $e) { |
| 113 | + // |
| 114 | + die('Decryption Error: ' . $e->getMessage()); |
| 115 | + // |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Security Contact Information: |
| 120 | + |
| 121 | +To report a security vulnerability please reference the support email address within our [composer.json](https://github.com/4cm/crypto/blob/master/composer.json) file. |
| 122 | + |
| 123 | +We will coordinate any necessary security resolutions and provide disclosure if requested. |
| 124 | + |
| 125 | + |
| 126 | +## License: |
| 127 | + |
| 128 | +The MIT License (MIT). Please see [License File](LICENSE) for more information. |
0 commit comments