function createAllParams(string $id, string $token, string $secret, int $timestamp, string $identifier, string $type, array $parameters, string $actionId): string
{
// in alphabetical order
$fields['accesstoken'] = $token;
$fields['actionid'] = $actionId;
$fields['identifier'] = $identifier;
$fields['resourceid'] = $id;
$fields['secret'] = $secret;
$fields['timestamp'] = $timestamp;
$fields['type'] = $type;
// please use an ordered data structure (such as a linked list) here since ksort is not recursive.
ksort($parameters);
// PHP defaults of json_encode() are being used here
$parametersBundled = json_encode($parameters);
$fieldsBundled = implode(',', $fields);
$allParams = $parametersBundled.','.$fieldsBundled;
return $allParams;
}
function createHmac(string $secret, string $allParams): string
{
// PHP md5 without second parameter (as used here) does return
// the hexadecimal representation __as lowercase string__, not binary!
return md5($secret.md5($allParams));
}
$token = '4a9c9t9c9d34dac46d';
$secret = '9348938afdef0293838475934875983475';
$timestamp = '1566393693'; // `date +%s` on shell
$testParameters = [
'testParam-string' => "teststring 你好 äöüß \" ' & \r\n",
'testParam-null' => null,
'testParam-float' => ".12387548574875847584593475", //floats have to be given as strings
'testParam-int' => 3,
'testParam-array-assoc' => [
'key8' => 'firstEntry',
'key1' => 'lastEntry',
],
'testParam-array-numerical' => ['hello', 'world'],
];
echo "\$testParameters: \n
".var_export($testParameters, true)."\n\n";
// output: array ( 'testParam-string' => 'teststring 你好 äöüß " \' & ', 'testParam-null' => NULL, 'testParam-float' => 0.1238754857487584792874457662037457339465618133544921875, 'testParam-int' => 3, 'testParam-array-assoc' => array ( 'key8' => 'firstEntry', 'key1' => 'lastEntry', ), 'testParam-array-numerical' => array ( 0 => 'hello', 1 => 'world', ), )
echo "
\$allParams:\n
";
$allParams = createAllParams('testID', $token, $secret, $timestamp, 'testIdentifier', 'testResourceType', $testParameters, 'testActionId');
echo "$allParams\n\n";
/*
output (one line):
{"testParam-array-assoc":{"key8":"firstEntry","key1":"lastEntry"},"testParam-array-numerical":["hello","world"],"testParam-float":0.12387548574876,"testParam-int":3,"testParam-null":null,"testParam-string":"teststring \u4f60\u597d <\/tag> \u00e4\u00f6\u00fc\u00df \" ' & \r\n"},4a9c9t9c9d34dac46d,testActionId,testIdentifier,testID,9348938afdef0293838475934875983475,1566393693,testResourceType
*/
echo "
HMAC:\n";
echo createHmac($secret, $allParams)."\n";
// output: bacf778f99c99a3e78745ad72373e337