internal GeneratePortNumberConfig(string variableName, string dataType, int fallback, int low, int high)
        {
            DataType     = dataType;
            VariableName = variableName;
            int startPort = CryptoRandom.NextInt(low, high);

            for (int testPort = startPort; testPort <= high; testPort++)
            {
                if (TryAllocatePort(testPort, out Socket testSocket))
                {
                    Socket = testSocket;
                    Port   = ((IPEndPoint)Socket.LocalEndPoint).Port;
                    return;
                }
            }

            for (int testPort = low; testPort < startPort; testPort++)
            {
                if (TryAllocatePort(testPort, out Socket testSocket))
                {
                    Socket = testSocket;
                    Port   = ((IPEndPoint)Socket.LocalEndPoint).Port;
                    return;
                }
            }

            Port = fallback;
        }
        public void EncryptedDataWithEmbededIvValidationIsValid()
        {
            using (var cr = new CryptoRandom())
            {
                for (int i = 0; i < 5; i++)
                {
                    byte[] key       = cr.NextBytes(32);
                    byte[] plainText = cr.NextBytes((uint)cr.NextInt(14, 98));

                    byte[]           encrypted = AesEncryption.EncryptAndEmbedIv(plainText, key);
                    ValidationResult result    = AesEncryption.ValidateEncryptedDataWithEmbededIv(encrypted, key);
                    Assert.True(result.IsValid);
                }
            }
        }
Beispiel #3
0
        public void EncryptedDataWithAdditionalDataCanBeDecrypted()
        {
            for (int i = 0; i < NumberOrTestRuns; i++)
            {
                using (CryptoRandom cr = new CryptoRandom())
                {
                    string password  = Guid.NewGuid().ToString();
                    byte[] plainText = cr.NextBytes((uint)cr.NextInt(44, 444));
                    byte[] encrypted = AesEncryption.EncryptWithPassword(plainText, password);
                    byte[] encryptedWithAdditionalData = AesEncryptionAdditionalData.AddAdditionalData(encrypted, additionalData);

                    byte[] decrypted = AesEncryption.DecryptWithPassword(encrypted, password);
                    Assert.Equal(Convert.ToBase64String(plainText), Convert.ToBase64String(decrypted));
                }
            }
        }
        public void EncryptedDataWithEmbededIvFailsKcvWhenKeyIsAltered()
        {
            using (var cr = new CryptoRandom())
            {
                for (int i = 0; i < 5; i++)
                {
                    byte[] key       = cr.NextBytes(32);
                    byte[] plainText = cr.NextBytes((uint)cr.NextInt(14, 98));

                    byte[] encrypted = AesEncryption.EncryptAndEmbedIv(plainText, key);
                    AlterData(key, 0);

                    ValidationResult result = AesEncryption.ValidateEncryptedDataWithEmbededIv(encrypted, key);
                    Assert.False(result.IsValid);
                    Assert.False(result.KeyIsValid);
                    Assert.Equal(result.ErrorType.Value, DataValidationErrors.KeyCheckValueValidationError);
                }
            }
        }
        public void EncryptedWithEmbeddedIvCanBeDecrypted()
        {
            using (var cr = new CryptoRandom())
            {
                for (int i = 0; i < 5; i++)
                {
                    byte[] key       = cr.NextBytes(32);
                    byte[] plainText = cr.NextBytes((uint)cr.NextInt(4 * 1025, 8 * 1025));

                    byte[] encrypted = AesEncryption.EncryptAndEmbedIv(plainText, key);
                    byte[] decrypted = AesEncryption.DecryptWithEmbeddedIv(encrypted, key);

                    string plainString     = Convert.ToBase64String(plainText);
                    string decryptedString = Convert.ToBase64String(decrypted);

                    Assert.Equal(plainString, decryptedString);
                }
            }
        }
        public void EncryptedWithPasswordCanBeDecrypted()
        {
            using (var cr = new CryptoRandom())
            {
                for (int i = 0; i < 5; i++)
                {
                    string password  = PasswordGenerator.GenerateStatic();
                    byte[] plainText = cr.NextBytes((uint)cr.NextInt(4 * 1025, 8 * 1025));

                    byte[] encrypted = AesEncryption.EncryptWithPassword(plainText, password);
                    byte[] decrypted = AesEncryption.DecryptWithPassword(encrypted, password);

                    string plainString     = Convert.ToBase64String(plainText);
                    string decryptedString = Convert.ToBase64String(decrypted);

                    Assert.Equal(plainString, decryptedString);
                }
            }
        }
        public void EncryptedDataValidationFailsKcvOnChangedKey()
        {
            using (var cr = new CryptoRandom())
            {
                for (int i = 0; i < 5; i++)
                {
                    byte[] key       = cr.NextBytes(32);
                    byte[] iv        = cr.NextBytes(16);
                    byte[] plainText = cr.NextBytes((uint)cr.NextInt(14, 98));

                    byte[] encrypted = AesEncryption.Encrypt(plainText, key, iv);
                    AlterData(key, 0); // change key

                    ValidationResult result = AesEncryption.ValidateEncryptedData(encrypted, key, iv);
                    Assert.False(result.IsValid);
                    Assert.False(result.KeyIsValid);
                    Assert.Equal(result.ErrorType.Value, DataValidationErrors.KeyCheckValueValidationError);
                }
            }
        }
        public void EncryptedDataWithAlteredMacWillFailMacValidation()
        {
            using (var cr = new CryptoRandom())
            {
                for (int i = 0; i < 5; i++)
                {
                    byte[] key       = cr.NextBytes(32);
                    byte[] iv        = cr.NextBytes(16);
                    byte[] plainText = cr.NextBytes((uint)cr.NextInt(14, 98));

                    byte[] encrypted = AesEncryption.Encrypt(plainText, key, iv);
                    AlterData(encrypted, 76); // alter MAC

                    ValidationResult result = AesEncryption.ValidateEncryptedData(encrypted, key, iv);
                    Assert.False(result.IsValid);
                    Assert.False(result.DataIntegrityIsValid);
                    Assert.Equal(result.ErrorType.Value, DataValidationErrors.DataIntegrityValidationError);
                }
            }
        }
        private void TestEncryptDecrypt(uint keySize)
        {
            using (var cr = new CryptoRandom())
            {
                for (int i = 0; i < 5; i++)
                {
                    byte[] key       = cr.NextBytes(keySize);
                    byte[] iv        = cr.NextBytes(16);
                    byte[] plainText = cr.NextBytes((uint)cr.NextInt(1, 2455));

                    byte[] encrypted = AesEncryption.Encrypt(plainText, key, iv);
                    byte[] decrypted = AesEncryption.Decrypt(encrypted, key, iv);

                    string plainString     = Convert.ToBase64String(plainText);
                    string decryptedString = Convert.ToBase64String(decrypted);

                    Assert.Equal(plainString, decryptedString);
                }
            }
        }
Beispiel #10
0
        public void EvaluateConfig(IEngineEnvironmentSettings environmentSettings, IVariableCollection vars, IMacroConfig rawConfig, IParameterSet parameters, ParameterSetter setter)
        {
            RandomMacroConfig config = rawConfig as RandomMacroConfig;

            if (config == null)
            {
                throw new InvalidCastException("Couldn't cast the rawConfig as RandomMacroConfig");
            }

            int value = CryptoRandom.NextInt(config.Low, config.High);

            Parameter p;

            if (parameters.TryGetParameterDefinition(config.VariableName, out ITemplateParameter existingParam))
            {
                // If there is an existing parameter with this name, it must be reused so it can be referenced by name
                // for other processing, for example: if the parameter had value forms defined for creating variants.
                // When the param already exists, use its definition, but set IsVariable = true for consistency.
                p            = (Parameter)existingParam;
                p.IsVariable = true;

                if (string.IsNullOrEmpty(p.DataType))
                {
                    p.DataType = config.DataType;
                }
            }
            else
            {
                p = new Parameter
                {
                    IsVariable = true,
                    Name       = config.VariableName,
                    DataType   = config.DataType
                };
            }

            vars[config.VariableName] = value.ToString();
            setter(p, value.ToString());
        }
Beispiel #11
0
        private Card ChooseLand()
        {
            // TODO : bilands, lands that enters the battlefield tapped,...
            var lands = Player.Hand.Lands().ToList();

            if (!lands.Any())
            {
                // 1. No land...
                return(null);
            }
            else if (lands.Count == 1)
            {
                // 2. Only one land in hand :
                return(lands[0]);
            }
            else if (lands.Select(c => c.Name).Distinct().Count() == 1)
            {
                // 3. Same land :
                return(lands[0]);
            }
            else
            {
                // 4. Choose the best land to play :
                // 4.1. Be able to cast a card in Hand :
                var totalManaAvailable = Player.GetAvailableMana();
                var sortedCards        = _CardsToPlayFirstQuery()
                                         .Where(c => !totalManaAvailable.IsEnoughFor(c.TypedManaCost))
                                         .ToList();
                var potentialCardsToCastByLand = new Dictionary <Card, List <Card> >();
                foreach (var land in lands)
                {
                    var potentialManaAvailable = totalManaAvailable.Clone();
                    potentialManaAvailable.Add(land?.GetAbility <ManaActivatedAbility>()?.GetEffect <AddToManaPoolEffect>()?.AvailableMana);
                    var potentialCardsToPlay = _CardsToPlayFirstQuery().Where(c => totalManaAvailable.IsEnoughFor(c.TypedManaCost)).ToList();
                    if (potentialCardsToPlay.Any())
                    {
                        if (!potentialCardsToCastByLand.ContainsKey(land))
                        {
                            potentialCardsToCastByLand.Add(land, potentialCardsToPlay);
                        }
                        else
                        {
                            potentialCardsToCastByLand[land].AddRange(potentialCardsToPlay);
                        }
                    }
                }

                Card bestCardToPlay = null;
                Card bestLandToPlay = null;
                foreach (var kv in potentialCardsToCastByLand)
                {
                    var bestCardToPlayForLand = kv.Value.OrderByDescending(c => c.Cmc).First();
                    if (bestCardToPlay == null)
                    {
                        bestCardToPlay = bestCardToPlayForLand;
                        bestLandToPlay = kv.Key;
                    }
                    else if (bestCardToPlayForLand.Cmc > bestCardToPlay.Cmc)
                    {
                        bestCardToPlay = bestCardToPlayForLand;
                        bestLandToPlay = kv.Key;
                    }
                    else if (bestCardToPlayForLand.Cmc == bestCardToPlay.Cmc)
                    {
                        // Choose the one with ramp or draw card ability :
                        if (bestCardToPlayForLand.HasAbility <ManaActivatedAbility>() ||
                            bestCardToPlayForLand.HasAbility <DrawCardActivatedAbility>())
                        {
                            bestCardToPlay = bestCardToPlayForLand;
                            bestLandToPlay = kv.Key;
                        }
                        // TODO : smarten this part...
                    }
                }

                // 4.2. No card can be cast with a land from hand,
                // choose a random land :
                if (bestLandToPlay == null)
                {
                    var landIndex = CryptoRandom.NextInt(0, lands.Count - 1);
                    bestLandToPlay = lands[landIndex];
                }

                return(bestLandToPlay);
            }
        }