Encryption::encrypt PHP Method

encrypt() public static method

Encrypt a string.
public static encrypt ( string $plain ) : string
$plain string
return string
    public static function encrypt($plain)
    {
        if (!function_exists('openssl_cipher_iv_length') || !function_exists('openssl_random_pseudo_bytes') || !function_exists('openssl_encrypt')) {
            throw new Exception("Encryption function don't exists");
        }
        // generate initialization vector,
        // this will make $iv different every time,
        // so, encrypted string will be also different.
        $iv_size = openssl_cipher_iv_length(self::CIPHER);
        $iv = openssl_random_pseudo_bytes($iv_size);
        // generate key for authentication using ENCRYPTION_KEY & HMAC_SALT
        $key = mb_substr(hash(self::HASH_FUNCTION, Config::get('ENCRYPTION_KEY') . Config::get('HMAC_SALT')), 0, 32, '8bit');
        // append initialization vector
        $encrypted_string = openssl_encrypt($plain, self::CIPHER, $key, OPENSSL_RAW_DATA, $iv);
        $ciphertext = $iv . $encrypted_string;
        // apply the HMAC
        $hmac = hash_hmac('sha256', $ciphertext, $key);
        return $hmac . $ciphertext;
    }

Usage Example

Beispiel #1
0
 protected function index()
 {
     $this->data['button_confirm'] = $this->language->get('button_confirm');
     $this->data['button_back'] = $this->language->get('button_back');
     $this->load->model('checkout/order');
     $order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     $this->data['mid'] = $this->config->get('paymate_username');
     $this->load->library('encryption');
     $encryption = new Encryption($this->config->get('config_encryption'));
     $this->data['return'] = HTTPS_SERVER . 'index.php?route=payment/paymate/callback&oid=' . base64_encode($encryption->encrypt($order_info['order_id'])) . '&conf=' . base64_encode($encryption->encrypt($order_info['payment_firstname'] . $order_info['payment_lastname']));
     if ($this->config->get('paymate_include_order')) {
         $this->data['ref'] = html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8') . " (#" . $order_info['order_id'] . ")";
     } else {
         $this->data['ref'] = html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8');
     }
     $currency = array('AUD', 'NZD', 'USD', 'EUR', 'GBP');
     if (in_array(strtoupper($order_info['currency']), $currency)) {
         $this->data['currency'] = $order_info['currency'];
         $this->data['amt'] = $this->currency->format($order_info['total'], $order_info['currency'], $order_info['value'], FALSE);
     } else {
         for ($findcur = 0; $findcur < sizeof($currency); $findcur++) {
             if ($this->currency->getValue($currency[$findcur])) {
                 $this->data['currency'] = $currency[$findcur];
                 $this->data['amt'] = $this->currency->format($order_info['total'], $currency[$findcur], '', FALSE);
                 break;
             } elseif ($findcur == sizeof($currency) - 1) {
                 $this->data['currency'] = 'AUD';
                 $this->data['amt'] = $order_info['total'];
             }
         }
     }
     $this->data['pmt_contact_firstname'] = html_entity_decode($order_info['payment_firstname'], ENT_QUOTES, 'UTF-8');
     $this->data['pmt_contact_surname'] = html_entity_decode($order_info['payment_lastname'], ENT_QUOTES, 'UTF-8');
     $this->data['pmt_contact_phone'] = $order_info['telephone'];
     $this->data['pmt_sender_email'] = $order_info['email'];
     $this->data['regindi_address1'] = html_entity_decode($order_info['payment_address_1'], ENT_QUOTES, 'UTF-8');
     $this->data['regindi_address2'] = html_entity_decode($order_info['payment_address_2'], ENT_QUOTES, 'UTF-8');
     $this->data['regindi_sub'] = html_entity_decode($order_info['payment_city'], ENT_QUOTES, 'UTF-8');
     $this->data['regindi_state'] = html_entity_decode($order_info['payment_zone'], ENT_QUOTES, 'UTF-8');
     $this->data['regindi_pcode'] = html_entity_decode($order_info['payment_postcode'], ENT_QUOTES, 'UTF-8');
     $this->data['pmt_country'] = $order_info['iso_code_2'];
     $this->data['action'] = 'https://www.paymate.com/PayMate/ExpressPayment';
     $this->data['back'] = HTTPS_SERVER . 'index.php?route=checkout/payment';
     $this->id = 'payment';
     if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/paymate.tpl')) {
         $this->template = $this->config->get('config_template') . '/template/payment/paymate.tpl';
     } else {
         $this->template = 'default/template/payment/paymate.tpl';
     }
     $this->render();
 }
All Usage Examples Of Encryption::encrypt