romabeckman/encryption

A library to encrypt and decrypt Tokens making signature. Encode and decode (JWT) in PHP.

1.0.3 2020-02-14 22:58 UTC

This package is auto-updated.

Last update: 2024-09-15 09:08:25 UTC


README

  • Require php 7.1
  • composer require romabeckman/encryption

Examples:

  • Example1: Encrypt and decrypt content adding signature
  • Example2: Encrypt and decrypt payload adding signature
  • Example3: JWT Token

Simple to encrypte and decrypt a text:

$encryption = new Encryption("__ENCRYPT_KEY__", "__SECURITY_KEY__");
$hash = Encryption::encrypt($Encryption, 'My text to example'); 
echo Encryption::decrypt($Encryption, $hash);
// must print 'My text to example'

You may encrypt only Payload without RFC 7519 (JWT) method. Some reason, you need encrypt only Payload and transmite by insecurity way. Example:

$encryption = new Encryption("__ENCRYPT_KEY__", "__SECURITY_KEY__"); 
$Payload = new Payload(
   ['name' => 'Firstname Lastname', 'email' => 'my_email@email.com'], // must encrypt
   strtotime('2 hours') // expiration time
);
// Optional attributes
$Payload
   ->setJti(1)
   ->setSub('My Teste')
   ->setCheckIssDomain(true) // Default false. If true, will set 'iss' like http://my_host.com and check when encoded
   ->setAud('Username');
$token = Payload::encode($Encryption, $Payload);
//$token = Payload::encode($Encryption, $Payload, true); // To get token for use in url

$decoded = Payload::decode($Encryption, $token);
//$decoded = Payload::decode($Encryption, $token, true); // To get token for use in url

Generate JWT token conform RFC 7519 (JWT) method. Support only: HS256, HS384 and HS512. Example:

$Encryption = new Encryption('__ENCRYPT_KEY__', '__SECURITY_KEY__');
$Payload = new Payload(
        ['nome' => 'Firstname Lastname', 'email' => 'my_email@email.com'],
        strtotime('2 hours')
);
$jwt = Jwt::encode($Encryption, $Payload);
$decoded = Jwt::decode($Encryption, $jwt);