Exemplo n.º 1
0
    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);
    }
Exemplo n.º 2
0
        /// <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;
            }
        }