예제 #1
0
        /// <remarks>
        /// Based on the C# sample from: https://developers.google.com/maps/documentation/business/webservices
        /// </remarks>
        internal Uri Sign(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }
            if (ClientId == null)
            {
                throw new ArgumentNullException("userID");
            }
            if (string.IsNullOrWhiteSpace(SigningKey))
            {
                throw new ArgumentException("Invalid signing key.");
            }
            if (!ClientId.StartsWith("gme-"))
            {
                throw new ArgumentException("A user ID must start with 'gme-'.");
            }

            var urlSegmentToSign = uri.LocalPath + uri.Query + "&client=" + ClientId;

            if (!string.IsNullOrWhiteSpace(Channel))
            {
                urlSegmentToSign += "&channel=" + Channel;
            }

            byte[] privateKey = FromBase64UrlString(SigningKey);
            byte[] signature;

            using (var algorithm = new HMACSHA1(privateKey))
            {
                signature = algorithm.ComputeHash(Encoding.ASCII.GetBytes(urlSegmentToSign));
            }

            return(new Uri(uri.Scheme + "://" + uri.Host + urlSegmentToSign + "&signature=" + ToBase64UrlString(signature)));
        }