public BinarySerializerData Serialize(List <ushort> value)
        {
            byte[] b = new byte[value.Count * stride];
            for (int i = 0; i < value.Count; i++)
            {
                Buffer.BlockCopy(BitShifter.ToByte(value[i]), 0, b, i * stride, stride);
            }

            return(new BinarySerializerData(FileHelper.Compress(b), value.Count * stride));
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            CommandLineParser cmd = CommandLineParser.Parse(args);

            Log.CreateInstance(true);

            string keyText = null;

            if (cmd["key"] != null)
            {
                keyText = cmd["key"].Value;
            }
            else
            {
                keyText = PasswordPrompt.Get("Please enter the encryption key");
                string keyText2 = PasswordPrompt.Get("Please confirm the encryption key");

                if (keyText != keyText2)
                {
                    Log.Instance.Write(Log_Severity.Fatal, "Keys did not match");
                }
            }

            if (cmd["to"] == null || cmd["from"] == null)
            {
                Log.Instance.Write(Log_Severity.Fatal, "Need arguments 'to' and 'from'");
            }

            ulong sender    = ulong.Parse(cmd["from"].Value);
            ulong recipient = ulong.Parse(cmd["to"].Value);

            var mt  = new MersenneTwister((uint)Guid.NewGuid().GetHashCode());
            var key = Encoding.UTF8.GetBytes(keyText);

            if (sender == 0)
            {
                sender = mt.NextULong();
            }

            if (recipient == 0)
            {
                recipient = mt.NextULong();
            }

            var iv   = mt.NextBytes(BLOCK_SIZE);
            var data = BitShifter.ToByte(sender).Concat(BitShifter.ToByte(recipient)).ToArray();

            BufferedBlockCipher cipher   = new CtsBlockCipher(new CbcBlockCipher(new AesEngine()));
            ICipherParameters   keyParam = new ParametersWithIV(new KeyParameter(key), iv);

            cipher.Init(true, keyParam);
            Log.Instance.Write(iv.Concat(cipher.DoFinal(data, 0, data.Length)).ToArray().ToHex());
        }
Exemplo n.º 3
0
        public static string Encrypt(ulong sender, ulong recipient)
        {
            var mt  = new MersenneTwister((uint)Guid.NewGuid().GetHashCode());
            var key = Encoding.UTF8.GetBytes(((FusionBotConfig)Globals.Bot.Config).DonationPaymentIdKey);

            if (sender == 0)
            {
                sender = mt.NextULong();
            }

            if (recipient == 0)
            {
                recipient = mt.NextULong();
            }

            var iv   = mt.NextBytes(BLOCK_SIZE);
            var data = BitShifter.ToByte(sender).Concat(BitShifter.ToByte(recipient)).ToArray();

            BufferedBlockCipher cipher   = new CtsBlockCipher(new CbcBlockCipher(new AesEngine()));
            ICipherParameters   keyParam = new ParametersWithIV(new KeyParameter(key), iv);

            cipher.Init(true, keyParam);
            return(iv.Concat(cipher.DoFinal(data, 0, data.Length)).ToArray().ToHex());
        }
        protected void GetMember(object objectToSerialize, string name, Type type, object value, XElement e, bool preferBinarySerialization)
        {
            //this happens when the type of the member is different to the type of the value assigned to it
            //for example a member might be declared as a base class or interface and have a derived class assigned to it
            Type valType;

            if (value != null && (valType = value.GetType()) != type)
            {
                type = valType;
            }

            Ns ns = Lookup(type);

            Object_Type ot = ns.ObjectType;

            switch (ot)
            {
            case Object_Type.Struct:
            case Object_Type.Class:
            {
                bool isShared = Attribute.IsDefined(ns.Type, typeof(SerializeAsSharedObject), true);
                if (isShared)
                {
                    //first thing we do is create an element that links this value to the shared object
                    XElement assetObjectElement = XHelper.CreateElement(e, ns.Name, name);

                    if (value == null)
                    {
                        XHelper.CreateAttribute(assetObjectElement, "ID", "null");
                        return;
                    }

                    byte[] hash = MD5.Create().ComputeHash(BitShifter.ToByte(value.GetHashCode()));
                    //prefix with an X to make sure first character is a letter in accordance with XML standard
                    string s = "X" + string.Concat(hash.Select(x => x.ToString("X2")));

                    XHelper.CreateAttribute(assetObjectElement, "ID", s);

                    //then we actually create the shared object if it has not already been created
                    if (!checksums.Contains(s))
                    {
                        checksums.Add(s);

                        XElement sharedObjectElement = XHelper.CreateElement(sharedElement, ns.Name, s);

                        if (!Serializer.HasSerializer(type, preferBinarySerialization))
                        {
                            if (value != null)
                            {
                                GetMembers(value, sharedObjectElement);
                            }
                            else
                            {
                                CreateValue(sharedObjectElement, type, value, false);
                            }
                        }
                        else
                        {
                            CreateValue(sharedObjectElement, type, value, preferBinarySerialization);
                        }
                    }
                }
                else
                {
                    //if this class has a serializer, use it, otherwise we recurse through the objects members and serialize the items individually
                    if (!Serializer.HasSerializer(type, preferBinarySerialization))
                    {
                        if (value != null)
                        {
                            GetMembers(value, XHelper.CreateElement(e, ns.Name, name));
                        }
                        else
                        {
                            CreateValue(XHelper.CreateElement(e, ns.Name, name), type, value, false);
                        }
                    }
                    else
                    {
                        CreateValue(XHelper.CreateElement(e, ns.Name, name), type, value, preferBinarySerialization);
                    }
                }
            }
            break;

            case Object_Type.Primitive:
            {
                //any types that are Object_Type.Primitive have a built in serializer. so we know they can always be serialized
                CreateValue(XHelper.CreateElement(e, ns.Name, name), type, value, preferBinarySerialization);
            }
            break;

            case Object_Type.Enum:
            {
                //any types that are Object_Type.Enum use the built in IntSerializer. so we know they can always be serialized
                CreateValue(XHelper.CreateElement(e, ns.Name, name), ns.Type, value, preferBinarySerialization);
            }
            break;

            case Object_Type.List:
            case Object_Type.Dictionary:
            case Object_Type.Array:
            {
                //for collections. we need to iterate through the items and serialize each one
                //but we need to set this up recursively, in case the items in a collection are themselves collections
                if (value != null)
                {
                    if (!Serializer.HasSerializer(type, preferBinarySerialization))
                    {
                        SerializeCollection(objectToSerialize, e, name, type, value, preferBinarySerialization);
                    }
                    else
                    {
                        CreateValue(XHelper.CreateElement(e, ns.Name, name), type, value, preferBinarySerialization);
                    }
                }
                else
                {
                    CreateValue(XHelper.CreateElement(e, ns.Name, name), type, value, preferBinarySerialization);
                }
            }
            break;

            default:
                CreateValue(XHelper.CreateElement(e, ns.Name, name), type, null, preferBinarySerialization);     //if we can't determine the object type, just serialize it as null
                break;
            }
        }
Exemplo n.º 5
0
        private static string GetIpFromInteger(uint ipInt)
        {
            var b = BitShifter.ToByte(ipInt);

            return($"{b[0]}.{b[1]}.{b[2]}.{b[3]}");
        }