Skip to content

Instantly share code, notes, and snippets.

@gertig
Last active January 4, 2016 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gertig/8651701 to your computer and use it in GitHub Desktop.
Save gertig/8651701 to your computer and use it in GitHub Desktop.
<?php
// Encode a string to URL-safe base64
function encodeBase64UrlSafe($value)
{
return str_replace(array('+', '/'), array('-', '_'),
base64_encode($value));
}
// Decode a string from URL-safe base64
function decodeBase64UrlSafe($value)
{
return base64_decode(str_replace(array('-', '_'), array('+', '/'),
$value));
}
// Sign a URL with a given crypto key
// Note that this URL must be properly URL-encoded
function signUrl($myUrlToSign, $privateKey)
{ // Decode the private key into its binary format
$decodedKey = decodeBase64UrlSafe($privateKey);
// Create a signature using the private key and the URL-encoded
// string using HMAC SHA1. This signature will be binary.
$signature = hash_hmac("MD5",$myUrlToSign, $decodedKey, true);
$encodedSignature = encodeBase64UrlSafe($signature);
return $encodedSignature;
}
$key = 'Encoding-Key';
$url = 'http://fake-domain.com/GetChapterEventList/?clientId=12345&chapterId=55';
$signature = signUrl($url, $key);
$opts = array('http'=>array('header'=>"Authorization: " . $signature));
$context = stream_context_create($opts);
$file = file_get_contents($url, false, $context);
echo $file
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment