Created
July 20, 2017 21:26
-
-
Save brainded/00ae937f9507fefb5a1f9ab8517695a2 to your computer and use it in GitHub Desktop.
Virtuous Hmac in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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