<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dave Barnes</title>
	<atom:link href="http://www.davebarnes.co.uk/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davebarnes.co.uk/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 09 Feb 2010 22:21:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HSBC PHP CPI Integration Without Installing Software</title>
		<link>http://www.davebarnes.co.uk/blog/2010/02/hsbc-php-cpi-integration-without-installing-software/</link>
		<comments>http://www.davebarnes.co.uk/blog/2010/02/hsbc-php-cpi-integration-without-installing-software/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 22:18:04 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.davebarnes.co.uk/blog/?p=4</guid>
		<description><![CDATA[All you need are:

Your StorefrontId – this is your 8 digit ID, this will need to be prefixed with your 2-letter country code and have the 3-letter currency code appended to the end. E.G. UK12345678GBP
Your encryption key this is a 32 character BASE64 encoded string  unique to you.
MHASH &#38; MCRYPT available  to PHP.
SSL [...]]]></description>
			<content:encoded><![CDATA[<p>All you need are:</p>
<ul>
<li>Your StorefrontId – this is your 8 digit ID, this will need to be prefixed with your 2-letter country code and have the 3-letter currency code appended to the end. E.G. <strong>UK12345678GBP</strong></li>
<li>Your encryption key this is a 32 character BASE64 encoded string  unique to you.</li>
<li><a href="http://www.php.net/mhash" target="_blank">MHASH</a> &amp; <a href="http://www.php.net/mcrypt" target="_blank">MCRYPT</a> available  to PHP.</li>
<li>SSL up and running on your server, HSBC will reject any requests  that don’t come from an SSL connection.</li>
<li>The class below to generate the hash (<strong>HSBC::GenerateHash()</strong>) from an array.</li>
</ul>
<p><span id="more-4"></span></p>
<pre>&lt;?php

class HSBC{

  private $cpiKey ='zEeWQNKelqPE2DRFueuDq1QrASjux2lM';  // YOUR own hash
  private $_fldif;
  private $a;

  function __construct(){
    $s = 'KmJTwzVPwjoxQdWJb1BxbuhBSa2RuM05+/aUdgYoGdFWWf04CKIQTxtxLeKCp+5J';
    $s1 = 'y8YhmjsAoMUW9RxfXBSos0A6LwGd+5pXv/MRAKCYFLG';
    $s2 = 'BqRkPAG8DFFAdeN5SMAArktCYuUGXi2q88EDoOs3Ykw0k';
    $this-&gt;a = chr(98).chr(84).chr(120).chr(114).chr(66).chr(87).chr(80).chr(112);

    $this-&gt;_fldif = $this-&gt;initKey($s, $s1, $s2);
    $this-&gt;_fldif = substr($this-&gt;_fldif,0,44);
  }

  public function GenerateHash($vector){

    $vector1 = array();
    for($i = 0; $i &lt; sizeof($vector); $i++)
    {
      $flag = false;
      $s2 = $vector[$i];
      $vSize= sizeof($vector1);
      for($k = 0; $k &lt; $vSize &amp;&amp; !$flag; $k++)
      {
        $s4 = $vector1[$k];
        $l = strcmp($s2, $s4);
        if($l &lt;= 0)
        {
          array_push($vector1, '');
          for($r = sizeof($vector1)-2; $r &gt;= $k; $r--)
          $vector1[$r+1] = $vector1[$r];

          $vector1[$k] = $s2;
          $flag = true;
        }
      }

      if(!$flag) array_push($vector1, $s2);
    }

    $s1 = '';
    for($j = 0; $j &lt; sizeof($vector1); $j++)
    {
      $s3 = $vector1[$j];
      $s1 = $s1 . $s3;
    }

    $abyte0 = $this-&gt;decryptToBinary($this-&gt;cpiKey);

    $ret = base64_encode(mhash(MHASH_SHA1, $s1.$abyte0, $abyte0));
    return $ret;
  }

  private function rot13(&amp;$abyte0)
  {
    for($i = 0; $i &lt; strlen($abyte0); $i++)
    {
      $c = ord($abyte0[$i]);
      if($c &gt;= ord('a') &amp;&amp; $c &lt;= ord('m') || $c &gt;= ord('A') &amp;&amp; $c &lt;= ord('M'))
      $abyte0[$i] = chr($c + 13);
      else
      if($c &gt;= ord('n') &amp;&amp; $c &lt;= ord('z') || $c &gt;= ord('N') &amp;&amp; $c &lt;= ord('Z'))
      $abyte0[$i] = chr($c - 13);
    }
  }

  private function encode($abyte0) {
    return base64_encode($abyte0);
  }

  private function decode($s) {
    return base64_decode($s);
  }

  private function encrypt($abyte0, $abyte1)
  {
    $td = mcrypt_module_open (MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
    $iv = $this-&gt;a;
    $ks = mcrypt_enc_get_key_size ($td);
    $key = substr($abyte1, 0, $ks);

    /* Intialize encryption */
    mcrypt_generic_init ($td, $key, $iv);
    return mcrypt_generic ($td, $abyte0);
  }

  private function decrypt($abyte0, $abyte1)
  {
    $td = mcrypt_module_open (MCRYPT_DES, '', MCRYPT_MODE_CBC, '');
    $iv = $this-&gt;a;
    $ks = mcrypt_enc_get_key_size ($td);
    $key = substr($abyte1, 0, $ks);

    /* Intialize encryption */
    mcrypt_generic_init ($td, $key, $iv);
    $ret = mdecrypt_generic($td, $abyte0);

    while($ret[strlen($ret)-1] == "\4" &amp;&amp; strlen($ret) &gt; 0){
      $ret=substr($ret, 0, strlen($ret)-1);
    }
    return $ret;
  }

  private function encryptEncode($abyte0, $abyte1)
  {
    return $this-&gt;encode($this-&gt;encrypt($abyte0, $abyte1));
  }

  private function decodeDecrypt($s, $abyte0)
  {
    return $this-&gt;decrypt($this-&gt;decode($s), $abyte0);
  }

  private function initKey($s, $s1, $s2)
  {
    $abyte0 = chr(0);
    $abyte1 = $s1;
    $abyte2 = $s2;
    $byte0 = 4;
    $i = $byte0 + 9;
    $j = rand(0, 30);
    $j = 0;
    if($j &gt; $byte0 * $i) $j -= $byte0 * $i;

    $k = 0;
    for($l = 0; $l &lt; $byte0 * $i; $l++)
    {
      switch(($j + $l) % $i)
      {
        case 0: // '\0'
        if($k == 2)
        {
          $abyte0 = $this-&gt;encrypt($abyte1, $abyte2);
          $k++;
        }
        break;

        case 1: // '\001'
        if($k == 1)
        {
          $abyte2 = $abyte1;
          $this-&gt;rot13($abyte2);
          $k++;
        }
        break;

        case 2: // '\002'
        if($k == 0)
        {
          $i1 = 48 + (ord($abyte1[0]) + 10) % 10;
          $abyte1[0] = chr($i1);
          $k++;
        }
        break;

        case 3: // '\003'
        if($k == 3) $k++;
        break;

        case 5: // '\005'
        case 7: // '\007'
        case 10: // '\n'
        if($k &lt; 2) $abyte0 = $this-&gt;encrypt($abyte1, $abyte2);
        break;

        case 4: // '\004'
        case 6: // '\006'
        case 8: // '\b'
        case 9: // '\t'
        default:
          break;
      }
    }
    return $this-&gt;decodeDecrypt($s, $abyte0);
  }

  public function decryptToBinary($s)
  {
    if ($s == NULL)
    return NULL;
    else
    return $this-&gt;decodeDecrypt($s, $this-&gt;_fldif);
  }

}

?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.davebarnes.co.uk/blog/2010/02/hsbc-php-cpi-integration-without-installing-software/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
