public HMACAuthrizationDelegatingHandler(IHashKeys hashKeys)
        {
            _hashKeys             = hashKeys;
            _signer               = new Signer();
            _authHeaderSerializer = new AuthorisationHeaderSerializer();

            InnerHandler = new HttpClientHandler();
        }
Beispiel #2
0
        public string Sign(string requestUrl, string httpMethod, byte[] requestBody, IAuthorizationHeader authHeader, IHashKeys hashKeys)
        {
            var encodedContent = string.Empty;

            var hash = ComputeHash(hashKeys.HashAlgorithm, requestBody);

            if (hash != null)
            {
                encodedContent = Convert.ToBase64String(hash);
            }

            var data          = $"{hashKeys.APPId}{httpMethod}{requestUrl}{authHeader.Epoch}{authHeader.Nonce}{encodedContent}";
            var calcSignature = Encoding.UTF8.GetBytes(data);

            using (var hmac = HMAC.Create(hashKeys.HmacAlgorithm))
            {
                hmac.Key = Convert.FromBase64String(hashKeys.ApiKey);
                var signatureBytes = hmac.ComputeHash(calcSignature);
                return(Convert.ToBase64String(signatureBytes));
            }
        }
Beispiel #3
0
        public async Task <string> SignAsync(HttpRequestMessage request, IAuthorizationHeader authHeader, IHashKeys hashKeys)
        {
            var requestContentBase64String = string.Empty;

#if NETSTANDARD2_0
            var requestUrl = System.Web.HttpUtility.UrlEncode(request.RequestUri.AbsoluteUri.ToLower());
#else
            var requestUrl = System.Uri.EscapeDataString(request.RequestUri.AbsoluteUri.ToLower());
#endif
            var    httpMethod = request.Method.Method;
            byte[] content    = null;

            if (request.Content != null)
            {
                content = await request.Content.ReadAsByteArrayAsync();
            }

            return(Sign(requestUrl, httpMethod, content, authHeader, hashKeys));
        }