Link to home
Start Free TrialLog in
Avatar of Marcus Barnet
Marcus BarnetFlag for Italy

asked on

[PHP] Hash a XML file/string

Hi to all, I'm not a PHP expert and so I hope you can help me with my problem! :)
I'm trying to implement a payment method on my website and I'm having troubles with the transaction code.

I have a string where I loaded some XML content with tags and values.
I need to hash the whole XML content using MD5 algorithm, (Encoding Windows-1252), and then replace the content of the value of the first XML tag with the hashing value and saving it in a new string deleting all empty spaces.

I try to explain you better what I would like to do:

I have this string:

 $transactionXmlString = "<Transaction hash=\"" .$secretWord. "\">";
  $transactionXmlString .= "<RedirectionURL>" . $redirectionUrl . "</RedirectionURL>";
  $transactionXmlString .= "<status_url>" . $statusUrl . "</status_url>";
  $transactionXmlString .= "<FailedRedirectionURL>" . $failedRedirectionUrl . "</FailedRedirectionURL>";
  $transactionXmlString .= "<ProfileID>" . $profileID . "</ProfileID>";
  $transactionXmlString .= "<ActionType>" . $actionType . "</ActionType>";
  $transactionXmlString .= "<Value>" . $value . "</Value>";
  $transactionXmlString .= "<Curr>" . $currency . "</Curr>";
  $transactionXmlString .= "<Email>" . $email . "</Email>";
  $transactionXmlString .= "<Lang>" . $language . "</Lang>";
  $transactionXmlString .= "<Enc />";
  $transactionXmlString .= "<ORef>" . $orderReference . "</ORef>";
  $transactionXmlString .= "<ClientAcc>" . $clientAccount . "</ClientAcc>";
  $transactionXmlString .= "<MobileNo>" . $mobileNo . "</MobileNo>";
  $transactionXmlString .= "<Address>" . $address . "</Address>";
  $transactionXmlString .= "<Country>" . $country . "</Country>";
  $transactionXmlString .= "<RegName>" . $regName . "</RegName>";
  $transactionXmlString .= "<ListAllCards>" . $listAllCards . "</ListAllCards>";
  $transactionXmlString .= "<NewCard1Try />";
  $transactionXmlString .= "<CardRestrict />";
  $transactionXmlString .= "<UDF1>" . $udf1 . "</UDF1>";
  $transactionXmlString .= "<UDF2>" . $udf2 . "</UDF2>";
  $transactionXmlString .= "<UDF3>" . $udf3. "</UDF3>";
  $transactionXmlString .= "<PspID>" . $PspID . "</PspID>";
  $transactionXmlString .= "<HideSSLLogo />";
  $transactionXmlString .= "</Transaction>";

Open in new window


I need to hash (using the MD5 algorithm) the whole string and replace the value of "$secretWord" in <Transaction> tag with the hashing result, by updating the string content. For example, if the hashing result is: FFD93F16256349265FBAEBBAF268DD0E, then the new content for transactionXmlString would be:

  $transactionXmlString = "<Transaction hash=" [b]FFD93F16256349265FBAEBBAF268DD0E[/b]">";
  $transactionXmlString .= "<RedirectionURL>" . $redirectionUrl . "</RedirectionURL>";
  $transactionXmlString .= "<status_url>" . $statusUrl . "</status_url>";
  $transactionXmlString .= "<FailedRedirectionURL>" . $failedRedirectionUrl . "</FailedRedirectionURL>";
  $transactionXmlString .= "<ProfileID>" . $profileID . "</ProfileID>";
  $transactionXmlString .= "<ActionType>" . $actionType . "</ActionType>";
  $transactionXmlString .= "<Value>" . $value . "</Value>";
  $transactionXmlString .= "<Curr>" . $currency . "</Curr>";
  $transactionXmlString .= "<Email>" . $email . "</Email>";
  $transactionXmlString .= "<Lang>" . $language . "</Lang>";
  $transactionXmlString .= "<Enc />";
  $transactionXmlString .= "<ORef>" . $orderReference . "</ORef>";
  $transactionXmlString .= "<ClientAcc>" . $clientAccount . "</ClientAcc>";
  $transactionXmlString .= "<MobileNo>" . $mobileNo . "</MobileNo>";
  $transactionXmlString .= "<Address>" . $address . "</Address>";
  $transactionXmlString .= "<Country>" . $country . "</Country>";
  $transactionXmlString .= "<RegName>" . $regName . "</RegName>";
  $transactionXmlString .= "<ListAllCards>" . $listAllCards . "</ListAllCards>";
  $transactionXmlString .= "<NewCard1Try />";
  $transactionXmlString .= "<CardRestrict />";
  $transactionXmlString .= "<UDF1>" . $udf1 . "</UDF1>";
  $transactionXmlString .= "<UDF2>" . $udf2 . "</UDF2>";
  $transactionXmlString .= "<UDF3>" . $udf3. "</UDF3>";
  $transactionXmlString .= "<PspID>" . $PspID . "</PspID>";
  $transactionXmlString .= "<HideSSLLogo />";
  $transactionXmlString .= "</Transaction>";

Open in new window


Can you help me to solve this problem, please?

I tried to directly use the md5() function in this way:

$xml_hash = md5($transactionXmlString);
echo $xml_hash;

Open in new window

but it doesn't seem to work.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

OK, here is the central issue.  The md5() hash can only be built from data that does not change.  If you hash the entire XML document, then insert the md5() hash into the document, the document has changed, and the hash is no longer valid!

To get around that, create the XML document in multiple parts - one that contains the hash data, and another that contains the "live" data.  I'll post an example for you in a moment.
Avatar of Marcus Barnet

ASKER

Thank you, Ray, I will wait for it! :)
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial