Skip to content

Instantly share code, notes, and snippets.

@MattHealy
Created June 30, 2021 11:54
Show Gist options
  • Save MattHealy/0b2da711cde4612b76e26eabfbd79ce3 to your computer and use it in GitHub Desktop.
Save MattHealy/0b2da711cde4612b76e26eabfbd79ce3 to your computer and use it in GitHub Desktop.
PHP HMAC-SHA512 example
<?php
// HMAC-SHA512 signing
function get_signature($event, $orderid, $listing_id) {
// Hashing algorithm to use
$algo = 'sha512';
// Shared key used for signing payloads
$secret_key = 'abcdef';
// Number of milliseconds since epoch
$timestamp = time() * 1000;
// Payload to be signed
$payload = "t=$timestamp&event=$event&orderid=$orderid&listing_id=$listing_id";
// Generate the signed payload
$signed_payload = hash_hmac(
$algo,
$payload,
$secret_key,
false,
);
return "t=$timestamp,$algo=$signed_payload";
}
$sig = get_signature("delivery", "12345", "L98765");
echo $sig . "\n";
// Include this value as a header in the request
// curl 'http://partner.se/diakrit/notify.php?event=change&orderid=XXXXXXX&listing_id=XXXXXXXX' -H 'X-DIAKRIT-SIGNATURE: sig'
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment