Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using RestSharp;
- using System;
- using System.Security.Cryptography;
- using System.Text;
- namespace ConsoleApp5
- {
- class Program
- {
- private readonly String _clientId = "xxx";
- private readonly String _apiKey = "xxx";
- private readonly String _apiSecret = "xxx";
- static void Main()
- {
- Program program = new Program();
- RestRequest request = new RestRequest("/api/v2/balance/", Method.POST);
- program.AddApiAuthentication(request);
- Console.ReadLine();
- }
- public void AddApiAuthentication(RestRequest restRequest)
- {
- var nonce = DateTime.Now.Ticks;
- var signature = GetSignature(nonce, _apiKey, _apiSecret, _clientId);
- long time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
- string version = "v2";
- string contentType = "application/x-www-form-urlencoded";
- restRequest.AddParameter("X-Auth", _apiKey);
- restRequest.AddParameter("X-Auth-Signature", signature);
- restRequest.AddParameter("X-Auth-Nonce", nonce);
- restRequest.AddParameter("X-Auth-Timestamp", time);
- restRequest.AddParameter("X-Auth-Version", version);
- restRequest.AddParameter("Content-Type", contentType);
- RestClient client = new RestClient
- {
- BaseUrl = new Uri("https://www.bitstamp.net/")
- };
- IRestResponse response = client.Execute(restRequest);
- Console.WriteLine(response.Content);
- }
- private string GetSignature(long nonce, string key, string secret, string clientId)
- {
- string msg = string.Format("{0}{1}{2}", nonce,
- clientId,
- key);
- return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();
- }
- public static byte[] SignHMACSHA256(String key, byte[] data)
- {
- HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
- return hashMaker.ComputeHash(data);
- }
- public static byte[] StringToByteArray(string str)
- {
- return System.Text.Encoding.ASCII.GetBytes(str);
- }
- public static string ByteArrayToString(byte[] hash)
- {
- return BitConverter.ToString(hash).Replace("-", "").ToLower();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement