Beispiel #1
0
        public void TestAnonymizeEnergiemengeEncryptionRoundtrip()
        {
            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,
                        Obiskennzahl             = "1-2-3-4",
                        Einheit = BO4E.ENUM.Mengeneinheit.KWH
                    }
                }
            };

            //ENCRYPTION
            var encConf = new AnonymizerConfiguration();

            encConf.SetOption(DataCategory.POD, AnonymizerApproach.ENCRYPT);
            Energiemenge encryptedEm;

            using (Anonymizer anonymizer = new Anonymizer(encConf))
            {
                X509Certificate2 x509certPubl = new X509Certificate2(X509Certificate2.CreateFromCertFile("anonymizerTests/certificates/publicX509Cert.crt"));
                anonymizer.SetPublicKey(x509certPubl);
                encryptedEm = anonymizer.ApplyOperations <Energiemenge>(em);
            }

            //DECRYPTION
            var decConf = new AnonymizerConfiguration();

            decConf.SetOption(DataCategory.POD, AnonymizerApproach.DECRYPT);
            Energiemenge decryptedEm;

            using (Anonymizer decryptingAnonymizer = new Anonymizer(decConf))
            {
                AsymmetricCipherKeyPair keyPair;
                using (var reader = File.OpenText(@"anonymizerTests/certificates/privateKey.pem")) // file containing RSA PKCS1 private key
                {
                    keyPair = (AsymmetricCipherKeyPair) new PemReader(reader).ReadObject();
                }
                decryptingAnonymizer.SetPrivateKey(keyPair.Private);
                decryptedEm = decryptingAnonymizer.ApplyOperations <Energiemenge>(encryptedEm);
            }
            Assert.AreEqual(em.LokationsId, decryptedEm.LokationsId);
            Assert.IsFalse(Anonymizer.HasHashedKey(em));
        }
Beispiel #2
0
        public void TestAnonymizeEnergiemengeHashing()
        {
            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
                    }
                }
            };
            var conf = new AnonymizerConfiguration();

            conf.SetOption(DataCategory.POD, AnonymizerApproach.HASH);
            conf.SetOption(DataCategory.USER_PROPERTIES, AnonymizerApproach.HASH);
            Anonymizer anonymizer = new Anonymizer(conf);
            var        verbrauch2 = JsonConvert.DeserializeObject <Verbrauch>("{\"zw\":\"000000000000485549\",\"startdatum\":\"2018-03-24T01:45:00Z\",\"enddatum\":\"2018-03-24T02:00:00Z\",\"wert\":\"59\",\"status\":\"IU012\",\"obiskennzahl\":\"1-1:2.29.0\",\"wertermittlungsverfahren\":\"MESSUNG\",\"einheit\":\"KWH\"}");

            em.Energieverbrauch.Add(verbrauch2);

            // hash everything
            var result = anonymizer.ApplyOperations <Energiemenge>(em);

            Assert.IsNotNull(result);
            Assert.AreNotEqual(em.LokationsId, result.LokationsId);
            Assert.IsTrue(Messlokation.ValidateId(result.LokationsId));
            Assert.AreEqual(em.Energieverbrauch.Count, result.Energieverbrauch.Count);
            Assert.IsNotNull(result.Energieverbrauch[1].UserProperties["zw"]);
            Assert.AreNotEqual(em.Energieverbrauch[1].UserProperties["zw"].Value <string>(), result.Energieverbrauch[1].UserProperties["zw"].Value <string>());
            Assert.IsTrue(Anonymizer.HasHashedKey(result));

            // do not hash zw user property
            conf.unaffectedUserProperties.Add("zw");
            result = anonymizer.ApplyOperations <Energiemenge>(em);
            Assert.IsNotNull(result);
            Assert.AreNotEqual(em.LokationsId, result.LokationsId);
            Assert.IsTrue(Messlokation.ValidateId(result.LokationsId));
            Assert.AreEqual(em.Energieverbrauch.Count, result.Energieverbrauch.Count);
            Assert.IsNotNull(result.Energieverbrauch[1].UserProperties["zw"]);
            Assert.AreEqual(em.Energieverbrauch[1].UserProperties["zw"].Value <string>(), result.Energieverbrauch[1].UserProperties["zw"].Value <string>());
            Assert.IsTrue(Anonymizer.HasHashedKey(result));
        }
Beispiel #3
0
        public void TestHashingDetectionForNonconformingString()
        {
            BO4E.StaticLogger.Logger = new Microsoft.Extensions.Logging.Debug.DebugLogger("Testlogger", (log, level) => { return(true); });
            Energiemenge em = new Energiemenge()
            {
                LokationsId      = "asdkasldkmaslkdmas", // not identifyable as lokationsId
                LokationsTyp     = BO4E.ENUM.Lokationstyp.MeLo,
                Energieverbrauch = new List <Verbrauch>()
            };

            Assert.IsFalse(Anonymizer.HasHashedKey(em));

            var hashConf = new AnonymizerConfiguration();

            hashConf.SetOption(DataCategory.POD, AnonymizerApproach.HASH);
            Energiemenge hashedEm;

            using (var a = new Anonymizer(hashConf))
            {
                hashedEm = a.ApplyOperations <Energiemenge>(em);
            }
            Assert.IsTrue(Anonymizer.HasHashedKey(hashedEm));
        }