public void ShowCaseTest() { Assert.IsTrue(em.IsValid()); BO4E.StaticLogger.Logger = new Microsoft.Extensions.Logging.Debug.DebugLogger("Testlogger", (log, level) => { return(true); }); // Image there is a service provider to analyse the verbrauchs data but he shouldn't know about the location data. // Yet it should still be possible to map the results back to my original data. So hashing seems like a good approach. var config = new AnonymizerConfiguration(); config.SetOption(BO4E.meta.DataCategory.POD, AnonymizerApproach.HASH); byte[] salt = new Byte[100]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetBytes(salt); config.hashingSalt = Convert.ToBase64String(salt); // Some random but not necessarily secret salt; Energiemenge anonymizedEm; using (Anonymizer anon = new Anonymizer(config)) { anonymizedEm = anon.ApplyOperations <Energiemenge>(em); } Debug.WriteLine($"No one knowing only {anonymizedEm.LokationsId} actually means {em.LokationsId}"); Assert.AreNotEqual(em.LokationsId, anonymizedEm.LokationsId); Debug.WriteLine($"But it won't cause any problems in satellite systems because the data is still there (!=null) and the business object is still valid."); Assert.IsNotNull(anonymizedEm.LokationsId); Assert.IsTrue(anonymizedEm.IsValid()); }
public void TestProtobufRoundTrip() { Energiemenge em = new Energiemenge() { LokationsId = "54321012345", LokationsTyp = BO4E.ENUM.Lokationstyp.MaLo, Energieverbrauch = new System.Collections.Generic.List <Verbrauch>() { new Verbrauch() { Einheit = BO4E.ENUM.Mengeneinheit.KWH, Wert = 10.0M, Startdatum = new DateTime(2019, 1, 1, 0, 0, 0, DateTimeKind.Utc), Enddatum = new DateTime(2019, 1, 2, 0, 0, 0, DateTimeKind.Utc), Obiskennzahl = "1�1.8.1" }, new Verbrauch() { Einheit = BO4E.ENUM.Mengeneinheit.MWH, Wert = 23.0M, Startdatum = new DateTime(2019, 1, 2, 0, 0, 0, DateTimeKind.Utc), Enddatum = new DateTime(2019, 1, 3, 0, 0, 0, DateTimeKind.Utc), Obiskennzahl = "1�1.8.1" } } }; Assert.IsTrue(em.IsValid(), "Must not serialize invalid Business Objects."); string emBase64; using (var stream = new MemoryStream()) { Serializer.Serialize <Energiemenge>(stream, em); using (var reader = new BinaryReader(stream)) { emBase64 = Convert.ToBase64String(stream.ToArray()); } } Assert.IsFalse(string.IsNullOrWhiteSpace(emBase64)); // now use base64 string to get back the original energiemenge Energiemenge emRoundTrip; using (var backStream = new MemoryStream(Convert.FromBase64String(emBase64))) { backStream.Seek(0, SeekOrigin.Begin); emRoundTrip = Serializer.Deserialize <Energiemenge>(backStream); } Assert.IsNotNull(emRoundTrip.LokationsId); Assert.IsTrue(emRoundTrip.IsValid()); Assert.AreEqual(em, emRoundTrip); }
public void TestSameHashDifferentObjectTypes() { BO4E.StaticLogger.Logger = new Microsoft.Extensions.Logging.Debug.DebugLogger("Testlogger", (log, level) => { return(true); }); Energiemenge em = new Energiemenge() { LokationsId = "DE0123456789012345678901234567890", LokationsTyp = BO4E.ENUM.Lokationstyp.MeLo, Energieverbrauch = new List <Verbrauch>() { new Verbrauch() { Wert = 123.456M, Wertermittlungsverfahren = BO4E.ENUM.Wertermittlungsverfahren.MESSUNG, Startdatum = new DateTime(2019, 1, 1, 0, 0, 0, DateTimeKind.Utc), Enddatum = new DateTime(2019, 2, 1, 0, 0, 0, DateTimeKind.Utc), Obiskennzahl = "1-2-3-4", Einheit = BO4E.ENUM.Mengeneinheit.KWH } } }; Assert.IsTrue(em.IsValid()); Messlokation melo = new Messlokation() { MesslokationsId = "DE0123456789012345678901234567890" }; Assert.IsTrue(melo.IsValid()); var conf = new AnonymizerConfiguration(); conf.SetOption(DataCategory.POD, AnonymizerApproach.HASH); using Anonymizer anonymizer = new Anonymizer(conf); var hashedEm = anonymizer.ApplyOperations <Energiemenge>(em); var hashedMelo = anonymizer.ApplyOperations <Messlokation>(melo); Assert.AreEqual(hashedEm.LokationsId, hashedMelo.MesslokationsId); }