maison_alexandra/www/system-brs-core/library/cDefuseCrypto/cDefuseCrypto.php

151 lines
4.5 KiB
PHP

<?php
namespace BRSystems\Core\Library\cDefuseCrypto;
use BRSystems\Core\Library\cNumbers\cNumbers;
use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;
use Exception;
/**
* CMS/APP cDefuseCrypto module
*
* @category LIB cDefuseCrypto
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @since Version 1.0
*/
class cDefuseCrypto
{
private $sName;
private $oKey;
private $bStatus = true;
/**
* Class constructor
*/
public function __construct() {
// echo "openssl version text: " . OPENSSL_VERSION_TEXT . "\n";
// echo "openssl version number: " . OPENSSL_VERSION_NUMBER . "\n";
$this->sName = get_class();
if(version_compare(PHP_VERSION, '7.0.0') >= 0)
require_once 'php-encryption-master/defuse-crypto.phar';
else
require_once 'php-encryption-master/defuse-crypto-php5.phar';
// Set my secret pass
$this->oKey = Key::loadFromAsciiSafeString("def0000030259bf916c771f1beb1b7d9767033d054f6a639d860d67c61ca34d63ec3ee8ffbce52f73b0ea6c6e86d12ef20059c6419d9e44a57dce995515a11eb02da45df");
// Check if necessary environment is ok
try{
if(!in_array('aes-256-ecb', openssl_get_cipher_methods(), true))
trigger_error("cDefuseCrypto couldn't initiate class due to missing 'aes-256-ecb' cipher method ---> Encryption failed!");
if(!strlen($sCipherData = Crypto::encrypt('test', $this->oKey, false)))
trigger_error("cDefuseCrypto couldn't initiate class due to missing parts ---> Encryption failed!");
}catch (Exception $e){
$this->bStatus = false;
trigger_error('cDefuseCrypto couldnt initiate class due to missing parts ---> switching to older data encryption');
}
}
/**
* Function to encrypt data
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cDefuseCrypto
* @param string $msg - data to encrypt
* @param bool|cDefuseCrypto $oKey - encrypt key
* @return string - encrypted string
*/
public function encrypt($msg, $oKey = null){
$ciphertext = '';
if($this->bStatus){
$ciphertext = Crypto::encrypt((string)$msg, ($oKey ? $oKey : $this->oKey), false);
$ciphertext = $this->safeBase64encode($ciphertext);
}
return $ciphertext;
}
/**
* Function to decrypt data
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cDefuseCrypto
* @param string $sCipherText - encrypted data to decrypt
* @param bool|cDefuseCrypto $oKey - encrypt key
* @return string - decrypted string
*/
public function decrypt($sCipherText, $oKey = null){
$sMsg = '';
if($this->bStatus){
$sCipherText = $this->safeBase64decode($sCipherText);
$sMsg = Crypto::decrypt($sCipherText, ($oKey ? $oKey : $this->oKey), false);
}
return $sMsg;
}
/**
* Function to create new Defuse encryption key / load key rom string
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cDefuseCrypto
* @return string - encrypted string
*/
public function createNewRandomDefuseKey(){
return Key::createNewRandomKey()->saveToAsciiSafeString();
}
public function loadDefuseKeyFromString($sKey){
return Key::loadFromAsciiSafeString($sKey);
}
/**
* Function to safely encode base 64 for url
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cDefuseCrypto
* @param string $data - data to encrypt
* @return string - encrypted string
*/
public function safeBase64encode($data){
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
/**
* Function to safely decode base 64 for url
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cDefuseCrypto
* @param string $data - encrypted data to decrypt
* @return string - decrypted string
*/
public function safeBase64decode($data){
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
/**
* Function to generate unique token for CSRF (Cross-site request forgery)
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category cDefuseCrypto
* @return string - sha1 encrypted string
*/
public static function genToken(){
return sha1(time() . cNumbers::getRandomChString());
}
}