Geocoding.Google.BusinessKey.GenerateSignature C# (CSharp) Method

GenerateSignature() public method

public GenerateSignature ( string url ) : string
url string
return string
		public string GenerateSignature(string url)
		{
			var encoding = new ASCIIEncoding();
			var uri = new Uri(url);

			// converting key to bytes will throw an exception, need to replace '-' and '_' characters first.
			string usablePrivateKey = SigningKey.Replace("-", "+").Replace("_", "/");
			byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);

			byte[] encodedPathAndQueryBytes = encoding.GetBytes(uri.LocalPath + uri.Query);

			// compute the hash
			var algorithm = new HMACSHA1(privateKeyBytes);
			byte[] hash = algorithm.ComputeHash(encodedPathAndQueryBytes);

			// convert the bytes to string and make url-safe by replacing '+' and '/' characters
			string signature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");

			// Add the signature to the existing URI.
			return uri.Scheme + "://" + uri.Host + uri.LocalPath + uri.Query + "&signature=" + signature;
		}

Usage Example

		public void Should_generate_signature_from_url()
		{
			var key = new BusinessKey("clientID", "vNIXE0xscrmjlyV-12Nj_BvUPaw=");

			string signedUrl = key.GenerateSignature("http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID");

			Assert.NotNull(signedUrl);
			Assert.Equal("http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA=", signedUrl);
		}
All Usage Examples Of Geocoding.Google.BusinessKey::GenerateSignature