-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUrlSigner.php
40 lines (30 loc) · 1.11 KB
/
UrlSigner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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)
{
// parse the url
$url = parse_url($myUrlToSign);
$urlPartToSign = $url['path'] . "?" . $url['query'];
// 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("sha1",$urlPartToSign, $decodedKey, true);
$encodedSignature = encodeBase64UrlSafe($signature);
return $myUrlToSign."&signature=".$encodedSignature;
}
echo signUrl("http://maps.google.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID", 'vNIXE0xscrmjlyV-12Nj_BvUPaw=');
?>