daycry/encryption

Encryption for Codeigniter 4

v2.1.8 2024-07-18 08:39 UTC

This package is auto-updated.

Last update: 2024-11-18 09:30:05 UTC


README

Donate

Encryption Library

Encryption for Codeigniter 4 compatibility with CryptoJsAes

Build status Coverage status Downloads GitHub release (latest by date) GitHub stars GitHub license

Installation via composer

Use the package with composer install

> composer require daycry/encryption

Manual installation

Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\Encryption namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:

$psr4 = [
    'Config'      => APPPATH . 'Config',
    APP_NAMESPACE => APPPATH,
    'App'         => APPPATH,
    'Daycry\Encryption' => APPPATH .'Libraries/encryption/src',
];

Usage Loading Library

$encryption = new \Daycry\Encryption\Encryption();
$data = $encryption->encrypt( 'data' );
var_dump( $data );

$encryption = new \Daycry\Encryption\Encryption();
$data = $encryption->decrypt( 'data' );
var_dump( $data );

Setting Cipher algoritm

$encryption = new \Daycry\Encryption\Encryption();
$data = $encryption->setCipher( 'AES-256-CTR' )->encrypt( 'data' );
var_dump( $data );

Setting Key

$encryption = new \Daycry\Encryption\Encryption();
$encrypt = $obj->setCipher( 'AES-256-CTR' )->setKey( '%T3sT1nG$' )->encrypt( 'data', true );
var_dump( $data );

Get algoritm available

var_dump( openssl_get_cipher_methods() );

CryptoJs Compatibility Returning Key

$result = \Daycry\Encryption\CryptoJsAes::encrypt( "Hello", "123456", true );

$textPlain = \Daycry\Encryption\CryptoJsAes::decrypt( $result );

CryptoJs Compatibility Without Returning Key

$result = \Daycry\Encryption\CryptoJsAes::encrypt( "Hello", "123456", false );

$textPlain = \Daycry\Encryption\CryptoJsAes::decrypt( $result, "123456" );

If you want encrypt/decrypt in javascript you must add this files in your view.

./public/assets

Example:

<script type="text/javascript" src="<?php echo base_url("assets/js/jquery.min.js")?>"></script>
<script type="text/javascript" src="<?php echo base_url("assets/js/crypto-js.min.js")?>"></script>
<script type="text/javascript" src="<?php echo base_url("assets/js/encryption.js")?>"></script>

<script>
	Encryption.getInstance().setKey("hello");
	var encrypted = Encryption.getInstance().encrypt("hello text");
	var decrypted = Encryption.getInstance().decrypt(encrypted);
	console.log(decrypted);
</script>

Encrypt and descrypt function accept the key as second parameter.

How Run Tests

cd vendor\daycry\encryption\
composer install
vendor\bin\phpunit