-
Notifications
You must be signed in to change notification settings - Fork 21
/
AuthHelper.cs
52 lines (41 loc) · 1.85 KB
/
AuthHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace TestAPIClient {
public class AuthHelper {
private readonly string apiKey;
private readonly string secret;
public AuthHelper(string apiKey, string secret) {
this.apiKey = apiKey;
this.secret = secret;
}
public Dictionary<string, string> GetHeaders(string nonce) {
return GetHeaders(nonce, DateTime.UtcNow, false);
}
public Dictionary<string, string> GetHeaders(string nonce, DateTime dateTime, bool asRetry) {
var signature = GenerateSignature(nonce, dateTime);
var headers = new Dictionary<string, string>();
headers.Add("Authorization", GenerateAuthHeaderContent(apiKey, signature));
headers.Add("Date", dateTime.ToString("R"));
headers.Add("x-mod-nonce", nonce);
headers.Add("x-mod-retry", asRetry.ToString().ToLower());
return headers;
}
private String GenerateAuthHeaderContent(String apiKey, String signature) {
return $"Signature keyId=\"{apiKey}\",algorithm=\"hmac-sha1\",headers=\"date x-mod-nonce\",signature=\"{signature}\"";
}
private string GenerateSignature(string nonce, DateTime dateTime) {
return HashAndEncode($"date: {dateTime:R}\nx-mod-nonce: {nonce}", secret);
}
private static string HashAndEncode(string message, string secret) {
var ascii = Encoding.ASCII;
HMACSHA1 hmac = new HMACSHA1(ascii.GetBytes(secret));
hmac.Initialize();
byte[] messageBuffer = ascii.GetBytes(message);
byte[] hash = hmac.ComputeHash(messageBuffer);
return WebUtility.UrlEncode(Convert.ToBase64String(hash));
}
}
}