Created
October 26, 2016 09:38
-
-
Save toleillo/2d3082249ff4af17e9bb10f7076fbd56 to your computer and use it in GitHub Desktop.
Class for MAC generation on IECISA Secure payment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class IecisaApi | |
* | |
* MAC generation for IECISA secure payment. | |
* | |
* $message = CF_XtnType + CF_User + CF_Date + CF_Time + CF_Amount +CF_Currency + CF_TicketNumber + CF_Lang | |
* | |
* Example usage: | |
* $message = 'V000002120212021217022014032553T51111111111111111104151500EUR000123asdfasdfasdfasdfasdf'; | |
* $secret = '133D59719B1B2134A3DB3A4FF2667A3B'; | |
* | |
* $iecisaApi = new IecisaApi($message, $secret); | |
* $mac = $iecisaApi->getMAC(); | |
* | |
* | |
* @author Adrián Navarro <[email protected]> | |
* @author Juan Pastor <[email protected]> | |
* | |
*/ | |
class IecisaApi | |
{ | |
protected $mac; | |
/** | |
* @param $data String concat transaction fields in order. | |
* @param $secret Secret key for your IECISA user | |
*/ | |
public function __construct($data, $secret) | |
{ | |
$zeros = '0000000000000000'; | |
$dataPadding = $data . hex2bin('80'); | |
$total = strlen($dataPadding); | |
if (0 !== $total % 8) { | |
for ($i = 0; $i < 8 -($total % 8); $i++) { | |
$dataPadding .= pack('H*', '0'); | |
} | |
} | |
$tempKey = $zeros; | |
for ($i = 0; $i < strlen($dataPadding)/8; $i++) { | |
$string = bin2hex(substr($dataPadding, $i*8, 8)); | |
$xor = bin2hex(pack('H*', $tempKey) ^ pack('H*', $string)); | |
$tempKey = $this->encrypt(pack('H*', $xor), pack('H*', substr($secret, 0, 16))); | |
} | |
$string = substr($secret, 16); | |
$xor = $this->decrypt(pack('H*', $tempKey), pack('H*', $string)); | |
$this->mac = $this->encrypt(pack('H*', $xor), pack('H*', substr($secret, 0, 16))); | |
} | |
public function getMAC() | |
{ | |
return substr($this->mac, 0, 8); | |
} | |
/** | |
* @param $data string | |
* @param $key string | |
* @return string | |
*/ | |
private function encrypt($data, $key) | |
{ | |
return bin2hex(mcrypt_encrypt(MCRYPT_DES, $key, $data, MCRYPT_MODE_ECB)); | |
} | |
/** | |
* @param $data string | |
* @param $key string | |
* @return string | |
*/ | |
private function decrypt($data, $key) | |
{ | |
return bin2hex(mcrypt_decrypt(MCRYPT_DES, $key, $data, MCRYPT_MODE_ECB)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment