Push updated code

This commit is contained in:
Zontreck 2022-07-24 19:38:20 -07:00
parent d1ce65cce7
commit 62c4eb5850
9 changed files with 549 additions and 2 deletions

42
ArgumentParser.cs Normal file
View file

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibZNI
{
public class ArgumentParser
{
public static Arguments Parse(string[] lInputs)
{
Arguments a = new Arguments();
for(int i = 0; i < lInputs.Length; i ++ )
{
if (lInputs[i].StartsWith("-"))
{
try
{
if (lInputs[i + 1].StartsWith("-"))
a[lInputs[i]] = "1";
else
{
a[lInputs[i]] = lInputs[i + 1];
}
}catch(IndexOutOfRangeException e)
{
a[lInputs[i]] = "1";
}
}
}
return a;
}
}
public class Arguments : Dictionary<string, string>
{
}
}

89
HTTP.cs Normal file
View file

@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
namespace LibZNI
{
public class HTTP
{
public static HTTPReplyData performRequest(string url, string sJson)
{
HttpRequestMessage hrm = new HttpRequestMessage();
hrm.Method = HttpMethod.Post;
hrm.RequestUri = new Uri(url);
hrm.Content = new StringContent(sJson, Encoding.UTF8, "application/json");
return HTTP.Request(hrm);
}
public static HTTPReplyData performRequest(string url, string sJson, string xSLOwner)
{
HttpRequestMessage hrm = new HttpRequestMessage();
hrm.Method = HttpMethod.Post;
hrm.RequestUri = new Uri(url);
hrm.Headers.Add("X-SecondLife-Owner-Key", xSLOwner);
hrm.Content = new StringContent(sJson, Encoding.UTF8, "application/json");
return HTTP.Request(hrm);
}
public static HTTPReplyData Request(HttpRequestMessage request)
{
HTTPReplyData rd = new HTTPReplyData();
HttpClient client = new HttpClient();
Task<HttpResponseMessage> t_hrm = client.SendAsync(request);
t_hrm.Wait();
HttpResponseMessage response = t_hrm.Result;
try
{
rd.Code = (int)response.StatusCode;
HttpContent cnt = response.Content;
HttpResponseHeaders hrh = response.Headers;
foreach (KeyValuePair<string, IEnumerable<string>> kvp in hrh)
{
foreach(string key in kvp.Value)
{
rd.Headers[kvp.Key] = key;
}
}
HttpContentHeaders hch = cnt.Headers;
foreach (KeyValuePair<string, IEnumerable<string>> kvp in hch)
{
foreach (string key in kvp.Value)
{
rd.Headers[kvp.Key] = key;
}
}
rd.ContentType = rd.Headers["Content-Type"];
Task<byte[]> bf = cnt.ReadAsByteArrayAsync();
bf.Wait();
rd.MessageAsBytes = bf.Result;
rd.MessageAsString = Encoding.UTF8.GetString(bf.Result);
return rd;
}catch(Exception ex)
{
return rd;
}
}
}
public class HTTPReplyData
{
public int Code;
public string ContentType;
public string MessageAsString;
public byte[] MessageAsBytes;
public Dictionary<string, string> Headers=new Dictionary<string, string>();
}
}

38
Integrity/Branch.cs Normal file
View file

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibZNI.Integrity
{
internal class Branch
{
private Leaf lA;
private Leaf lB;
public Branch(Leaf a, Leaf b)
{
lA = a;
lB = b;
}
public byte[] GetNodeHash()
{
byte[] h1 = Tools.SHA256HashBytes(lA.InputBytes);
byte[] h2 = Tools.SHA256HashBytes(lB.InputBytes);
byte[] h3 = new byte[h1.Length + h2.Length];
h1.CopyTo(h3, 0);
h2.CopyTo(h3, h1.Length);
byte[] b = Tools.SHA256HashBytes(Tools.SHA256HashBytes(h3));
return b;
}
public override string ToString()
{
byte[] i = GetNodeHash();
return Tools.Hash2String(i);
}
}
}

21
Integrity/Leaf.cs Normal file
View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibZNI.Integrity
{
internal class Leaf
{
public byte[] InputBytes;
public Leaf(byte[] inputBytes)
{
InputBytes = inputBytes;
}
public Leaf(string input)
{
InputBytes = Encoding.UTF8.GetBytes(input);
}
}
}

View file

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibZNI.Integrity
{
internal class LeafCreationException : Exception
{
public LeafCreationException(string sMsg):base(sMsg)
{
}
}
}

88
Integrity/Tree.cs Normal file
View file

@ -0,0 +1,88 @@

#define MERKLE_VERBOSE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibZNI.Integrity
{
public class Tree
{
private List<Leaf> Leaves;
public string MasterHash;
public Tree()
{
Leaves = new List<Leaf>();
}
public void NewLeaf(byte[] b = null, string s = null)
{
if(b != null)
{
Leaves.Add(new Leaf(b));
return;
}
if(s != null)
{
Leaves.Add(new Leaf(s));
return;
}
throw new LeafCreationException("New leaf cannot be null!");
}
/// <summary>
/// This process will lock the program up for a few moments while the tree is processed
/// </summary>
public void ProcessTree()
{
List<Branch> branches = new List<Branch>();
if((Leaves.Count % 2) != 0)
{
Leaves.Add(Leaves.Last());
}
for(int i=0;i< Leaves.Count; i += 2)
{
Branch br = new Branch(Leaves[i], Leaves[i+1]);
bool iDo = false;
#if MERKLE_VERBOSE
iDo = true;
#endif
if(iDo)
{
Console.WriteLine($"New Branch: {br}");
}
branches.Add(br);
}
while(branches.Count > 1)
{
if((branches.Count % 2) != 0)
{
branches.Add(branches.Last());
}
Branch[] copy = branches.ToArray();
branches.Clear();
for(int i = 0; i < copy.Length; i += 2)
{
Leaf a = new Leaf(copy[i].GetNodeHash());
Leaf b = new Leaf(copy[i + 1].GetNodeHash());
bool iDo = false;
Branch br = new Branch(a,b);
#if MERKLE_VERBOSE
iDo=true;
#endif
if (iDo)
{
Console.WriteLine($"New Branch: {br}");
}
branches.Add(br);
}
}
MasterHash = branches[0].ToString();
}
}
}

View file

@ -4,11 +4,20 @@
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SignAssembly>false</SignAssembly>
<SignAssembly>True</SignAssembly>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Configurations>Debug;Release;DebPub;KVPBuild</Configurations>
<AssemblyOriginatorKeyFile>C:\Users\tyler\Downloads\ZNI.pfx</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<None Remove="TEA.cs.ignore" />
</ItemGroup>
<ItemGroup>
<Compile Include="TEA.cs.ignore" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />

163
TEA.cs.ignore Normal file
View file

@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace LibZNI
{
public class XTEA
{
int[] xtea_key = new int[4];
int rounds = 6;
int DELTA;
int SECRET = 99999;
XTEA()
{
DELTA = int.Parse("0x9E3779B9", System.Globalization.NumberStyles.HexNumber);
}
public int hex2int(string hex)
{
if(hex.Substring(0,2) == "0x")
{
return int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}
if (hex.Substring(0, 1) == "x")
{
return int.Parse("0" + hex, System.Globalization.NumberStyles.HexNumber);
}
return int.Parse("0x" + hex, System.Globalization.NumberStyles.HexNumber);
}
void set_xtea_key(int[] key)
{
xtea_key = key;
}
public int[] make_key(string str)
{
MD5 sum = MD5.Create();
str = Convert.ToHexString(sum.ComputeHash(Encoding.UTF8.GetBytes(str)));
return new int[4]
{
hex2int(str.Substring(0, 8)),
hex2int(str.Substring(8, 8)),
hex2int(str.Substring(16, 8)),
hex2int(str.Substring(24, 8))
};
}
static string ConvertToBase64Arithmetic(uint i)
{
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
StringBuilder sb = new StringBuilder();
do
{
sb.Insert(0, alphabet[(int)(i % 64)]);
i = i / 64;
} while (i != 0);
return sb.ToString();
}
public string encipher(int v0, int v1)
{
int num = rounds;
int sum = 0;
do
{
v0 += (((v1 << 4) ^ ((v1 >> 5) & 0x07FFFFFF)) + v1) ^ (sum + xtea_key[sum & 3]);
sum += DELTA;
v1 += (((v0 << 4) ^ ((v0 >> 5) & 0x07FFFFFF)) + v0) ^ (sum + xtea_key[(sum >> 11) & 3]);
num--;
} while (num >0);
return ConvertToBase64Arithmetic((uint)v0).Substring(0, 6) + ConvertToBase64Arithmetic((uint)v1).Substring(0, 6);
}
public string decipher(int v0, int v1)
{
int num = rounds;
int sum = DELTA * num;
do
{
v1 -= (((v0 << 4) ^ ((v0 >> 5) & 0x07FFFFFF)) + v0) ^ (sum + xtea_key[(sum >> 11) & 3]);
sum -= DELTA;
v0 -= (((v1 << 4) ^ ((v1 >> 5) & 0x07FFFFFF)) + v1) ^ (sum + xtea_key[sum & 3]);
num--;
} while (num > 0);
return ConvertToBase64Arithmetic((uint)v0).Substring(0, 5) + ConvertToBase64Arithmetic((uint)v1).Substring(0, 5);
}
public string encrypt_string(string str)
{
str = Tools.Base64Encode(str);
int i = str.IndexOf('=');
if(i != -1)
{
str = str.Remove(i);
}
int len = str.Length;
str += "AAAAAAAAAA=";
string result="";
i = 0;
do
{
int i1 = int.Parse(Tools.Base64Decode(str.Substring(i, 5)+"A="));
int i2 = int.Parse(Tools.Base64Decode(str.Substring(i + 5, 5) + "A="));
result += encipher(i1, i2);
i += 10;
} while (i < len);
return result;
}
/*
public string decrypt_string(string str)
{
int len = str.Length;
int i = 0;
string result = "";
do
{
int v0;
int v1;
v0 = int.Parse(Tools.Base64Decode(str.Substring(i, 5) + "=="));
i += 6;
v1 = int.Parse(Tools.Base64Decode(str.Substring(i, 5) + "=="));
i += 6;
result += decipher(v0, v1);
} while (i < len);
i = result.Length - 1;
while(result.Substring(i-1,(i-(i-1))) == "AA")
{
result = result.Remove(i, ((i - i)==0? 1 : i));
i--;
}
i = result.Length - 1;
result = result.Substring()
}*/
}
}

View file

@ -1,16 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;
[assembly: LibZNI.AutoUpdater("/job/LibZNI", "library.tar")]
namespace LibZNI
{
public class Tools
{
/// <summary>
/// This function is meant for aiding in attacking the TripleDES encryption for the EDRA algorithm. We intentionally want to break TripleDES since we know for a fact we have one of the correct answers that was used as the encryption key. By doing this, we find the right encryption key with zero knowledge
/// </summary>
/// <param name="input">The TripleDES byte array we want to blank</param>
/// <returns>Zero filled Byte Array</returns>
public static byte[] MakeBlankKey(byte[] input)
{
return new byte[input.Length];
}
/// <summary>
/// This function is meant for aiding in attacking the TripleDES encryption for the EDRA algorithm. We intentionally want to break TripleDES since we know for a fact that we have one of the solutions. This function will increment the current key. If the attack has completed an exception will be thrown
/// </summary>
/// <param name="tdes">The key array</param>
/// <returns>The next key in sequence</returns>
public static byte[] IncrementAttackVector(byte[] tdes)
{
string hstr = Convert.ToHexString(tdes);
// Loop over the hex to check if every digit is an F
int Fi = 0;
for(int i = 0; i < hstr.Length; i++)
{
if (hstr[i] == 'F')
{
Fi++;
}
}
if (Fi == hstr.Length) throw new ArgumentException("The operation is already completed");
BigInteger num = BigInteger.Parse(hstr, System.Globalization.NumberStyles.HexNumber);
num++;
hstr = Convert.ToHexString(num.ToByteArray());
return Convert.FromHexString(hstr);
}
public static Int32 getTimestamp()
{
@ -63,6 +97,17 @@ namespace LibZNI
{
SHA256 Hasher = SHA256.Create();
return Tools.Hash2String(Hasher.ComputeHash(ToHash));
}
public static byte[] SHA256HashBytes(byte[] ToHash)
{
SHA256 hasher = SHA256.Create();
return hasher.ComputeHash(ToHash);
}
public static byte[] SHA256HashBytes(string ToHash)
{
SHA256 hasher = SHA256.Create();
return hasher.ComputeHash(UTF8Encoding.UTF8.GetBytes(ToHash));
}
public static string ZHX(string ToHash)
@ -275,6 +320,36 @@ namespace LibZNI
}
return sSplice;
}
public static BigInteger GetAtIndex(this List<BigInteger> X, BigInteger I)
{
BigInteger D = -1;
foreach(BigInteger V in X)
{
D++;
if (D >= I) return V;
}
return 0;
}
public static int GoesIntoTimes(this BigInteger X, BigInteger Z)
{
int nTimes = 0;
if (X < Z) nTimes++;
BigInteger XC = X;
while(XC < Z)
{
nTimes++;
XC = XC + X;
if (XC >= Z)
{
nTimes--;
break;
}
}
return nTimes;
}
}
public static class ZNILSLTools
@ -294,6 +369,12 @@ namespace LibZNI
{
return ParseString2List(item, opts, keepopts);
}
public static string llDumpList2String<T>(this List<T> items, string delimiter)
{
return String.Join(delimiter, items.Select(t => t.ToString()).ToArray());
}
internal static string[] Augment(this string[] itm, string x)
{
List<string> working = new List<string>(itm);