protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload) { if (CanMakeAuthenticatedRequest(payload)) { request.AddHeader("KC-API-KEY", PublicApiKey.ToUnsecureString()); request.AddHeader("KC-API-TIMESTAMP", payload["nonce"].ToStringInvariant()); request.AddHeader("KC-API-PASSPHRASE", CryptoUtility.SHA256Sign(Passphrase.ToUnsecureString(), PrivateApiKey.ToUnsecureString(), true)); var endpoint = request.RequestUri.PathAndQuery; //For Gets, Deletes, no need to add the parameters in JSON format var message = ""; var sig = ""; if (request.Method == "GET" || request.Method == "DELETE") { //Request will be a querystring message = string.Format("{0}{1}{2}", payload["nonce"], request.Method, endpoint); sig = CryptoUtility.SHA256Sign(message, PrivateApiKey.ToUnsecureString(), true); } else if (request.Method == "POST") { message = string.Format("{0}{1}{2}{3}", payload["nonce"], request.Method, endpoint, CryptoUtility.GetJsonForPayload(payload, true)); sig = CryptoUtility.SHA256Sign(message, PrivateApiKey.ToUnsecureString(), true); } request.AddHeader("KC-API-KEY-VERSION", 2.ToStringInvariant()); request.AddHeader("KC-API-SIGN", sig); } if (request.Method == "POST") { string msg = CryptoUtility.GetJsonForPayload(payload, true); byte[] content = msg.ToBytesUTF8(); await request.WriteAllAsync(content, 0, content.Length); } }
protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload) { // Only Private APIs are POST and need Authorization if (CanMakeAuthenticatedRequest(payload) && request.Method == "POST") { string requestContentBase64String = string.Empty; string nonce = payload["nonce"] as string; payload.Remove("nonce"); string jsonContent = CryptoUtility.GetJsonForPayload(payload); if (!string.IsNullOrEmpty(jsonContent)) { using (MD5 md5 = MD5.Create()) { requestContentBase64String = Convert.ToBase64String(md5.ComputeHash(jsonContent.ToBytesUTF8())); } } string baseSig = string.Concat(PublicApiKey.ToUnsecureString(), request.Method, WebUtility.UrlEncode(request.RequestUri.AbsoluteUri).ToLowerInvariant(), nonce, requestContentBase64String); string signature = CryptoUtility.SHA256SignBase64(baseSig, Convert.FromBase64String(PrivateApiKey.ToUnsecureString())); request.AddHeader("authorization", string.Format("amx {0}:{1}:{2}", PublicApiKey.ToUnsecureString(), signature, nonce)); // Cryptopia is very picky on how the payload is passed. There might be a better way to do this, but this works... byte[] content = jsonContent.ToBytesUTF8(); await request.WriteAllAsync(content, 0, content.Length); } }
protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload) { // Only Private APIs are POST and need Authorization if (CanMakeAuthenticatedRequest(payload) && request.Method == "POST") { var requestContentBase64String = string.Empty; var nonce = payload["nonce"] as string; payload.Remove("nonce"); var jsonContent = payload.GetJsonForPayload(); if (!string.IsNullOrEmpty(jsonContent)) { using (var md5 = MD5.Create()) { requestContentBase64String = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(jsonContent))); } } var baseSig = string.Concat(PublicApiKey.ToUnsecureString(), request.Method, Uri.EscapeDataString(request.RequestUri.AbsoluteUri).ToLower(), nonce, requestContentBase64String); var signature = CryptoUtility.SHA256SignBase64(baseSig, Convert.FromBase64String(PrivateApiKey.ToUnsecureString())); request.AddHeader("authorization", $"amx {PublicApiKey.ToUnsecureString()}:{signature}:{nonce}"); var content = jsonContent.ToBytesUTF8(); await request.WriteAllAsync(content, 0, content.Length); } }
protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload) { // Only Private APIs are POST and need Authorization if (CanMakeAuthenticatedRequest(payload) && request.Method == "POST") { var msg = CryptoUtility.GetFormForPayload(payload); var sig = CryptoUtility.SHA512Sign(msg, PrivateApiKey.ToUnsecureString()); request.AddHeader("Key", PublicApiKey.ToUnsecureString()); request.AddHeader("Sign", sig.ToLowerInvariant()); byte[] content = msg.ToBytesUTF8(); await request.WriteAllAsync(content, 0, content.Length); } }
/// <summary> /// Write a form to a request /// </summary> /// <param name="request">Request</param> /// <param name="form">Form to write</param> public static async Task WriteToRequestAsync(this IHttpWebRequest request, string form) { if (string.IsNullOrEmpty(form) && request.Method != "GET") { request.AddHeader("content-length", "0"); } else { byte[] bytes = form.ToBytesUTF8(); request.AddHeader("content-length", bytes.Length.ToStringInvariant()); await request.WriteAllAsync(bytes, 0, bytes.Length); } }
protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload) { if (CanMakeAuthenticatedRequest(payload)) { // ensure nonce is the last parameter string nonce = payload["nonce"].ToStringInvariant(); payload.Remove("nonce"); var msg = CryptoUtility.GetFormForPayload(payload) + "&nonce=" + nonce; var sig = CryptoUtility.SHA512Sign(msg, CryptoUtility.ToBytesUTF8(PrivateApiKey)).ToLowerInvariant(); request.AddHeader("Sign", sig); request.AddHeader("Key", PublicApiKey.ToUnsecureString()); byte[] content = msg.ToBytesUTF8(); await request.WriteAllAsync(content, 0, content.Length); } }
protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload) { // Only Private APIs are POST and need Authorization if (CanMakeAuthenticatedRequest(payload) && request.Method == "POST") { var signature = string.Empty; payload.Add("key", PublicApiKey.ToUnsecureString()); var jsonContent = payload.GetJsonForPayload(); if (!string.IsNullOrEmpty(jsonContent)) { signature = CryptoUtility.SHA512Sign(jsonContent, PrivateApiKey.ToUnsecureBytesUTF8()).ToLowerInvariant(); } request.AddHeader("Hash", signature); var content = jsonContent.ToBytesUTF8(); await request.WriteAllAsync(content, 0, content.Length); } }
protected override async Task ProcessRequestAsync(IHttpWebRequest request, Dictionary <string, object> payload) { if (CanMakeAuthenticatedRequest(payload)) { request.AddHeader("KC-API-KEY", PublicApiKey.ToUnsecureString()); request.AddHeader("KC-API-NONCE", payload["nonce"].ToStringInvariant()); var endpoint = request.RequestUri.AbsolutePath; var message = string.Format("{0}/{1}/{2}", endpoint, payload["nonce"], CryptoUtility.GetFormForPayload(payload, false)); var sig = CryptoUtility.SHA256Sign(Convert.ToBase64String(message.ToBytesUTF8()), PrivateApiKey.ToUnsecureString()); request.AddHeader("KC-API-SIGNATURE", sig); if (request.Method == "POST") { string msg = CryptoUtility.GetFormForPayload(payload, false); byte[] content = msg.ToBytesUTF8(); await request.WriteAllAsync(content, 0, content.Length); } } }