Ejemplo n.º 1
0
        public virtual string GenerateRandomString(string originalValue, ObfuscationFormatOption arg)
        {
            originalValue.ThrowArgumentNullExceptionIfNull(nameof(originalValue));
            arg.ThrowArgumentNullExceptionIfNull(nameof(arg));

            var scramblerClient = new ScramblerClient <string>(new StringScrambler());
            int length          = 5;

            if (arg.Arguments.ContainsKey("length"))
            {
                if (int.TryParse(arg.Arguments["length"], out int result))
                {
                    length = result;
                }

                if (originalValue.Length > length)
                {
                    return(scramblerClient.ExecuteScramble(originalValue.Substring(0, length)));
                }

                return(scramblerClient.ExecuteScramble(originalValue.PadRight(length, 'A')));
            }

            return(scramblerClient.ExecuteScramble(originalValue));
        }
Ejemplo n.º 2
0
        public void ExecuteEmailScramblerExceptionTest()
        {
            IScrambler <string>      scrambler = new EmailScambler();
            ScramblerClient <string> client    = new ScramblerClient <string>(scrambler);

            client.ExecuteScramble("this is not an email");
        }
Ejemplo n.º 3
0
        public void ExecuteScramblerFactoryTest()
        {
            ScramblerClientFactory factory = new ScramblerClientFactory();

            ScramblerClient <String> strClient = factory.GetScrambler <string>();
            string inputStr  = "Hello world";
            string outputStr = strClient.ExecuteScramble(inputStr);

            Assert.AreNotEqual(inputStr, outputStr);

            ScramblerClient <int> intClient = factory.GetScrambler <int>();
            int inputInt  = 1;
            int outputInt = intClient.ExecuteScramble(inputInt, 2, 10);

            Assert.AreNotEqual(inputInt, outputInt);

            ScramblerClient <Guid> guidClient = factory.GetScrambler <Guid>();
            Guid inputGuid  = Guid.NewGuid();
            Guid outputGuid = guidClient.ExecuteScramble(inputGuid);

            Assert.AreNotEqual(inputGuid, outputGuid);

            ScramblerClient <double> doubleClient = factory.GetScrambler <double>();
            double inputDouble  = 1;
            double outputDouble = doubleClient.ExecuteScramble(inputDouble, 2, 10);

            Assert.AreNotEqual(inputDouble, outputDouble);

            ScramblerClient <decimal> decimalClient = factory.GetScrambler <decimal>();
            decimal inputDecimal  = 1;
            decimal outputDecimal = decimalClient.ExecuteScramble(inputDecimal, 2, 10);

            Assert.AreNotEqual(inputDecimal, outputDecimal);
        }
        public void HandleObfuscation(Entity entity, FieldToBeObfuscated field, IEntityMetadataCache metaData)
        {
            entity.ThrowArgumentNullExceptionIfNull(nameof(entity));
            field.ThrowArgumentNullExceptionIfNull(nameof(field));
            metaData.ThrowArgumentNullExceptionIfNull(nameof(metaData));

            // Get the min and maximum values for the field using the meta data cache
            DoubleAttributeMetadata doubleMetaData = (DoubleAttributeMetadata)metaData.GetAttribute(entity.LogicalName, field.FieldName);

            int min = (int)doubleMetaData.MinValue.GetValueOrDefault(0);
            int max = (int)doubleMetaData.MaxValue.GetValueOrDefault(10);

            if (field.CanBeFormatted)
            {
                Dictionary <string, object> metadataParameters = new Dictionary <string, object>
                {
                    { "min", min },
                    { "max", max }
                };

                entity[field.FieldName] = formattingClient.CreateFormattedValue((double)entity[field.FieldName], field, metadataParameters);
                return;
            }

            entity[field.FieldName] = doubleScramblerClient.ExecuteScramble((double)entity[field.FieldName], min, max);
        }
Ejemplo n.º 5
0
        public void ExecuteMetricsStringScramblerTest()
        {
            IScrambler <string>      scrambler = new StringScrambler();
            ScramblerClient <string> client    = new ScramblerClient <string>(scrambler);

            List <string> inputStrings = new List <string>();

            for (var i = 0; i <= 100000; i++)
            {
                inputStrings.Add($"Hello World {i}");
            }

            var watch = System.Diagnostics.Stopwatch.StartNew();

            foreach (string str in inputStrings)
            {
                FluentActions.Invoking(() => client.ExecuteScramble(str))
                .Should()
                .NotThrow();
            }

            watch.Stop();
            var elapsedSeconds = watch.ElapsedMilliseconds / 1000;

            Console.WriteLine($"Time taken: {elapsedSeconds}");
        }
        public void HandleObfuscation(Entity entity, FieldToBeObfuscated field, IEntityMetadataCache metaData)
        {
            // Get the min and maximum values for the field using the meta data cache
            IntegerAttributeMetadata intMetaData = (IntegerAttributeMetadata)metaData.GetAttribute(entity.LogicalName, field.FieldName);
            int min = intMetaData.MinValue.GetValueOrDefault(0);
            int max = intMetaData.MaxValue.GetValueOrDefault(10);

            entity[field.FieldName] = intScramblerClient.ExecuteScramble((int)entity[field.FieldName], min, max);
        }
Ejemplo n.º 7
0
        public void ExecuteDecimalScramblerTest()
        {
            IScrambler <decimal>      scrambler = new DecimalScrambler();
            ScramblerClient <decimal> client    = new ScramblerClient <decimal>(scrambler);

            decimal input  = 5.432M;
            decimal output = client.ExecuteScramble(input);

            Assert.AreNotEqual(input, output);
        }
Ejemplo n.º 8
0
        public void ExecuteDoubleScramblerTest()
        {
            IScrambler <double>      scrambler = new DoubleScrambler();
            ScramblerClient <double> client    = new ScramblerClient <double>(scrambler);

            double input  = 10.5493;
            double output = client.ExecuteScramble(input);

            Assert.AreNotEqual(input, output);
        }
Ejemplo n.º 9
0
        public void ExecuteGuidScramblerTest()
        {
            IScrambler <Guid>      scrambler = new GuidScrambler();
            ScramblerClient <Guid> client    = new ScramblerClient <Guid>(scrambler);

            Guid input  = Guid.NewGuid();
            Guid output = client.ExecuteScramble(input);

            Assert.AreNotEqual(input, output);
        }
Ejemplo n.º 10
0
        public void ExecuteIntegerScramblerTest()
        {
            IScrambler <int>      scrambler = new IntegerScrambler();
            ScramblerClient <int> client    = new ScramblerClient <int>(scrambler);

            int input  = 5;
            int output = client.ExecuteScramble(input, 0, 5);

            Assert.AreNotEqual(input, output);
        }
Ejemplo n.º 11
0
        public void ExecuteStringScramblerTest()
        {
            IScrambler <string>      scrambler = new StringScrambler();
            ScramblerClient <string> client    = new ScramblerClient <string>(scrambler);
            string input  = "Hello World";
            string output = client.ExecuteScramble(input);

            Assert.AreNotEqual(input, output);
            Assert.AreEqual(input.Length, output.Length);
        }
Ejemplo n.º 12
0
        public void ExecuteEmailScramblerTest()
        {
            IScrambler <string>      scrambler = new EmailScambler();
            ScramblerClient <string> client    = new ScramblerClient <string>(scrambler);

            string input  = "*****@*****.**";
            string output = client.ExecuteScramble(input);

            Assert.AreNotEqual(input, output);
            Assert.IsTrue(output.Contains("@"));
        }
        public void HandleObfuscation(Entity entity, FieldToBeObfuscated field, IEntityMetadataCache metaData)
        {
            entity.ThrowArgumentNullExceptionIfNull(nameof(entity));
            field.ThrowArgumentNullExceptionIfNull(nameof(field));
            metaData.ThrowArgumentNullExceptionIfNull(nameof(metaData));

            DecimalAttributeMetadata decimalMetaData = (DecimalAttributeMetadata)metaData.GetAttribute(entity.LogicalName, field.FieldName);
            int min = (int)decimalMetaData.MinValue.GetValueOrDefault(0);
            int max = (int)decimalMetaData.MaxValue.GetValueOrDefault(10);

            entity[field.FieldName] = decimalScramblerClient.ExecuteScramble((decimal)entity[field.FieldName], min, max);
        }
Ejemplo n.º 14
0
        public void ExecuteMetricsStringScramblerTest()
        {
            IScrambler <string>      scrambler = new StringScrambler();
            ScramblerClient <string> client    = new ScramblerClient <string>(scrambler);

            List <string> inputStrings = new List <string>();

            for (var i = 0; i <= 100000; i++)
            {
                inputStrings.Add(String.Format("Hello World" + i));
            }

            var watch = System.Diagnostics.Stopwatch.StartNew();

            foreach (string str in inputStrings)
            {
                client.ExecuteScramble(str);
            }
            watch.Stop();
            var elapsedSeconds = watch.ElapsedMilliseconds / 1000;

            Console.WriteLine(String.Format("Time taken: ", elapsedSeconds));
        }
        public void HandleObfuscation(Entity entity, FieldToBeObfuscated field, IEntityMetadataCache metaData)
        {
            entity.ThrowArgumentNullExceptionIfNull(nameof(entity));
            field.ThrowArgumentNullExceptionIfNull(nameof(field));
            metaData.ThrowArgumentNullExceptionIfNull(nameof(metaData));

            StringAttributeMetadata stringMetaData = (StringAttributeMetadata)metaData.GetAttribute(entity.LogicalName, field.FieldName);

            if (field.CanBeFormatted)
            {
                Dictionary <string, object> metadataParams = new Dictionary <string, object>();

                if (stringMetaData.MaxLength != null)
                {
                    metadataParams.Add("maxlength", stringMetaData.MaxLength);
                }

                entity[field.FieldName] = formattingClient.CreateFormattedValue((string)entity[field.FieldName], field, metadataParams);
                return;
            }

            entity[field.FieldName] = strScramblerClient.ExecuteScramble((string)entity[field.FieldName]);
        }