Skip to content

Instantly share code, notes, and snippets.

@tomaskavalek
Last active October 2, 2018 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomaskavalek/7a8e7a11a58eaa1b70e475e0b9d1b473 to your computer and use it in GitHub Desktop.
Save tomaskavalek/7a8e7a11a58eaa1b70e475e0b9d1b473 to your computer and use it in GitHub Desktop.
GopayHelper – replace mcrypt functions for PHP 7.2
<?php
public static function encrypt($data, $secureKey) {
return GopayHelper::encrypt_php72($data, $secureKey, 'DES-EDE3');
}
public static function decrypt($data, $secureKey) {
return GopayHelper::decrypt_php72($data, $secureKey, 'DES-EDE3');
}
/**
* @param string $data
* @param string $key
* @param string $method
* @return string
*/
public static function encrypt_php72(string $data, string $key, string $method): string
{
$data_padded = $data;
if (strlen($data_padded) % 8) {
$data_padded = str_pad($data_padded,
strlen($data_padded) + 8 - strlen($data_padded) % 8, "\0");
}
$ivSize = openssl_cipher_iv_length($method);
$iv = openssl_random_pseudo_bytes($ivSize);
$encrypted = openssl_encrypt($data_padded, $method, $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
return bin2hex($encrypted);
}
/**
* @param string $data
* @param string $key
* @param string $method
* @return string
*/
public static function decrypt_php72(string $data, string $key, string $method): string
{
$data = GopayHelper::convert($data);
$ivSize = openssl_cipher_iv_length($method);
$iv = substr($data, 0, $ivSize);
$data = openssl_decrypt(substr($data, $ivSize), $method, $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
return trim($data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment