Пример #1
0
        public void TestToxHash()
        {
            byte[] data = new byte[0xBEEF];
            new Random().NextBytes(data);

            byte[] hash = ToxTools.Hash(data);
        }
Пример #2
0
        public bool SetVideoBitrate(int friendNumber, int bitrate, out ToxAvErrorSetBitrate error)
        {
            ThrowIfDisposed();

            error = ToxAvErrorSetBitrate.Ok;
            return(ToxAvFunctions.BitrateSet(_toxAv, ToxTools.Map(friendNumber), -1, bitrate, ref error));
        }
Пример #3
0
        public bool SendAudioFrame(int friendNumber, ToxAvAudioFrame frame, out ToxAvErrorSendFrame error)
        {
            ThrowIfDisposed();

            error = ToxAvErrorSendFrame.Ok;
            return(ToxAvFunctions.AudioSendFrame(_toxAv, ToxTools.Map(friendNumber), frame.Data, (uint)(frame.Data.Length / frame.Channels), (byte)frame.Channels, (uint)frame.SamplingRate, ref error));
        }
Пример #4
0
        public bool Answer(int friendNumber, int audioBitrate, int videoBitrate, out ToxAvErrorAnswer error)
        {
            ThrowIfDisposed();

            error = ToxAvErrorAnswer.Ok;
            return(ToxAvFunctions.Answer(_toxAv, ToxTools.Map(friendNumber), (uint)audioBitrate, (uint)videoBitrate, ref error));
        }
Пример #5
0
        public bool SendControl(int friendNumber, ToxAvCallControl control, out ToxAvErrorCallControl error)
        {
            ThrowIfDisposed();

            error = ToxAvErrorCallControl.Ok;
            return(ToxAvFunctions.CallControl(_toxAv, ToxTools.Map(friendNumber), control, ref error));
        }
Пример #6
0
        private byte[] GetAvatarHash(Stream stream)
        {
            var buffer = new byte[stream.Length];

            stream.Read(buffer, 0, (int)stream.Length);
            return(ToxTools.Hash(buffer));
        }
Пример #7
0
        public bool SendVideoFrame(int friendNumber, ToxAvVideoFrame frame, out ToxAvErrorSendFrame error)
        {
            ThrowIfDisposed();

            error = ToxAvErrorSendFrame.Ok;
            return(ToxAvFunctions.VideoSendFrame(_toxAv, ToxTools.Map(friendNumber), (ushort)frame.Width, (ushort)frame.Height, frame.Y, frame.U, frame.V, ref error));
        }
Пример #8
0
        /// <summary>
        /// Initializes a new instance of tox dns3.
        /// </summary>
        /// <param name="publicKey"></param>
        public ToxDns(string publicKey)
        {
            _toxDns3 = ToxDnsFunctions.New(ToxTools.StringToHexBin(publicKey));

            if (_toxDns3 == null || _toxDns3.IsInvalid)
            {
                throw new Exception("Could not create a new tox_dns3 instance with the provided public_key");
            }
        }
Пример #9
0
        public bool HashMatches(int friendNumber, byte[] hash)
        {
            byte[] avatar = GetAvatar(friendNumber);
            if (avatar == null)
            {
                return(false);
            }

            return(ToxTools.Hash(avatar).SequenceEqual(hash));
        }
Пример #10
0
        /// <summary>
        /// Decodes and decrypts the dns3 string returned by <see cref="GenerateDns3String"/>.
        /// </summary>
        /// <param name="dns3String">String to decrypt.</param>
        /// <param name="requestId">The request id retrieved with GenerateDns3String.</param>
        /// <returns></returns>
        public string DecryptDns3TXT(string dns3String, int requestId)
        {
            ThrowIfDisposed();

            byte[] id            = new byte[ToxConstants.AddressSize];
            byte[] idRecordBytes = Encoding.UTF8.GetBytes(dns3String);

            int result = ToxDnsFunctions.DecryptDns3TXT(_toxDns3, id, idRecordBytes, (uint)idRecordBytes.Length, ToxTools.Map(requestId));

            if (result == 0)
            {
                return(ToxTools.HexBinToString(id));
            }
            else
            {
                throw new Exception("Could not decrypt and decode id_record");
            }
        }
Пример #11
0
        /// <summary>
        /// Generates a dns3 string used to query the dns server.
        /// </summary>
        /// <param name="name">Name of the registered user.</param>
        /// <param name="requestId">The request id, to be used when calling DecryptDns3TXT.</param>
        /// <returns></returns>
        public string GenerateDns3String(string name, out int requestId)
        {
            ThrowIfDisposed();

            byte[] bytes  = Encoding.UTF8.GetBytes(name);
            byte[] result = new byte[1024];

            uint id     = new uint();
            int  length = ToxDnsFunctions.GenerateDns3String(_toxDns3, result, (ushort)result.Length, ref id, bytes, (byte)bytes.Length);

            requestId = ToxTools.Map(id);

            if (length != -1)
            {
                return(Encoding.UTF8.GetString(result, 0, length));
            }
            else
            {
                throw new Exception("Failed to generate a dns3 string");
            }
        }
Пример #12
0
        /// <summary>
        /// Decodes and decrypts the dns3 string returned by <see cref="GenerateDns3String"/>.
        /// </summary>
        /// <param name="dns3String"></param>
        /// <param name="requestId"></param>
        /// <returns></returns>
        public string DecryptDns3TXT(string dns3String, uint requestId)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            byte[] id            = new byte[32 + sizeof(uint) + sizeof(ushort)];
            byte[] idRecordBytes = Encoding.UTF8.GetBytes(dns3String);

            int result = ToxDnsFunctions.DecryptDns3TXT(_toxDns3, id, idRecordBytes, (uint)idRecordBytes.Length, (uint)requestId);

            if (result == 0)
            {
                return(ToxTools.HexBinToString(id));
            }
            else
            {
                throw new Exception("Could not decrypt and decode id_record");
            }
        }
Пример #13
0
        public void StringToHexBin_Result_AreEqual(string input, byte[] expected)
        {
            var result = ToxTools.StringToHexBin(input);

            Assert.AreEqual(expected, result);
        }
Пример #14
0
        private void RegisterAudioVideoCallbacks()
        {
            _onReceiveAudioFrameCallback = (IntPtr toxAv, uint friendNumber, IntPtr pcm, uint sampleCount, byte channels, uint samplingRate, IntPtr userData) =>
            {
                if (OnAudioFrameReceived != null)
                {
                    OnAudioFrameReceived(this, new ToxAvEventArgs.AudioFrameEventArgs(ToxTools.Map(friendNumber), new ToxAvAudioFrame(pcm, sampleCount, samplingRate, channels)));
                }
            };

            _onReceiveVideoFrameCallback = (IntPtr toxAv, uint friendNumber, ushort width, ushort height, IntPtr y, IntPtr u, IntPtr v, int yStride, int uStride, int vStride, IntPtr userData) =>
            {
                if (OnVideoFrameReceived != null)
                {
                    OnVideoFrameReceived(this, new ToxAvEventArgs.VideoFrameEventArgs(ToxTools.Map(friendNumber), new ToxAvVideoFrame(width, height, y, u, v, yStride, uStride, vStride)));
                }
            };

            ToxAvFunctions.RegisterAudioReceiveFrameCallback(_toxAv, _onReceiveAudioFrameCallback, IntPtr.Zero);
            ToxAvFunctions.RegisterVideoReceiveFrameCallback(_toxAv, _onReceiveVideoFrameCallback, IntPtr.Zero);
        }
Пример #15
0
        public void ValidHexString_Input_IsFalse(string hexString)
        {
            var result = ToxTools.ValidHexString(hexString);

            Assert.IsFalse(result);
        }
Пример #16
0
        public void IsValid_ValidIdString_IsTrue()
        {
            var result = ToxId.IsValid(ToxTools.HexBinToString(this.validTestId));

            Assert.IsTrue(result);
        }
Пример #17
0
        public void SendAvatar(int friendNumber, byte[] avatar)
        {
            //cancel any existing avatar file transfers with this friend first
            for (int i = _transfers.Count() - 1; i >= 0; i--)
            {
                //reverse loop to be able to delete entries while iterating through them, fun stuff
                var entry = _transfers.ElementAt(i);
                if (entry.Key.FriendNumber == friendNumber && entry.Key.Direction == FileTransferDirection.Outgoing)
                {
                    CancelTransfer(entry.Key);
                }
            }

            var         error = ToxErrorFileSend.Ok;
            ToxFileInfo fileInfo;

            if (avatar != null)
            {
                fileInfo = _tox.FileSend(friendNumber, ToxFileKind.Avatar, avatar.Length, "avatar.png", ToxTools.Hash(avatar), out error);
            }
            else
            {
                fileInfo = _tox.FileSend(friendNumber, ToxFileKind.Avatar, 0, "avatar.png");
            }

            if (error != ToxErrorFileSend.Ok)
            {
                Debugging.Write("Could not send file transfer request: " + error.ToString());
                return;
            }

            //no point in adding a 'dummy' transfer to our list
            if (avatar == null)
            {
                return;
            }

            var tr = new FileTransfer(fileInfo.Number, friendNumber, ToxFileKind.Avatar, FileTransferDirection.Outgoing);

            if (!_transfers.ContainsKey(tr))
            {
                _transfers.Add(tr, new MemoryStream(avatar));
            }
            else
            {
                Debugging.Write("Tried to add a filetransfer that's already in the list, panic!");
            }
        }
Пример #18
0
 /// <summary>
 /// Retrieves a string of the tox key.
 /// </summary>
 /// <returns></returns>
 public string GetString()
 {
     return(ToxTools.HexBinToString(key));
 }
Пример #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ToxKey"/> class
 /// </summary>
 /// <param name="type"></param>
 /// <param name="key"></param>
 public ToxKey(ToxKeyType type, string key)
 {
     KeyType  = type;
     this.key = ToxTools.StringToHexBin(key);
 }
Пример #20
0
        public void HexBinToString_Result_AreEqual(byte[] input, string expected)
        {
            var result = ToxTools.HexBinToString(input);

            Assert.AreEqual(expected, result);
        }
Пример #21
0
 public void Constructor_InvalidChecksumString_ArgumentException()
 {
     Assert.Throws <ArgumentException>(() => new ToxId(ToxTools.HexBinToString(this.invalidTestId)));
 }