Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/src/CoreBundle/Services/
Upload File :
Current File : /var/www/v3.cesa.co.za/src/CoreBundle/Services/Openssl_EncryptDecrypt.php

<?php

/*
 * Encrypt and decrypt forgot text
 */

namespace App\CoreBundle\Services;

use Defuse\Crypto\Crypto;
use Defuse\Crypto\Key;

/**
 * Encrypt and decrypt strings
 *
 * @author otto
 */
class Openssl_EncryptDecrypt {

    public $encryption_key = 'def000005f0b4dc0f66e4309bc5fde80177a37b5071203378857d90427b38d67ad39a228012db434a17450c89d49d32e03617420c6f50908a65303df3e27f001295d9982';

    function encrypt($pure_string) {

        return Crypto::encrypt($pure_string, Key::loadFromAsciiSafeString($this->encryption_key));

        /*
          $cipher = 'AES-256-CBC';
          $options = OPENSSL_RAW_DATA;
          $hash_algo = 'sha256';
          $sha2len = 32;
          $ivlen = openssl_cipher_iv_length($cipher);
          $iv = openssl_random_pseudo_bytes($ivlen);
          $ciphertext_raw = openssl_encrypt($pure_string, $cipher, $this->encryption_key, $options, $iv);
          $hmac = hash_hmac($hash_algo, $ciphertext_raw, $this->encryption_key, true);
          return $iv . $hmac . $ciphertext_raw;
         */
    }

    function decrypt($encrypted_string) {

        return Crypto::decrypt($encrypted_string, Key::loadFromAsciiSafeString($this->encryption_key));

        /*
          $cipher = 'AES-256-CBC';
          $options = OPENSSL_RAW_DATA;
          $hash_algo = 'sha256';
          $sha2len = 32;
          $ivlen = openssl_cipher_iv_length($cipher);
          $iv = substr($encrypted_string, 0, $ivlen);
          $hmac = substr($encrypted_string, $ivlen, $sha2len);
          $ciphertext_raw = substr($encrypted_string, $ivlen + $sha2len);
          $original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $this->encryption_key, $options, $iv);
          $calcmac = hash_hmac($hash_algo, $ciphertext_raw, $this->encryption_key, true);
          if (function_exists('hash_equals')) {
          if (hash_equals($hmac, $calcmac))
          return $original_plaintext;
          } else {
          if ($this->hash_equals_custom($hmac, $calcmac))
          return $original_plaintext;
          }
         */
    }

    /**
     * (Optional)
     * hash_equals() function polyfilling.
     * PHP 5.6+ timing attack safe comparison
     */
    function hash_equals_custom($knownString, $userString) {
        if (function_exists('mb_strlen')) {
            $kLen = mb_strlen($knownString, '8bit');
            $uLen = mb_strlen($userString, '8bit');
        } else {
            $kLen = strlen($knownString);
            $uLen = strlen($userString);
        }
        if ($kLen !== $uLen) {
            return false;
        }
        $result = 0;
        for ($i = 0; $i < $kLen; $i++) {
            $result |= (ord($knownString[$i]) ^ ord($userString[$i]));
        }
        return 0 === $result;
    }

}