-
Notifications
You must be signed in to change notification settings - Fork 1
/
HMAC.cs
35 lines (29 loc) · 1.11 KB
/
HMAC.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using NBomber.Http.CSharp;
namespace LoadTester
{
public static class HMAC
{
public static HttpRequestMessage WithHMACSignature(this HttpRequestMessage request, string keyId, string secret)
{
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
var signature = CalculateHash("SECRET", $"GET{request.RequestUri}{timestamp}");
return request
.WithHeader("X-Request-Timestamp", timestamp)
.WithHeader("Authorization", "HMAC-SHA256 id=Test;signature=" + signature);
}
private static string CalculateHash(string key, string input)
{
var textBytes = Encoding.UTF8.GetBytes(input);
var keyBytes = Encoding.UTF8.GetBytes(key);
using var hash = new HMACSHA256(keyBytes);
var hashBytes = hash.ComputeHash(textBytes);
return Convert.ToBase64String(hashBytes);
}
}
}