-
Notifications
You must be signed in to change notification settings - Fork 53
/
Utils.cs
155 lines (134 loc) · 4.67 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace WidevineClient
{
class Utils
{
public static double EvaluateEquation(string equation, int decimals = 3)
{
var dataTable = new DataTable();
return Math.Round((double)dataTable.Compute(equation, ""), decimals);
}
public static string RunCommand(string command, string args)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = command;
p.StartInfo.Arguments = args;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return output;
}
public static int RunCommandCode(string command, string args)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = command;
p.StartInfo.Arguments = args;
p.Start();
p.WaitForExit();
return p.ExitCode;
}
public static byte[] Xor(byte[] a, byte[] b)
{
byte[] x = new byte[Math.Min(a.Length, b.Length)];
for (int i = 0; i < x.Length; i++)
{
x[i] = (byte)(a[i] ^ b[i]);
}
return x;
}
public static string GenerateRandomId()
{
return BytesToHex(RandomBytes(3)).ToLower();
}
public static byte[] RandomBytes(int length)
{
var bytes = new byte[length];
new Random().NextBytes(bytes);
return bytes;
}
public static string[] GetElementsInnerTextByAttribute(string html, string element, string attribute)
{
List<string> content = new List<string>();
foreach (string line in html.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None))
{
if (line.Contains("<" + element) && line.Contains(attribute))
{
string contentPart = line.Substring(0, line.LastIndexOf("<"));
if (contentPart.EndsWith(">"))
contentPart = contentPart[..^1];
contentPart = contentPart[(contentPart.LastIndexOf(">") + 1)..];
if (contentPart.Contains("<"))
contentPart = contentPart[..contentPart.IndexOf("<")];
content.Add(contentPart);
}
}
return content.ToArray();
}
public static string BytesToHex(byte[] data)
{
return BitConverter.ToString(data).Replace("-", "");
}
public static byte[] HexToBytes(string hex)
{
hex = hex.Trim();
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
public static bool IsBase64Encoded(string str)
{
try
{
byte[] data = Convert.FromBase64String(str);
return true;
}
catch
{
return false;
}
}
public static string Base64Pad(string base64)
{
if (base64.Length % 4 != 0)
{
base64 = base64.PadRight(base64.Length + (4 - (base64.Length % 4)), '=');
}
return base64;
}
public static string Base64ToString(string base64)
{
return Encoding.UTF8.GetString(Convert.FromBase64String(base64));
}
public static string StringToBase64(string str)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
}
public static void TitleProgress(long read, long length)
{
long readMB = read / 1024 / 1024;
long lengthMB = length / 1024 / 1024;
Console.Title = $"{readMB}/{lengthMB}MB";
}
public static void TitleProgressNoConversion(long read, long length)
{
Console.Title = $"{read}/{length}MB";
}
public static string Version()
{
return System.Reflection.Assembly.GetCallingAssembly().GetName().Version.ToString();
}
}
}