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(string[] args)
- {
- Program program = new Program();
- var _request = new RestRequest();
- _request.Resource = "api/v2/balance";
- program.AddApiAuthentication(_request);
- Console.ReadLine();
- }
- public void AddApiAuthentication(RestRequest restRequest)
- {
- var nonce = DateTime.Now.Ticks;
- var signature = GetSignature(nonce, _apiKey, _apiSecret, _clientId);
- restRequest.AddParameter("X-Auth", _apiKey);
- restRequest.AddParameter("X-Auth-Signature", signature);
- restRequest.AddParameter("X-Auth-Nonce", nonce);
- var client = new RestClient();
- client.BaseUrl = new Uri("http://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