/
util.cs
242 lines (218 loc) · 6.56 KB
/
util.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using Microsoft.Win32;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Media;
using System.Xml;
namespace SpeedportHybridControl.Implementations
{
public static class util
{
public static byte[] HexToByte(string hex)
{
return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
}
/**
* get sha256
*
* @param string password
* @return string
*/
public static string sha256(this string password)
{
SHA256Managed crypt = new SHA256Managed();
string hash = BitConverter.ToString(crypt.ComputeHash(Encoding.ASCII.GetBytes(password), 0, Encoding.ASCII.GetByteCount(password)));
crypt = null;
return hash.Replace("-", "").ToLower();
}
/**
* get pbkdf2
*
* @param string password
* @param string salt
* @param int iterations
* @param int length
* @return string
*/
public static string pbkdf2(this string password, string salt, int iterations = 1000, int length = 16)
{
Rfc2898DeriveBytes hash = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), iterations);
string derivedk = BitConverter.ToString(hash.GetBytes(length));
hash = null;
return derivedk.Replace("-", "").ToLower(); ;
}
/**
* get specific value from JToken
*
* @param JToken jArray
* @param string varid
* @return string
*/
public static string getVar(this JToken jArray, string varid)
{
foreach (JToken jToken in jArray)
{
JToken jVarid = jToken["varid"];
if (jVarid.ToString().Equals(varid))
{
jVarid = null;
jArray = null;
varid = null;
return jToken["varvalue"].ToString();
}
jVarid = null;
}
jArray = null;
varid = null;
return string.Empty;
}
/**
* check if string is empty or null
*
* @param string value
* @return bool
*/
public static bool IsNullOrEmpty(this string value)
{
return string.IsNullOrEmpty(value);
}
/**
* convert string to int
*
* @param string value
* @return int
*/
public static int ToInt(this string value)
{
int b = 0;
int.TryParse(value, out b);
return b;
}
/**
* calculate the background color for rsrp
* see http://www.lte-anbieter.info/technik/rsrp.php
*
* @param int rsrp
* @return Brush
*/
public static Brush getRSRPColor(int rsrp)
{
if (rsrp >= -65)
{
return Brushes.DarkGreen;
}
else if (rsrp <= -66 && rsrp >= -83)
{
return Brushes.Green;
}
else if (rsrp <= -84 && rsrp >= -95)
{
return Brushes.Yellow;
}
else if (rsrp <= -96 && rsrp >= -105)
{
return Brushes.Orange;
}
else if (rsrp <= -106 && rsrp >= -125)
{
return Brushes.Red;
}
else if (rsrp <= -126)
{
return Brushes.DarkRed;
}
return Brushes.Transparent;
}
/**
* calculate the background color for rsrq
* see http://www.lte-anbieter.info/technik/rsrq.php
*
* @param int rsrp
* @return Brush
*/
public static Brush getRSRQColor(int rsrq)
{
if (rsrq >= -3)
{
return Brushes.DarkGreen;
}
else if (rsrq <= -4 && rsrq >= -6)
{
return Brushes.Green;
}
else if (rsrq <= -7 && rsrq >= -8)
{
return Brushes.Yellow;
}
else if (rsrq <= -9 && rsrq >= -11)
{
return Brushes.Orange;
}
else if (rsrq <= -12 && rsrq >= -15)
{
return Brushes.Red;
}
else if (rsrq <= -16 && rsrq >= -20)
{
return Brushes.DarkRed;
}
return Brushes.Transparent;
}
/**
* check for update
*/
public static bool checkUpdate(string currentVersion)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("https://stricted.net/version.xml");
string version = xmlDocument.DocumentElement["version"].InnerText;
if (currentVersion.Equals(version).Equals(false))
{
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
public static bool checkInstalled(string c_name)
{
string displayName = string.Empty;
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (string.IsNullOrWhiteSpace(displayName).Equals(false) && displayName.Equals(c_name))
{
return true;
}
}
key.Close();
}
registryKey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null)
{
foreach (RegistryKey subkey in key.GetSubKeyNames().Select(keyName => key.OpenSubKey(keyName)))
{
displayName = subkey.GetValue("DisplayName") as string;
if (string.IsNullOrWhiteSpace(displayName).Equals(false) && displayName.Equals(c_name))
{
return true;
}
}
key.Close();
}
return false;
}
}
}