Skip to content

phlib/encrypt

Repository files navigation

phlib/encrypt

Code Checks Codecov Latest Stable Version Total Downloads Licence

PHP encryption/decryption tool

Install

Via Composer

$ composer require phlib/encrypt

Usage

Creation of an encryptor

$encryptor = new \Phlib\Encrypt\Encryptor\OpenSsl($encryptionPassword);

The encryption password should be a random string of data, preferably at least 32 bytes long, and should be stored on the server. For example, this could be a configuration value or constant (perhaps base64 encoded depending on how the value needs to be stored).

The same encryption password must be used to decrypt as was used to encrypt.

Example of creating an encryption password

$encryptionPassword = \openssl_random_pseudo_bytes(32);

Encrypt/decrypt some data

$encryptor = new \Phlib\Encrypt\Encryptor\OpenSsl($encryptionPassword);
$myData    = 'some sensitive data which needs encrypting';

$encrypted = $encryptor->encrypt($myData);

// $encryptor could be a completely different instance here,
// so long as it is initialised with the same encryption password
$decrypted = $encryptor->decrypt(encrypted);

License

This package is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see http://www.gnu.org/licenses/.