Beispiel #1
0
        public string Generate(SequenceItem item, CryptoRandomRange cryptoRandom)
        {
            if (item.Probability > PercentEnum.Never &&
                (int)cryptoRandom.GetRandomInRange(1, 100) <= (int)item.Probability)
            {
                var characters = item as CharacterSequenceItem;
                if (characters != null)
                {
                    return(new CharacterItemGenerator(_globalConfiguration)
                           .Generate(characters, cryptoRandom));
                }
                var words = item as WordSequenceItem;
                if (words != null)
                {
                    string word = string.Empty;
                    word = new WordItemGenerator(_globalConfiguration)
                           .Generate(words, cryptoRandom);

                    word = new ItemVisitor.CapitalizeVisitor().Visit(words, word, cryptoRandom);

                    word = new ItemVisitor.SubstitutionsVisitor(_globalConfiguration)
                           .Visit(words, word, cryptoRandom);

                    return(word);
                }
            }
            return(string.Empty);
        }
        public string Visit(WordSequenceItem item, string word, CryptoRandomRange cryptoRandom)
        {
            if (word == null || word.Length == 0)
            {
                return(word);
            }

            if (item.Capitalize == CapitalizeEnum.Proper)
            {
                word = word[0].ToString(CultureInfo.InvariantCulture).ToUpper() + word.Substring(1);
            }
            else if (item.Capitalize != CapitalizeEnum.Never)
            {
                string capitalizedWord = string.Empty;
                foreach (char c in word)
                {
                    if ((int)cryptoRandom.GetRandomInRange(1, 100) <= (int)item.Capitalize)
                    {
                        capitalizedWord += c.ToString(CultureInfo.InvariantCulture).ToUpper();
                    }
                    else
                    {
                        capitalizedWord += c.ToString(CultureInfo.InvariantCulture).ToLower();
                    }
                }
                word = capitalizedWord;
            }
            else
            {
                word = word.ToLower();
            }
            return(word);
        }
Beispiel #3
0
        public string Generate(CharacterSequenceItem item, CryptoRandomRange cryptoRandom)
        {
            string      targetCharacterSet = string.Empty;
            List <char> characterList      = null;
            uint        length             = item.Length;

            if (length > 0 &&
                item.LengthStrength != StrengthEnum.Full &&
                (int)cryptoRandom.GetRandomInRange(1, 100) <= (uint)item.LengthStrength)
            {
                length = (uint)cryptoRandom.GetRandomInRange(0, item.Length - 1);
            }

            while (targetCharacterSet.Length < length &&
                   (null == characterList || characterList.Count > 0))
            {
                if (characterList == null)
                {
                    characterList = new List <char>();
                    if (item.Characters != null)
                    {
                        characterList.AddRange(item.Characters);
                    }
                    if (item.Characters == null || !item.Characters.Override)
                    {
                        characterList.AddRange(_globalConfiguration.DefaultCharacters);
                    }
                }

                if (characterList.Count > 0)
                {
                    int charPos = (int)cryptoRandom.GetRandomInRange(0, (ulong)characterList.Count - 1);
                    targetCharacterSet += characterList[charPos];
                    if (!item.AllowDuplicate)
                    {
                        characterList.RemoveAt(charPos);
                    }
                }
            }

            return(targetCharacterSet);
        }
Beispiel #4
0
 public string Visit(WordSequenceItem item, string word, CryptoRandomRange cryptoRandom)
 {
     if (item.Substitution > PercentEnum.Never)
     {
         List <BaseSubstitution> applicableSubstitution = new List <BaseSubstitution>();
         if (item.Substitutions != null)
         {
             applicableSubstitution.AddRange(item.Substitutions);
         }
         if (item.Substitutions == null || !item.Substitutions.Override)
         {
             applicableSubstitution.AddRange(_globalConfiguration.DefaultSubstitutions);
         }
         foreach (BaseSubstitution substitution in applicableSubstitution)
         {
             word = new SubstitutionVisitor(cryptoRandom).ApplySubstitutionItem(substitution, word, (ulong)item.Substitution);
         }
     }
     return(word);
 }
Beispiel #5
0
        public string Generate(WordSequenceItem item, CryptoRandomRange cryptoRandom)
        {
            string targetWord = string.Empty;

            {
                List <string> wordList = new List <string>();
                if (item.Words != null)
                {
                    wordList.AddRange(item.Words);
                }
                if (item.Words == null || !item.Words.Override)
                {
                    wordList.AddRange(_globalConfiguration.DefaultWords);
                }

                if (wordList.Count > 0)
                {
                    targetWord = wordList[(int)cryptoRandom.GetRandomInRange(0, (ulong)wordList.Count - 1)];
                }
            }
            return(targetWord);
        }
 public SubstitutionVisitor(CryptoRandomRange cryptoRandom)
 {
     myCryptoRandom = cryptoRandom;
 }