martinusso / opencrypt
Two-way encryption (encrypt and decrypt) data using PHP with OpenSSL
Installs: 19 301
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 2
Forks: 4
Open Issues: 0
Requires
- php: ^5.6 || ^7.0
- ext-openssl: *
Requires (Dev)
- phpunit/phpunit: ^6.4
This package is auto-updated.
Last update: 2024-10-22 23:16:27 UTC
README
Two-way encryption (encrypt and decrypt) data using PHP with OpenSSL
Installation
composer require martinusso/opencrypt
Tips
- $secretKey should have been previously generated in a cryptographically safe way, like openssl_random_pseudo_bytes. OpenCrypt has a static method
OpenCrypt::generateKey()
for this.
Usage
$password = "OpenCrypt";
// Should have been previously generated in a cryptographically safe way
$secretKey = 'SECRET_KEY';
// You can pass the IV as argument or it is generated automatically
$openCrypt = new OpenCrypt($secretKey [, string $iv ]);
// get the IV
$iv = $openCrypt->iv();
// encrypt
$encryptedPassword = $openCrypt->encrypt($password);
// $encryptedPassword = 'GWw3bqL7FqjmRs0yyIR/8A=='
// decrypt
$decryptedPassword = $openCrypt->decrypt($encryptedPassword);
// $decryptedPassword = 'OpenCrypt'
generate IV
OpenCrypt offers a static method to generate a safe IV:
$iv = OpenCrypt::generateIV();
generate key
it is also possible to generate a safe secret key:
$secretKey = OpenCrypt::generateKey();
License
This software is open source, licensed under the The MIT License (MIT). See LICENSE for details.