Beispiel #1
0
        public NewUserProfileViewModel(string directory)
        {
            this.WhenAnyValue(x => x.Password)
            .Select(x => !string.IsNullOrEmpty(x))
            .ToPropertyEx(this, x => x.Encrypted);

            this.Login = ReactiveCommand.Create(() =>
            {
                var filePath = Path.Combine(directory, this.Name + ".tox");
                var tox      = new Tox(ToxOptions.Default());
                if (this.Encrypted)
                {
                    var data = tox.GetData().Bytes;
                    ToxEncryption.Encrypt(data, this.Password, out _);
                    var toxdata = ToxData.FromBytes(data);
                    tox.Dispose();
                    toxdata.Save(filePath);

                    tox = new Tox(ToxOptions.Default(), toxdata, this.Password);
                }

                return(new ToxSession(tox, filePath));
            }, this.WhenAnyValue(x => x.Name)
                                                .Select(fileName =>
            {
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    return(false);
                }

                var filePath = Path.Combine(directory, fileName + ".tox");
                if (File.Exists(filePath))
                {
                    return(false);
                }

                if ((from i in Path.GetInvalidFileNameChars()
                     from c in fileName
                     select i == c).Any(x => x))
                {
                    return(false);
                }

                return(true);
            }));
        }
Beispiel #2
0
        public void TestToxEncryption()
        {
            string password = "******";

            byte[] garbage = new byte[0xBEEF];
            new Random().NextBytes(garbage);

            byte[] encryptedData = ToxEncryption.EncryptData(garbage, password);
            Assert.IsNotNull(encryptedData, "Failed to encrypt the data");

            byte[] decryptedData = ToxEncryption.DecryptData(encryptedData, password);
            Assert.IsNotNull(decryptedData, "Failed to decrypt the data");

            if (!garbage.SequenceEqual(decryptedData))
            {
                Assert.Fail("Original data is not equal to the decrypted data");
            }
        }