-
Notifications
You must be signed in to change notification settings - Fork 48
/
urlSigner.vb
51 lines (39 loc) · 2.11 KB
/
urlSigner.vb
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
41
42
43
44
45
46
47
48
49
50
51
'Based on C# example code available here:
'http://googlemaps.github.io/js-v2-samples/urlsigning/UrlSigner.cs
'Converted to VB.NET by Danny Moules
'Licensed under Creative Commons Attribution-Share Alike 2.0 UK: England & Wales
'(http://creativecommons.org/licenses/by-sa/2.0/uk/)
Option Explicit On
Option Strict On
Imports System
Imports System.Security.Cryptography
Imports System.Text
Namespace SignUrl
Public Module GoogleUrlSigner
Public Function Sign(ByVal url As String, ByVal keyString As String) As String
Dim encoding As ASCIIEncoding = New ASCIIEncoding()
'URL-safe decoding
Dim privateKeyBytes As Byte() = Convert.FromBase64String(keyString.Replace("-", "+").Replace("_", "/"))
Dim objURI As Uri = New Uri(url)
Dim encodedPathAndQueryBytes As Byte() = encoding.GetBytes(objURI.LocalPath & objURI.Query)
'compute the hash
Dim algorithm As HMACSHA1 = New HMACSHA1(privateKeyBytes)
Dim hash As Byte() = algorithm.ComputeHash(encodedPathAndQueryBytes)
'convert the bytes to string and make url-safe by replacing '+' and '/' characters
Dim signature As String = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_")
'Add the signature to the existing URI.
Return objURI.Scheme & "://" & objURI.Host & objURI.LocalPath & objURI.Query & "&signature=" & signature
End Function
End Module
Public Module Program
Sub Main()
Const keyString As String = "vNIXE0xscrmjlyV-12Nj_BvUPaw="
'The URL shown here is a static URL which should be already
'URL-encoded. In practice, you will likely have code
'which assembles your URL from user or web service input
'and plugs those values into its parameters.
Dim urlString As String = "http://maps.google.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID"
Console.WriteLine(GoogleUrlSigner.Sign(urlString, keyString))
End Sub
End Module
End Namespace