-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cs
More file actions
54 lines (53 loc) · 1.24 KB
/
Copy pathEngine.cs
File metadata and controls
54 lines (53 loc) · 1.24 KB
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
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
namespace TTMC.Tuya
{
internal class Engine
{
private static SHA256 sha = SHA256.Create();
public static string Encrypt(string message, string key)
{
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
return Convert.ToHexString(hmac.ComputeHash(Encoding.UTF8.GetBytes(message)));
}
public static string GetHash(string value)
{
return Convert.ToHexString(sha.ComputeHash(Encoding.UTF8.GetBytes(value))).ToLower();
}
public static JsonSerializerOptions jsonSerializerOptions
{
get
{
return new()
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
}
}
public static Base ToBase(string text)
{
if (text.StartsWith('{') && text.EndsWith('}'))
{
Base? item = JsonSerializer.Deserialize<Base>(text);
if (item != null)
{
if (item.success)
{
return item;
}
if (!string.IsNullOrEmpty(item.msg))
{
throw new(item.msg);
}
}
}
throw new(text);
}
}
}