sparqapi/api/application/helpers/custom_encryption_helper.php
2022-10-31 22:46:00 +05:30

88 lines
2.7 KiB
PHP

<?php
/**
* JSON Web Token implementation, based on this spec:
* http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
*
* PHP version 5
*
* @category Authentication
* @package Authentication_JWT
* @author Neuman Vong <neuman@twilio.com>
* @author Anant Narayanan <anant@php.net>
* @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD
* @link https://github.com/firebase/php-jwt
*/
class custom_encryption
{
// Store cipher method
static private $ciphering = "AES-256-CBC";
static private $key = "Awuc64pVpcB9xf";
public static function encode($str)
{
$string_to_encrypt = $str;
// $string_to_encrypt = 'CJf1ac5a9c47284292d42eadc8f30b75df/20201015';
// echo "Original String: " . $string_to_encrypt . "\n";
// Use OpenSSl encryption method
$iv_length = openssl_cipher_iv_length(SELF::$ciphering);
$options = 0;
// echo '$iv_length-'.$iv_length;
// Use random_bytes() function which gives
// randomly 16 digit values
// $encryption_iv = random_bytes($iv_length);
$encryption_iv = openssl_random_pseudo_bytes($iv_length);
// echo '$encryption_iv-'.$encryption_iv;
// Alternatively, we can use any 16 digit
// characters or numeric for iv
$encryption_key = openssl_digest(SELF::$key, 'MD5', TRUE);
// echo '$encryption_key='.$encryption_key;
// Encryption of string process starts
$encryption = openssl_encrypt($string_to_encrypt, SELF::$ciphering,
$encryption_key, $options, $encryption_iv);
// Display the encrypted string
// echo "Encrypted String: " . $encryption . "\n";
return base64_encode($encryption.'.'.base64_encode($encryption_iv));
// return base64_encode(openssl_encrypt($data, $this->method, $this->key, 0, $iv) . $this->separator . base64_encode($iv));
}
public static function decode($str)
{
//echo 'Received-'.base64_decode($str);
$received_data = explode('.',base64_decode($str));
$str = $received_data[0];
// Use OpenSSl encryption method
$iv_length = openssl_cipher_iv_length(SELF::$ciphering);
$options = 0;
// echo '$iv_length-'.$iv_length;
// Decryption of string process starts
// Used random_bytes() which gives randomly
// 16 digit values
// $decryption_iv = random_bytes($iv_length);
$decryption_iv = base64_decode($received_data[1]);
// echo '$decryption_iv-'.$decryption_iv;
// Store the decryption key
$decryption_key = openssl_digest(SELF::$key, 'MD5', TRUE);
// echo '$encryption_key='.$decryption_key;
// Descrypt the string
$decryption = openssl_decrypt ($str, SELF::$ciphering,
$decryption_key, $options, ($decryption_iv));
// Display the decrypted string
// echo "Decrypted String: " . $decryption;
return $decryption;
}
}