Search code examples
phpshsha

how to match sha256 output


I have a device using a compiled binary to generate a sha256 key, which needs to be authenticated on the server which uses php (7.3) and with same data the resulting keys are different while they should match.

The binary is same as the .sh file posted here.

What am I missing?

Here is the .sh code

#!/bin/sh

SEC="xyxyxyxyxyxyxyxyxyxyxyxyxyxyxy"
if [ -z $1 ]
then
    PROD=$(cat prod.txt)
else
    PROD=$1
    EXPIRE=$2
fi
SDATA="${PROD}\$${SEC}\$${EXPIRE}"
KEY=$(echo -n ${SDATA} | openssl sha256 -binary | base64)

echo "ABC TXYZ Gen"
echo "Code: ${PROD}"
[ ! -z "${EXPIRE}" ] && echo "Expire date : ${EXPIRE}"
echo "Activate code:"
echo ${KEY%%=} | tee activate.txt

And this is the php

public function generateActivate($params = [])
{
    if (!isset($params['Mod'])) {
        return false;
    }
    if (!isset($params['Prod'])) {
        return false;
    }
    if (!isset($params['Vers'])) {
        return false;
    }

    $sec="xyxyxyxyxyxyxyxyxyxyxyxyxyxyxy
    $data = $params['Prod'].$sec;
    $params['Activate'] = base64_encode(hash('sha256', $data, true));

Unfortunatelly I can't run a compiled binary on the server as it asks for glib2.28 version not available on the server (Centos 7).


Solution

  • Your shell code has

    SDATA="${PROD}\$${SEC}\$${EXPIRE}"
    

    whereas your php has:

    $data = $params['Prod'].$sec;
    

    Either remove the dollars and the expiry from the shell code:

    SDATA="${PROD}${SEC}"
    

    or add them to the php:

    $data = $params['Prod'] . '$' . $sec . '$' . $params['Expire'];