Skip to content

Instantly share code, notes, and snippets.

View thebitbrine's full-sized avatar

TheBitBrine thebitbrine

View GitHub Profile
@thebitbrine
thebitbrine / AllowListener.cs
Last active February 14, 2019 08:57
Allows to listen on a specific address for HTTP requests.
public void AllowListener(string URL)
{
string command = $"http add urlacl url={ new Uri(URL).AbsoluteUri } user=Everyone";
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("netsh", command) { Verb = "runas" });
}
@thebitbrine
thebitbrine / Objectify.cs
Last active February 10, 2019 17:04
Parses KeyValuePair into string and back.
#region Objectify
public string WriteObject(List<KeyValuePair<string, object>> RawData)
{
var ParsedData = new StringBuilder();
foreach (var Item in RawData)
ParsedData.Append($"{Sanify(Item.Key)}:{Sanify(Item.Value)}:{Sanify(Item.Value.GetType().FullName)}\n");
return ParsedData.ToString();
}
@thebitbrine
thebitbrine / FormatJSON.cs
Last active March 10, 2019 14:58
Formats JSON.
public string FormatJson(string json)
{
const string indentString = " ";
int indentation = 0;
int quoteCount = 0;
var result =
from ch in json
let quotes = ch == '"' ? quoteCount++ : quoteCount
let lineBreak = ch == ',' && quotes % 2 == 0 ? ch + Environment.NewLine + string.Concat(Enumerable.Repeat(indentString, indentation)) : null
let openChar = ch == '{' || ch == '[' ? ch + Environment.NewLine + string.Concat(Enumerable.Repeat(indentString, ++indentation)) : ch.ToString()
@thebitbrine
thebitbrine / ValidateIPv4.cs
Created February 5, 2019 21:29
Validates IPv4 IP addresses.
public bool ValidateIPv4(string IP)
{
if (String.IsNullOrWhiteSpace(IP)) return false;
string[] splitValues = IP.Split('.');
if (splitValues.Length != 4) return false;
return splitValues.All(r => byte.TryParse(r, out _));
}
@thebitbrine
thebitbrine / Base64.cs
Created January 28, 2019 02:41
Basic Base64 encoding and decoding.
public string Base64Encode(string Text)
{
return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(Text));
}
public string Base64Decode(string EncodedData)
{
return System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(EncodedData));
}
@thebitbrine
thebitbrine / AES256CBCSTR.cs
Last active January 28, 2019 02:12
Basic AES256 (CBC) string encryption and decryption.
public byte[] EncryptString_AES(string Data, string Key)
{
byte[] PaddedKey = StringToPaddedBytes(Key);
byte[] Encrypted;
byte[] IV;
using (System.Security.Cryptography.Aes AES = System.Security.Cryptography.Aes.Create())
using (var MemEncrypt = new System.IO.MemoryStream())
{
AES.Mode = System.Security.Cryptography.CipherMode.CBC;
@thebitbrine
thebitbrine / GetWebString.cs
Last active April 29, 2019 12:40
Downloads content from given URL and return it as a string.
public string GetWebString(string URL)
{
var client = new System.Net.WebClient() {Encoding = System.Text.Encoding.UTF8};
client.Headers.Add("user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");
return client.DownloadString(URL);
}
@thebitbrine
thebitbrine / InsertText.cs
Last active January 9, 2019 01:06
Inserts text into given string positioned by relative location percentage.
public string InsertText(string Line, string Text, int PositionPercent)
{
int Index = 0;
char[] LineArr = Line.ToCharArray();
double Position = ((double)Line.Length / 100) * PositionPercent;
for (int i = 0; i < Line.Length; i++)
{
if (i >= Position && Index < Text.Length)
{
LineArr[i] = Text[Index];
@thebitbrine
thebitbrine / CheckPort.cs
Last active January 8, 2019 18:26
Check if given port is accessible or not.
public bool CheckPort(string Address, int Port, int Timeout)
{
using (var Client = new System.Net.Sockets.TcpClient())
{
var Open = Client.BeginConnect(Address, Port, null, null).AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(Timeout));
Client.Close();
return Open;
}
}
@thebitbrine
thebitbrine / GenSeq.cs
Created January 7, 2019 18:43
Generates 'int' sequence into an array.
public int[] GenSeq(int Start, int End, int Step)
{
List<int> Seq = new List<int>();
for (int i = Start; i < End; i = i + Step)
Seq.Add(i);
return Seq.ToArray();
}