-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAlibabaCloudMqttHelper.cs
57 lines (41 loc) · 2.01 KB
/
AlibabaCloudMqttHelper.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
53
54
55
56
57
using System;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApp
{
public class AlibabaCloudMqttHelper
{
public static MqttClientcredentials CreateClientCredentials(string productKey, string deviceName, string deviceSecret)
{
productKey = productKey ?? throw new ArgumentNullException(nameof(productKey));
deviceName = deviceName ?? throw new ArgumentNullException(nameof(deviceName));
deviceSecret = deviceSecret ?? throw new ArgumentNullException(nameof(deviceSecret));
string userName = $"{deviceName}&{productKey}";
long timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
string plainPwd = $"clientId{productKey}.{deviceName}{nameof(deviceName)}{deviceName}{nameof(productKey)}{productKey}{nameof(timestamp)}{timestamp}";
string password = HmacSha256(plainPwd, deviceSecret);
string clientId = $"{productKey}.{deviceName}|securemode=2,signmethod=hmacsha256,{nameof(timestamp)}={timestamp}|";
return new MqttClientcredentials(userName, password, clientId);
}
public class MqttClientcredentials
{
public string UserName { get; }
public string Password { get; }
public string ClientId { get; }
public MqttClientcredentials(string userName, string password, string clientId)
{
UserName = userName;
Password = password;
ClientId = clientId;
}
}
public static string HmacSha256(string plainText, string key)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
HMACSHA256 hmac = new HMACSHA256(keyBytes);
byte[] sign = hmac.ComputeHash(plainTextBytes);
return BitConverter.ToString(sign).Replace("-", string.Empty);
}
}
}