public void TestToxEncryptionLoad() { var tox1 = new Tox(ToxOptions.Default); tox1.Name = "Test"; tox1.StatusMessage = "Hey"; var key = new ToxEncryptionKey("heythisisatest"); var data = tox1.GetData(key); Assert.IsNotNull(data, "Failed to encrypt the Tox data"); Assert.IsTrue(data.IsEncrypted, "We encrypted the data, but toxencryptsave thinks we didn't"); var tox2 = new Tox(ToxOptions.Default, ToxData.FromBytes(data.Bytes), key); if (tox2.Id != tox1.Id) Assert.Fail("Failed to load tox data correctly, tox id's don't match"); if (tox2.Name != tox1.Name) Assert.Fail("Failed to load tox data correctly, names don't match"); if (tox2.StatusMessage != tox1.StatusMessage) Assert.Fail("Failed to load tox data correctly, status messages don't match"); tox1.Dispose(); tox2.Dispose(); }
public void TestToxEncryption() { var key = new ToxEncryptionKey("heythisisatest"); byte[] garbage = new byte[0xBEEF]; new Random().NextBytes(garbage); byte[] encryptedData = ToxEncryption.EncryptData(garbage, key); Assert.IsNotNull(encryptedData, "Failed to encrypt the data"); byte[] decryptedData = ToxEncryption.DecryptData(encryptedData, key); Assert.IsNotNull(decryptedData, "Failed to decrypt the data"); if (!garbage.SequenceEqual(decryptedData)) Assert.Fail("Original data is not equal to the decrypted data"); }
public static byte[] EncryptData(byte[] data, ToxEncryptionKey key, out ToxErrorEncryption error) { if (data == null) throw new ArgumentNullException("data"); if (key == null) throw new ArgumentNullException("key"); byte[] output = new byte[data.Length + EncryptionExtraLength]; var pass = key.ToPassKey(); error = ToxErrorEncryption.Ok; if (!ToxEncryptionFunctions.PassKeyEncrypt(data, (uint)data.Length, ref pass, output, ref error) || error != ToxErrorEncryption.Ok) return null; return output; }
public static byte[] DecryptData(byte[] data, ToxEncryptionKey key, out ToxErrorDecryption error) { if (data == null) throw new ArgumentNullException("data"); if (key == null) throw new ArgumentNullException("key"); byte[] output = new byte[data.Length - EncryptionExtraLength]; var pass = key.ToPassKey(); error = ToxErrorDecryption.Ok; if (!ToxEncryptionFunctions.PassKeyDecrypt(data, (uint)data.Length, ref pass, output, ref error) || error != ToxErrorDecryption.Ok) return null; return output; }
public static byte[] EncryptData(byte[] data, ToxEncryptionKey key, out ToxErrorEncryption error) { if (data == null) { throw new ArgumentNullException("data"); } if (key == null) { throw new ArgumentNullException("key"); } byte[] output = new byte[data.Length + EncryptionExtraLength]; var pass = key.ToPassKey(); error = ToxErrorEncryption.Ok; if (!ToxEncryptionFunctions.PassKeyEncrypt(data, (uint)data.Length, ref pass, output, ref error) || error != ToxErrorEncryption.Ok) { return(null); } return(output); }
public ToxData GetData(ToxEncryptionKey key) { return _tox.GetData(key); }
public static byte[] DecryptData(byte[] data, ToxEncryptionKey key) { var error = ToxErrorDecryption.Ok; return(DecryptData(data, key, out error)); }
/// <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; }
/// <summary> /// Retrieves a ToxData object that contains the profile data of this Tox instance, encrypted with the provided key. /// </summary> /// <param name="key">The key to encrypt the Tox data with.</param> /// <returns></returns> public ToxData GetData(ToxEncryptionKey key) { ThrowIfDisposed(); var data = GetData(); byte[] encrypted = ToxEncryption.EncryptData(data.Bytes, key); return new ToxData(encrypted); }
private byte[] GetData(string password) { if (password == String.Empty) return ToxModel.Instance.GetData().Bytes; var encryptionKey = new ToxEncryptionKey(password); return ToxModel.Instance.GetData(encryptionKey).Bytes; }
public static byte[] EncryptData(byte[] data, ToxEncryptionKey key) { var error = ToxErrorEncryption.Ok; return EncryptData(data, key, out error); }
public ExtendedTox(ToxOptions options, ToxData data = null, ToxEncryptionKey key = null) : base(options, data, key) { }