45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace WebSocket.Clients
|
|
{
|
|
public class Auth
|
|
{
|
|
public string Key { get; set; }
|
|
public string Signature { get; set; }
|
|
public long Timestamp { get; set; }
|
|
|
|
public void CreateSignature(string secret, DateTime? now = null)
|
|
{
|
|
Timestamp = GetTimestamp(now ?? DateTime.UtcNow);
|
|
|
|
Signature = ComputeHash(secret, Timestamp + Key);
|
|
}
|
|
private static readonly long _InitialJavaScriptDateTicks = 621355968000000000;
|
|
private long GetTimestamp(DateTime time)
|
|
{
|
|
long javaScriptTicks = (time.Ticks - _InitialJavaScriptDateTicks) / 10000 / 1000;
|
|
return javaScriptTicks;
|
|
}
|
|
private static string ComputeHash(string apiKey, string message)
|
|
{
|
|
var key = Encoding.UTF8.GetBytes(apiKey);
|
|
string hashString;
|
|
using (var hmac = new System.Security.Cryptography.HMACSHA256(key))
|
|
{
|
|
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(message));
|
|
hashString = String.Concat(hash.Select(x => x.ToString("x2")));
|
|
}
|
|
|
|
return hashString;
|
|
}
|
|
}
|
|
public class AuthRequest
|
|
{
|
|
public string E { get { return "auth"; } }
|
|
public Auth Auth { get; set; }
|
|
}
|
|
}
|