Question

I'm writing a PHP function that takes an array of $_GET parameters and spits out an SHA signature of these (salted with a secret) to verify authenticity.

Currently it looks like this:

private function generateShasign($fields) {
    unset($fields['SHASIGN']);
    ksort($fields, SORT_NATURAL | SORT_FLAG_CASE);
    $phrase = "";
    foreach($fields as $key => $field){
        // if(!empty($field) && $field != '0'){
            $phrase .= strtoupper($key) . '=' . $field . 'SECRETSALT';
        // }
    }
    return strtoupper(sha1($phrase));
}

It unsets the SHA-SIGNATURE parameter from the request, puts them in 'KEY=value.SECRETSALT" format and sorts them (using ksort())

Now, this works PERFECTLY on my local environment. The returned SHA equals the SHA var in the get request and everything goes fine.

On a remote testing server however, it does not work. Ksort does not seem to be sorting at all. AND it does not throw an error.

A quick print_r($phrase) returns (on the faulty remote server):

SHA SIGN: ORDERID=ge9xBpZjXSECRETSALTCURRENCY=EURSECRETSALTAMOUNT=50SECRETSALTPM=CreditCardSECRETSALTACCEPTANCE=test123SECRETSALTSTATUS=5SECRETSALTCARDNO=XXXXXXXXXXXX1111SECRETSALTED=0214SECRETSALTCN=MATTHIASSECRETSALTTRXDATE=02/22/14SECRETSALTPAYID=28345877SECRETSALTNCERROR=0SECRETSALTBRAND=VISASECRETSALTIP=84.198.21.23SECRETSALT7F0B539A1DECC55E57860DEB9F7B3A301E1960AD

and on the proper working environment:

ACCEPTANCE=test123SECRETSALTAMOUNT=50SECRETSALTBRAND=VISASECRETSALTCARDNO=XXXXXXXXXXXX1111SECRETSALTCN=MATTHIASSECRETSALTCURRENCY=EURSECRETSALTED=0214SECRETSALTIP=84.198.21.23SECRETSALTNCERROR=0SECRETSALTORDERID=iXAVBM1SBSECRETSALTPAYID=28345882SECRETSALTPM=CreditCardSECRETSALTSTATUS=5SECRETSALTTRXDATE=02/22/14SECRETSALT

Obviously, ksort() is not sorting anything on the remote environment. Hence why the order of the parameters in the to-be-sha-encoded phrase is exactly the same as the order in the GET request.

Why ksort() is not functioning AND not throwing an error is puzzling me bigtime.

Locally i'm running PHP/5.4.22 (Apache). And remotely i -believe- PHP 5.3 (NGINX)

UPDATE:

ksort returns false. Can not figure out why it's failing though.

Any help is greatly appreciated!

Was it helpful?

Solution

Here we go:

http://php.net/ChangeLog-5.php#5.4.0

Added support for SORT_NATURAL and SORT_FLAG_CASE in array sort functions (sort, rsort, ksort, krsort, asort, arsort and array_multisort).

OTHER TIPS

The SORT_NATURAL and SORT_FLAG_CASE were not working on the remote environment. For watherver reason, i don't know. SORT_STRING however was working. So i worked around my issue by converting the keys to uppercase first, and then sorting with SORT_STRING.

Doesn't solve the issue, but it's a viable workaround i guess.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top