Skip to content

Instantly share code, notes, and snippets.

@brainded
Created July 20, 2017 21:26
Show Gist options
  • Save brainded/00ae937f9507fefb5a1f9ab8517695a2 to your computer and use it in GitHub Desktop.
Save brainded/00ae937f9507fefb5a1f9ab8517695a2 to your computer and use it in GitHub Desktop.
Virtuous Hmac in PHP
<?php
function getHmacToken($applicationKey, $requestHttpMethod, $requestUrl)
{
$privateKey = "VIRTUOUS_API_KEY";
$nonce = uniqid();
$timestamp = (string)time();
$toBeHashed = strtolower($applicationKey . urlencode($requestUrl) . $requestHttpMethod . $nonce . $timestamp);
$hmac = getHashedUrl($toBeHashed, $privateKey);
$authorization = "Hmac " . $applicationKey . ":" . $hmac . ":" . $nonce . ":" . $timestamp;
return $authorization;
}
function getHashedUrl($toBeHashed, $privateKey)
{
$apiKey = base64_decode($privateKey);
$hmac = hash_hmac(
"SHA256",
$toBeHashed, // Hash the combined string
$apiKey, // Using private dev key to sign it
true
);
$hmac_b64 = base64_encode($hmac);
return $hmac_b64;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment