Represents Tox data (unencrypted or encrypted).
示例#1
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            ToxData data = obj as ToxData;

            if ((object)data == null)
            {
                return(false);
            }

            return(this == data);
        }
示例#2
0
文件: Tox.cs 项目: hexafluoride/Detox
        /// <summary>
        /// Initializes a new instance of Tox.
        /// </summary>
        /// <param name="options">The options to initialize this instance of Tox with.</param>
        /// <param name="data">A byte array containing Tox save data.</param>
        /// <param name="key">The key to decrypt the given encrypted Tox profile data. If the data is not encrypted, this should be null.</param>
        public Tox(ToxOptions options, ToxData data, ToxEncryptionKey key = null)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            var optionsStruct = options.Struct;

            if (key == null || !data.IsEncrypted)
            {
                var error = ToxErrorNew.Ok;
                optionsStruct.SetData(data.Bytes, ToxSaveDataType.ToxSave);

                _tox = ToxFunctions.New(ref optionsStruct, ref error);

                if (_tox == null || _tox.IsInvalid || error != ToxErrorNew.Ok)
                    throw new Exception("Could not create a new instance of tox, error: " + error.ToString());
            }
            else
            {
                var error = ToxErrorNew.Ok;
                var decryptError = ToxErrorDecryption.Ok;
                byte[] decryptedData = ToxEncryption.DecryptData(data.Bytes, key, out decryptError);
                optionsStruct.SetData(decryptedData, ToxSaveDataType.ToxSave);

                _tox = ToxFunctions.New(ref optionsStruct, ref error);

                if (_tox == null || _tox.IsInvalid || error != ToxErrorNew.Ok || decryptError != ToxErrorDecryption.Ok)
                    throw new Exception(string.Format("Could not create a new instance of tox, error: {0}, decrypt error: {1}" + error.ToString(), decryptError.ToString()));
            }

            optionsStruct.Free();
            Options = options;
        }
示例#3
0
 public ExtendedTox(ToxOptions options, ToxData data = null, ToxEncryptionKey key = null) :
     base(options, data, key)
 {
 }
示例#4
0
        internal static ToxDataInfo FromToxData(ToxData data)
        {
            try
            {
                ToxId         id            = null;
                string        name          = null;
                string        statusMessage = null;
                ToxUserStatus status        = ToxUserStatus.None;
                byte[]        secretKey     = null;

                using (var stream = new MemoryStream(data.Bytes))
                    using (var reader = new BinaryReader(stream))
                    {
                        stream.Position += sizeof(uint);

                        uint cookie = reader.ReadUInt32();
                        if (cookie != ToxConstants.Cookie)
                        {
                            throw new Exception("Invalid cookie, this doesn't look like a tox profile");
                        }

                        uint length = reader.ReadUInt32();
                        long left   = reader.BaseStream.Length - reader.BaseStream.Position;

                        while (left >= length)
                        {
                            var type = ReadStateType(reader);

                            if (type == StateType.EOF)
                            {
                                break;
                            }

                            switch (type)
                            {
                            case StateType.NospamKeys:
                                int    nospam    = reader.ReadInt32();
                                byte[] publicKey = reader.ReadBytes(ToxConstants.PublicKeySize);

                                secretKey = reader.ReadBytes(ToxConstants.SecretKeySize);
                                id        = new ToxId(publicKey, nospam);
                                break;

                            case StateType.Name:
                                name = Encoding.UTF8.GetString(reader.ReadBytes((int)length), 0, (int)length);
                                break;

                            case StateType.StatusMessage:
                                statusMessage = Encoding.UTF8.GetString(reader.ReadBytes((int)length), 0, (int)length);
                                break;

                            case StateType.Status:
                                status = (ToxUserStatus)reader.ReadByte();
                                break;

                            default:
                            case StateType.Dht:
                            case StateType.Friends:
                            case StateType.TcpRelay:
                            case StateType.PathNode:
                                stream.Position += length; //skip this
                                break;

                            case StateType.Corrupt:
                                throw new Exception("This Tox save file is corrupt");
                            }

                            left = reader.BaseStream.Length - reader.BaseStream.Position;
                            if (left < sizeof(uint))
                            {
                                break;
                            }
                            else
                            {
                                length = reader.ReadUInt32();
                            }
                        }
                    }

                return(new ToxDataInfo()
                {
                    Id = id,
                    Name = name,
                    StatusMessage = statusMessage,
                    Status = status,
                    SecretKey = new ToxKey(ToxKeyType.Secret, secretKey)
                });
            }
            catch { return(null); }
        }
示例#5
0
        internal static ToxDataInfo FromToxData(ToxData data)
        {
            try
            {
                ToxId id = null;
                string name = null;
                string statusMessage = null;
                ToxUserStatus status = ToxUserStatus.None;
                byte[] secretKey = null;

                using (var stream = new MemoryStream(data.Bytes))
                using (var reader = new BinaryReader(stream))
                {
                    stream.Position += sizeof(uint);

                    uint cookie = reader.ReadUInt32();
                    if (cookie != ToxConstants.Cookie)
                        throw new Exception("Invalid cookie, this doesn't look like a tox profile");

                    uint length = reader.ReadUInt32();
                    long left = reader.BaseStream.Length - reader.BaseStream.Position;

                    while (left >= length)
                    {
                        var type = ReadStateType(reader);

                        switch (type)
                        {
                            case StateType.NospamKeys:
                                uint nospam = reader.ReadUInt32();
                                byte[] publicKey = reader.ReadBytes(ToxConstants.PublicKeySize);

                                secretKey = reader.ReadBytes(ToxConstants.SecretKeySize);
                                id = new ToxId(publicKey, nospam);
                                break;
                            case StateType.Dht:
                                stream.Position += length; //skip this
                                break;
                            case StateType.Friends:
                                stream.Position += length; //skip this
                                break;
                            case StateType.Name:
                                name = Encoding.UTF8.GetString(reader.ReadBytes((int)length), 0, (int)length);
                                break;
                            case StateType.StatusMessage:
                                statusMessage = Encoding.UTF8.GetString(reader.ReadBytes((int)length), 0, (int)length);
                                break;
                            case StateType.Status:
                                status = (ToxUserStatus)reader.ReadByte();
                                break;
                            case StateType.TcpRelay:
                                stream.Position += length; //skip this
                                break;
                            case StateType.PathNode:
                                stream.Position += length; //skip this
                                break;
                            case StateType.Corrupt:
                                throw new Exception("This Tox save file is corrupt");
                            default:
                                break;
                        }

                        left = reader.BaseStream.Length - reader.BaseStream.Position;
                        if (left < sizeof(uint))
                            break;
                        else
                            length = reader.ReadUInt32();
                    }
                }

                return new ToxDataInfo()
                {
                    Id = id,
                    Name = name,
                    StatusMessage = statusMessage,
                    Status = status,
                    SecretKey = new ToxKey(ToxKeyType.Secret, secretKey)
                };
            }
            catch { return null; }
        }