bayfrontmedia/encryptor

A fast, simple two-way encryption library utilizing OpenSSL.

v2.0.0 2023-01-26 18:19 UTC

This package is auto-updated.

Last update: 2024-10-26 22:02:53 UTC


README

A fast, simple two-way encryption library utilizing OpenSSL.

License

This project is open source and available under the MIT License.

Author

Bayfront Media

Requirements

  • PHP ^8.0
  • OpenSSL PHP extension
  • JSON PHP extension

Installation

composer require bayfrontmedia/encryptor

Usage

Start using Encryptor

A private, reproducible key must be passed to the constructor. The same key must be used when encrypting and decrypting. If the key used to encrypt a value is lost, it will not be able to be decrypted.

An optional second constructor parameter allows you to specify which cipher method to use. By default, Encryptor uses AES-256-CBC.

If an invalid cipher method is used, a Bayfront\Encryptor\InvalidCipherException exception will be thrown.

use Bayfront\Encryptor\Encryptor;

$encryptor = new Encryptor('private_key');

Public methods

getKey

Description:

Returns the encryption key.

Parameters:

  • None

Returns:

  • (string)

getCipher

Description:

Returns the cipher method used for encryption.

Parameters:

  • None

Returns:

  • (string)

encrypt

Description:

Encrypts a given value.

Parameters:

  • $value (mixed)
  • $serialize = true (bool)

Returns:

  • (string)

Throws:

  • Bayfront\Encryptor\EncryptException

Example:

try {

    $encrypted = $encryptor->encrypt([
        'name' => 'John',
        'user_id' => 8
    ]);

} catch (EncryptException $e) {
    die($e->getMessage());
}

encryptString

Description:

Encrypts a string without serialization.

Parameters:

  • $value (string)

Returns:

  • (string)

Throws:

  • Bayfront\Encryptor\EncryptException

Example:

try {

    $encrypted_string = $encryptor->encryptString('A string to encrypt');

} catch (EncryptException $e) {
    die($e->getMessage());
}

decrypt

Description:

Decrypts a given value.

Parameters:

  • $data (string)
  • $unserialize = true (bool)

Returns:

  • (mixed)

Throws:

  • Bayfront\Encryptor\DecryptException

Example:

try {

    $decrypted = $encryptor->decrypt($encrypted);

} catch (DecryptException $e) {
    die($e->getMessage());
}

decryptString

Description:

Decrypts a string without unserialization.

Parameters:

  • $data (string)

Returns:

  • (string)

Throws:

  • Bayfront\Encryptor\DecryptException

Example:

try {

    $decrypted_string = $encryptor->decryptString($encrypted_string);

} catch (DecryptException $e) {
    die($e->getMessage());
}