-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyAuthentication.cs
57 lines (49 loc) · 1.57 KB
/
MyAuthentication.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.Threading;
using System.Threading.Tasks;
using DotPulsar.Abstractions;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// Token-based authentication implementation.
/// </summary>
public class MyAuthentication : IAuthentication
{
private readonly string _authData;
public MyAuthentication(string accessId, string accessKey)
{
_authData = "{\"username\":\"" + accessId +"\", \"password\":\"" + GenPwd(accessId, accessKey) + "\"}";
}
/// <summary>
/// The authentication method name
/// </summary>
public string AuthenticationMethodName => "auth1";
/// <summary>
/// Get the authentication data
/// </summary>
public async ValueTask<byte[]> GetAuthenticationData(CancellationToken cancellationToken)
{
await Task.Delay(1, cancellationToken);
return System.Text.Encoding.UTF8.GetBytes(_authData);
}
// pwd
private static string GenPwd(string accessId, string accessKey){
string md5HexKey = Md5(accessKey);
string mixStr = accessId + md5HexKey;
String md5MixStr = Md5(mixStr);
return md5MixStr.Substring(8,16);
}
// md5
private static string Md5(string md5Str) {
using (MD5 md5 = MD5.Create())
{
byte[] dataHash = md5.ComputeHash(Encoding.UTF8.GetBytes(md5Str));
StringBuilder sb = new StringBuilder();
foreach (byte b in dataHash)
{
sb.Append(b.ToString("x2").ToLower());
}
return sb.ToString();
}
}
}