-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignature.cs
184 lines (164 loc) · 5.82 KB
/
Signature.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
namespace Utility.Encryption
{
public class Signature
{
private long epoch { get; set; }
private eCryptographyType cryptographyType { get; set; }
private string signature { get; set; }
private string secret { get; set; }
public Signature() : base()
{
cryptographyType = eCryptographyType.HMACSHA256;
SetEpoch();
signature = string.Empty;
}
public Signature(string secret, string value2Sign = "", eCryptographyType cryptographyType = eCryptographyType.HMACSHA256) : base()
{
SetEpoch();
SetSecret(secret);
SetCryptographyType(cryptographyType);
CreateSignature(value2Sign);
}
public Signature(string secret, long epoch, string value2Sign = "", eCryptographyType cryptographyType = eCryptographyType.HMACSHA256) : base()
{
SetEpoch(epoch);
SetSecret(secret);
SetCryptographyType(cryptographyType);
CreateSignature(value2Sign);
}
public static Signature Create(string secret, string value2Sign = "", eCryptographyType cryptographyType = eCryptographyType.HMACSHA256)
{
return new Signature(secret, value2Sign, cryptographyType);
}
public static Signature Create(string secret, long epoch, string value2Sign = "", eCryptographyType cryptographyType = eCryptographyType.HMACSHA256)
{
return new Signature(secret, epoch, value2Sign, cryptographyType);
}
public void SetEpoch()
{
epoch = Extensions.ToUnixTimeSeconds(DateTime.UtcNow);
}
public void SetEpoch(long value)
{
epoch = value;
}
public void SetSecret(string value)
{
secret = value;
}
#region "SetCryptographyType"
public void SetCryptographyType(object value)
{
SetCryptographyType(value.ToString());
}
public void SetCryptographyType(int value)
{
SetCryptographyType(value.ToString());
}
#if NET40
public void SetCryptographyType(string value)
{
switch (value.ToUpper())
{
case "0":
case "HMACMD5":
cryptographyType = eCryptographyType.HMACMD5;
break;
case "1":
case "HMACRIPEMD160":
cryptographyType = eCryptographyType.HMACRIPEMD160;
break;
case "2":
case "HMACSHA1":
cryptographyType = eCryptographyType.HMACSHA1;
break;
case "4":
case "HMACSHA384":
cryptographyType = eCryptographyType.HMACSHA384;
break;
default:
cryptographyType = eCryptographyType.HMACSHA256;
break;
}
}
#elif NET5_0_OR_GREATER
public void SetCryptographyType(string value)
{
cryptographyType = value.ToUpper() switch
{
"0" or "HMACMD5" => eCryptographyType.HMACMD5,
"2" or "HMACSHA1" => eCryptographyType.HMACSHA1,
"4" or "HMACSHA384" => eCryptographyType.HMACSHA384,
"5" or "HMACSHA512" => eCryptographyType.HMACSHA512,
_ => eCryptographyType.HMACSHA256,
};
}
#else
public void SetCryptographyType(string value)
{
switch (value.ToUpper())
{
case "0":
case "HMACMD5":
cryptographyType = eCryptographyType.HMACMD5;
break;
case "1":
case "HMACRIPEMD160":
cryptographyType = eCryptographyType.HMACRIPEMD160;
break;
case "2":
case "HMACSHA1":
cryptographyType = eCryptographyType.HMACSHA1;
break;
case "4":
case "HMACSHA384":
cryptographyType = eCryptographyType.HMACSHA384;
break;
case "5":
case "HMACSHA512":
cryptographyType = eCryptographyType.HMACSHA512;
break;
default:
cryptographyType = eCryptographyType.HMACSHA256;
break;
}
}
#endif
public void SetCryptographyType(eCryptographyType value)
{
cryptographyType = value;
}
#endregion
public void CreateSignature(string secret, string value2Sign = "", eCryptographyType cryptographyType = eCryptographyType.HMACSHA256)
{
SetSecret(secret);
SetCryptographyType(cryptographyType);
CreateSignature(value2Sign);
}
public void CreateSignature(string value = "")
{
if (!(string.IsNullOrEmpty(secret)))
{
string string2Hash = string.IsNullOrEmpty(value) ? string.Format("{0}|{1}", DateTimeOffset.UtcNow.Date, epoch) : string.Format("{0}|{1}", value, epoch);
signature = SignatureValidation.HashData(string2Hash, secret, cryptographyType);
}
else
throw new Exception("Secret is missing and you must run SetSecret(\"value\") before creating the signature!");
}
public string GetSignature()
{
if (!(string.IsNullOrEmpty(signature)))
return signature;
else
{
CreateSignature();
return signature;
}
}
public long GetEpoch()
{
return epoch;
}
}
}