Skip to content

Instantly share code, notes, and snippets.

@apih
Last active November 6, 2022 16:33
Show Gist options
  • Save apih/ad0872fb0d3aaaa162ff8b9918509cf0 to your computer and use it in GitHub Desktop.
Save apih/ad0872fb0d3aaaa162ff8b9918509cf0 to your computer and use it in GitHub Desktop.
X Signature verification sample
<?php
function buildSourceString($data, $prefix = '')
{
uksort($data, function($a, $b) {
$a_len = strlen($a);
$b_len = strlen($b);
$result = strncasecmp($a, $b, min($a_len, $b_len));
if ($result === 0) {
$result = $b_len - $a_len;
}
return $result;
});
$processed = [];
foreach ($data as $key => $value) {
if ($key === 'x_signature') continue;
if (is_array($value)) {
$processed[] = buildSourceString($value, $key);
} else {
$processed[] = $prefix . $key . $value;
}
}
return implode('|', $processed);
}
$source_string = buildSourceString($_POST);
$xsignature_key = 'blablabla'; // Get from your stored settings
$xsignature = $_POST['x_signature'];
$equal = hash_equals(hash_hmac('sha256', $source_string, $xsignature_key), $xsignature);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment