public void Locker_Read() { //Arrange string testFilePath = Path.Combine(TestContext.DeploymentDirectory, "existingtestlocker.bin"); Locker <List <LockerKey> > keyLocker = null; Exception exception = null; byte[] salt = { 1, 2, 3, 4, 5, 6, 7, 8 }; AESEncryptor encryptor = new AESEncryptor(salt, 1234); GenericBinarySerializer <List <LockerKey> > serializer = new GenericBinarySerializer <List <LockerKey> >(); //Act try { CreateAndSaveLockerWithOneKey(testFilePath, "password", salt, "first", "one"); keyLocker = new Locker <List <LockerKey> >(encryptor, serializer, "password"); keyLocker.Open(testFilePath); } catch (Exception e) { exception = e; } //Assert Assert.IsNull(exception, $"Was not expecting an exception [{exception?.Message}]"); Assert.IsNotNull(keyLocker.Keys); Assert.AreEqual(1, keyLocker.Keys.Count, "was expecting 1 key"); var key = keyLocker.Keys.FirstOrDefault(k => k.Key == "first"); Assert.AreEqual("one", key.Value, "Mismatched first value"); }
public Locker <List <LockerKey> > GetNewLocker(string password, string filePath = null) { AESEncryptor encryptor = new AESEncryptor(_AESSalt, _AESIterations); GenericBinarySerializer <List <LockerKey> > serializer = new GenericBinarySerializer <List <LockerKey> >(); return(new Locker <List <LockerKey> >(encryptor, serializer, password, filePath)); }
public static Locker GetLocker(GlobalArguments arguments) { IEncryptor encryptor = new AESEncryptor(arguments.Salt.ToBytes(), arguments.Iterations); GenericBinarySerializer <Dictionary <string, string> > serializer = new GenericBinarySerializer <Dictionary <string, string> >(); Locker locker = new Locker(encryptor, serializer, arguments.Password, arguments.LockerPath); locker.Open(); return(locker); }
public void GenericBinarySerializer_Deserialize() { var reader = new StringReader(m_serializationString); var sut = new GenericBinarySerializer(); var actual = sut.Deserialize <Dictionary <string, int> >(() => reader); CollectionAssert.AreEqual(m_serializationData, actual); }
public void SaveModel(string path) { if (type == MACHINE_TYPE.NEURAL_NETWORK) { var binarySerializer = new GenericBinarySerializer(); binarySerializer.Serialize <IPredictorModel <double> >(model, () => new StreamWriter(root + path)); } }
public void GenericBinarySerializer_Serialize() { var writer = new StringWriter(); var sut = new GenericBinarySerializer(); sut.Serialize(m_serializationData, () => writer); var actual = writer.ToString(); Assert.AreEqual(m_serializationString, actual); }
public void GenericBinarySerializer_Serialize_Null() { //Arrange //Act ISerializer <Dictionary <string, string> > serializer = new GenericBinarySerializer <Dictionary <string, string> >(); byte[] result = serializer.Serialize(null); //Assert Assert.IsNull(result, "Was expecting null for a result"); }
public void GenericBinarySerializer_Serialize_Empty() { //Arrange string expectedResult = "AAEAAAD/////AQAAAAAAAAAEAQAAAOIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlvbmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXQMAAAAHVmVyc2lvbghDb21wYXJlcghIYXNoU2l6ZQADAAiSAVN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljLkdlbmVyaWNFcXVhbGl0eUNvbXBhcmVyYDFbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dCAAAAAAJAgAAAAAAAAAEAgAAAJIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuR2VuZXJpY0VxdWFsaXR5Q29tcGFyZXJgMVtbU3lzdGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0AAAAACw=="; //Act ISerializer <Dictionary <string, string> > serializer = new GenericBinarySerializer <Dictionary <string, string> >(); string result = Convert.ToBase64String(serializer.Serialize(new Dictionary <string, string>())); //Assert Assert.AreEqual(expectedResult, result, "Was expecting results to be equal"); }
private Locker <List <LockerKey> > CreateAndSaveLockerWithOneKey(string lockerPath, string password, byte[] salt, string key, string value) { AESEncryptor encryptor = new AESEncryptor(salt, 1234); GenericBinarySerializer <List <LockerKey> > serializer = new GenericBinarySerializer <List <LockerKey> >(); var keyLocker = new Locker <List <LockerKey> >(encryptor, serializer, password); keyLocker.Keys.Add(new LockerKey { Key = key, Value = value }); keyLocker.Save(lockerPath); return(keyLocker); }
public static int CreateFile(CreateArguments arguments) { int result = 0; IEncryptor encryptor = new AESEncryptor(arguments.Salt.ToBytes(), arguments.Iterations); GenericBinarySerializer <Dictionary <string, string> > serializer = new GenericBinarySerializer <Dictionary <string, string> >(); Locker locker = new Locker(encryptor, serializer, arguments.Password, arguments.LockerPath); // If ia key name is passed in add the kay to the keys list before saving if (String.IsNullOrWhiteSpace(arguments.Key) == false) { locker.Keys.Add(arguments.Key, arguments.Value); } locker.Save(); return(result); }
public void LoadModel(string path) { model = null; try { var binarySerializer = new GenericBinarySerializer(); model = binarySerializer.Deserialize <IPredictorModel <double> >( () => new StreamReader(root + path)); } catch (Exception e) { Debug.Log(e.Message); model = null; } }
public void GenericBinarySerializer_Serialize() { //Arrange Dictionary <string, string> dictionary = new Dictionary <string, string>(); dictionary.Add("first", "one"); string expectedResult = "AAEAAAD/////AQAAAAAAAAAEAQAAAOIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlvbmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBD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bj1iNzdhNWM1NjE5MzRlMDg5XSxbU3lzdGVtLlN0cmluZywgbXNjb3JsaWIsIFZlcnNpb249NC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5XV0E/P///+QBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuS2V5VmFsdWVQYWlyYDJbW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV0sW1N5c3RlbS5TdHJpbmcsIG1zY29ybGliLCBWZXJzaW9uPTQuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49Yjc3YTVjNTYxOTM0ZTA4OV1dAgAAAANrZXkFdmFsdWUBAQYFAAAABWZpcnN0BgYAAAADb25lCw=="; //Act ISerializer <Dictionary <string, string> > serializer = new GenericBinarySerializer <Dictionary <string, string> >(); string result = Convert.ToBase64String(serializer.Serialize(dictionary)); //Assert Assert.AreEqual(expectedResult, result, "Was expecting results to be equal"); }
public void GenericBinarySerializer_DeSerialize_Null() { //Arrange Exception exception = null; //Act ISerializer <Dictionary <string, string> > serializer = new GenericBinarySerializer <Dictionary <string, string> >(); try { Dictionary <string, string> result = serializer.DeSerialize(null); } catch (ArgumentNullException e) { exception = e; } //Assert Assert.IsNotNull(exception, "Was expecting an ArgumentNullException exception"); }
public void Locker_RoundTrip() { //Arrange string expectedFilePath = Path.Combine(TestContext.DeploymentDirectory, "rttestlocker.bin"); string passowrd = "password"; Exception exception = null; byte[] salt = { 1, 2, 3, 4, 5, 6, 7, 8 }; AESEncryptor encryptor = new AESEncryptor(salt, 1234); GenericBinarySerializer <List <KeyValuePair <string, string> > > serializer = new GenericBinarySerializer <List <KeyValuePair <string, string> > >(); Locker <List <KeyValuePair <string, string> > > openLocker = null; //Act Locker <List <KeyValuePair <string, string> > > saveLocker = new Locker <List <KeyValuePair <string, string> > >(encryptor, serializer, passowrd); try { saveLocker.Keys.Add(new KeyValuePair <string, string>("first", "one")); saveLocker.Save(expectedFilePath); openLocker = new Locker <List <KeyValuePair <string, string> > >(encryptor, serializer, passowrd); openLocker.Open(expectedFilePath); } catch (Exception e) { exception = e; } //Assert Assert.IsNull(exception, $"Was not expecting an exception [{exception?.Message}]"); bool fileExists = File.Exists(expectedFilePath); Assert.IsTrue(fileExists, "Was expecting a locker file to exist"); Assert.IsNotNull(openLocker, "Was expecting openlocker to have a value"); Assert.IsNotNull(openLocker.Keys); Assert.AreEqual(1, openLocker.Keys.Count, "was expecting 1 key"); var key = openLocker.Keys.FirstOrDefault(k => k.Key == "first"); Assert.AreEqual("one", key.Value, "Mismatched first value"); }
public void Serialize_Encrypt_RoundTrip() { List <KeyValuePair <string, string> > dictionary = new List <KeyValuePair <string, string> >(); dictionary.Add(new KeyValuePair <string, string>("first", "one")); string key = "12345678901234567890123456789012"; byte[] salt = { 1, 2, 3, 4, 5, 6, 7, 8 }; GenericBinarySerializer <List <KeyValuePair <string, string> > > serializer = new GenericBinarySerializer <List <KeyValuePair <string, string> > >(); AESEncryptor encryptor = new AESEncryptor(salt, 1234); //Act byte[] serializedData = serializer.Serialize(dictionary); byte[] encryptedData = encryptor.Encrypt(serializedData, key); byte[] decryptedData = encryptor.Decrypt(encryptedData, key); List <KeyValuePair <string, string> > deserializedData = serializer.DeSerialize(decryptedData); Assert.IsTrue(serializedData.AreEqual(decryptedData), "Was expecting serialized and decrypted data to be equal"); Assert.AreEqual(dictionary.Count, deserializedData.Count, "Was expecting dictionaries to have 1 entry"); }
public void RandomForest_Default_Parameters_Save_Load_Model_Using_Serializer() { #region read and split data // Use StreamReader(filepath) when running from filesystem var parser = new CsvParser(() => new StringReader(Resources.winequality_white)); var targetName = "quality"; // read feature matrix var observations = parser.EnumerateRows(c => c != targetName) .ToF64Matrix(); // read regression targets var targets = parser.EnumerateRows(targetName) .ToF64Vector(); // creates training test splitter, // Since this is a regression problem, we use the random training/test set splitter. // 30 % of the data is used for the test set. var splitter = new RandomTrainingTestIndexSplitter <double>(trainingPercentage: 0.7, seed: 24); var trainingTestSplit = splitter.SplitSet(observations, targets); var trainSet = trainingTestSplit.TrainingSet; var testSet = trainingTestSplit.TestSet; #endregion // create learner with default parameters var learner = new RegressionRandomForestLearner(trees: 100); // learn model with found parameters var model = learner.Learn(trainSet.Observations, trainSet.Targets); // predict the training and test set. var trainPredictions = model.Predict(trainSet.Observations); var testPredictions = model.Predict(testSet.Observations); // since this is a regression problem we are using square error as metric // for evaluating how well the model performs. var metric = new MeanSquaredErrorRegressionMetric(); // measure the error on training and test set. var trainError = metric.Error(trainSet.Targets, trainPredictions); var testError = metric.Error(testSet.Targets, testPredictions); TraceTrainingAndTestError(trainError, testError); //Save/load model as xml, in the file system use new StreamWriter(filePath); var xmlSerializer = new GenericXmlDataContractSerializer(); var savedModelXml = new StringWriter(); xmlSerializer .Serialize <IPredictorModel <double> >(model, () => savedModelXml); var loadedModelXml = xmlSerializer .Deserialize <IPredictorModel <double> >(() => new StringReader(savedModelXml.ToString())); //Save/load model as binary, in the file system use new StreamWriter(filePath); var binarySerializer = new GenericBinarySerializer(); var savedModelBinary = new StringWriter(); binarySerializer .Serialize <IPredictorModel <double> >(model, () => savedModelBinary); var loadedModelBinary = binarySerializer .Deserialize <IPredictorModel <double> >(() => new StringReader(savedModelBinary.ToString())); }