Advertisement
Guest User

Untitled

a guest
May 28th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.27 KB | None | 0 0
  1. class SignGenerator {
  2.    
  3.     // Merchant secure data - merchant key
  4.     private $key = "1234567812345678123456781234567812345678123456781234567812345678";
  5.    
  6.     // Transaction data
  7.     private $mid;
  8.     private $amount;
  9.     private $currencyAplhaCode;
  10.     private $msTxnId;
  11.     private $firstName;
  12.     private $familyName;
  13.     private $timestamp;
  14.    
  15.     // Sign config
  16.     private $_mode = MCRYPT_MODE_CBC;
  17.     private $_cipher;
  18.     private $_paddingType;
  19.    
  20.    
  21.     public function __construct($mid,$amount,$currency,$mstxnid,$firstname,$familyname,$timestamp){
  22.         $this->_cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', $this->_mode, '');
  23.         $this->_paddingType = 'PKCS7';
  24.        
  25.         $this->mid = $mid;
  26.         $this->amount = $amount;
  27.         $this->currencyAplhaCode = $currency;
  28.         $this->msTxnId = $mstxnid;
  29.         $this->firstName = $firstname;
  30.         $this->familyName = $familyname;
  31.         $this->timestamp = $timestamp;
  32.        
  33.         $this->iv = $mid.strrev($mid);
  34.     }
  35.    
  36.     public function getHexKey(){
  37.         return pack("H*" , $this->key );
  38.     }
  39.  
  40.     /* 
  41.      * WORK IN PHP OR NEWER
  42.     public function getHexKey(){
  43.         $hKey = "";
  44.         for($i=0;$i<strlen($this->key);$i=$i+2){
  45.             $hKey .= hex2bin($this->key[$i].$this->key[$i+1]);
  46.         }
  47.         return $hKey;
  48.     }
  49.     */
  50.    
  51.     public function getPlainText(){
  52.         return $this->mid.$this->amount.$this->currencyAplhaCode.$this->msTxnId.$this->firstName.$this->familyName.$this->timestamp;
  53.     }
  54.    
  55.     private function getData(){
  56.         return sha1($this->getPlainText(),true);
  57.     }
  58.    
  59.     public function signTransaction(){
  60.         if ($this->_paddingType == 'PKCS7'){
  61.             $data = $this->AddPadding($this->getData());
  62.         }
  63.  
  64.         mcrypt_generic_init($this->_cipher, $this->getHexKey(), $this->iv);
  65.         $result = mcrypt_generic($this->_cipher, $data);
  66.         mcrypt_generic_deinit($this->_cipher);
  67.      
  68.         return strtoupper(substr(bin2hex($result),0,32));
  69.     }
  70.    
  71.     private function AddPadding($data){
  72.       $block = mcrypt_get_block_size('des', $this->_mode);
  73.       $pad   = $block - (strlen($data) % $block);
  74.       $data .= str_repeat(chr($pad), $pad);
  75.       return $data;
  76.     }
  77.    
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement