/// <summary> Creates a new permutation that could be used with stdPerm </summary> /// <param name="max"></param> /// <param name="length"></param> /// <param name="rng"></param> /// <returns></returns> private static List <int> Permutation(int max, int length = -1, SRNG rng = null) { if (length < 0) { length = max; } if (rng == null) { rng = new SRNG(); } List <int> nums = new List <int>(max); for (int i = 0; i < max; i++) { nums.Add(i); } List <int> chosen = new List <int>(length); for (int i = 0; i < length; i++) { int j = rng.NextInt(0, nums.Count); chosen.Add(nums[j]); nums.RemoveAt(j); } return(chosen); }
// Unfortunately, this ends up relying on the SRNG. // oh well, it is the most compact way to test this kind of structure. public static FJ SetupData3() { var root = FJ.NewRoot(new JsonObject("name", "Universe9001")); SRNG rng = new SRNG(331337); return(Fill(rng, root, 0, new GenData[] { new GenData("Galaxy", 1, 4), new GenData("System", 2, 10), new GenData("Planet", 1, 7), new GenData("Moon", 0, 5) })); }
private static FJ Fill(SRNG rng, FJ parent, int index, GenData[] data) { var datum = data[index]; var num = rng.NextInt(datum.min, datum.max); for (int i = 0; i < num; i++) { var node = parent.AddChild(datum.thing + rng.NextInt(10000, 99999)); if (index != data.Length - 1) { Fill(rng, node, index + 1, data); } } return(parent); }
/// <summary> /// Attempts to set enc and dec to a pair of methods. /// Tests them agains some test data in a loop similar to how data is handled over the network, /// and makes sure that arbitrary cuts on the data doesn't break the encryption scheme. /// </summary> /// <param name="encrypt"> Function for encrypting the data </param> /// <param name="decrypt"> Function for decrypting the data </param> public void SetEncDec(Crypt encrypt, Crypt decrypt) { Log.Info("Doing encrypt/decrypt self test"); if (encrypt == null || decrypt == null) { Log.Warning("Proper Encryption/Decryption functions must be provided to clients..."); return; } byte[] oneEnc = encrypt(oneByte); byte[] oneDec = decrypt(oneEnc); if (oneDec.Length != 1 && oneDec[0] != oneByte[0]) { Log.Warning("!Encryption/decryption must properly handle tiny things as well! (One byte in size)"); return; } List <byte[]> wew = new List <byte[]>(); StringBuilder recieved = ""; int stringpos = 0; SRNG rand = new SRNG(); while (stringpos < testMessage.Length) { int next = stringpos + rand.NextInt(1, 42); if (next > testMessage.Length) { next = testMessage.Length; } int diff = next - stringpos; string cut = testMessage.Substring(stringpos, diff); stringpos = next; wew.Add(cut.ToBytesUTF8()); } string[] testMessages = testMessage.Split(EOT); byte[][] multibytearraydrifting = wew.ToArray(); StringBuilder held = ""; string str; int pos = 0; foreach (var bytes in multibytearraydrifting) { byte[] message = encrypt(bytes); message = decrypt(message); str = message.GetStringUTF8(); held += str; int index = held.IndexOf(EOT); while (index >= 0) { string pulled = held.Substring(0, index); held = held.Remove(0, index + 1); index = held.IndexOf(EOT); if (pulled.Length > 0) { if (pulled != testMessages[pos]) { Log.Warning($"Failed to convert message {pos} accurately. Expected {testMessage[pos]}, got { pulled }"); } recieved = recieved + pulled + EOT; pos++; } } } string fullRecieved = recieved.ToString(); if (fullRecieved == testMessage) { enc = encrypt; dec = decrypt; } else { string s = "Client.SetEncDec: Attempted to set Encryption/Decryption methods, but they did not work properly." + "\n\nExpected: " + testMessage + "\n\nRecieved: " + fullRecieved; Log.Warning(s); enc = null; dec = null; } }