/// <summary>
 /// Calculates the number of possible combinations of phrases based on the current dictionary and given phrase strength.
 /// </summary>
 public PhraseCombinations CalculateCombinations(PhraseStrength strength)
 {
     if (Clause.RandomMappings.ContainsKey(strength))
     {
         return(this.CalculateCombinations(Clause.RandomMappings[strength]));
     }
     else
     {
         return(this.CalculateCombinations(Clause.CreatePhraseDescription(strength, this.Randomness)));
     }
 }
コード例 #2
0
 private IEnumerable <Clause> GetPhraseDescription()
 {
     if (this.PhraseStrength != PhraseStrength.Custom && !Clause.RandomMappings.ContainsKey(this.PhraseStrength))
     {
         return(Clause.CreatePhraseDescription(this.PhraseStrength, new KeePassRandomSource()));
     }
     else
     {
         return(Enumerable.Empty <Clause>());
     }
 }
        /// <summary>
        /// Generates a single phrase based on a randomly selected phrase strength in a UTF8 <c>byte[]</c>.
        /// This is slightly slower than <c>Generate()</c> and allows deterministic destruction of the data, but is still unencrypted.
        /// </summary>
        /// <param name="strengths">A collection of the predefined <c>PhraseStrength</c> enumeration members to choose between at random.</param>
        /// <param name="wordDelimiter">The string to place between each word in the passphrase.</param>
        public byte[] GenerateAsUtf8Bytes(IEnumerable <PhraseStrength> strengths, string wordDelimiter)
        {
            _ = strengths ?? throw new ArgumentNullException(nameof(strengths));

            if (strengths.Any(s => Clause.RandomMappings.ContainsKey(s) || s == PhraseStrength.Custom))
            {
                throw new ArgumentException("Random or Custom phrase strengths must be passed to the singular version.");
            }
            var strength = this.ChooseAtRandom(strengths);

            return(GenerateAsUtf8Bytes(Clause.CreatePhraseDescription(strength, Randomness), wordDelimiter));
        }
        /// <summary>
        /// Generates a single phrase as a <c>SecureString</c> based on the given phrase strength.
        /// This is the slowest and most secure method.
        /// </summary>
        /// <param name="strengths">A collection of the predefined <c>PhraseStrength</c> enumeration members to choose between at random.</param>
        /// <param name="includeSpacesBetweenWords">Include spaces between words (defaults to true).</param>
        public SecureString GenerateAsSecure(IEnumerable <PhraseStrength> strengths, bool includeSpacesBetweenWords)
        {
            _ = strengths ?? throw new ArgumentNullException(nameof(strengths));

            if (strengths.Any(s => Clause.RandomMappings.ContainsKey(s) || s == PhraseStrength.Custom))
            {
                throw new ArgumentException("Random or Custom phrase strengths must be passed to the singular version.");
            }
            var strength = this.ChooseAtRandom(strengths);

            return(GenerateAsSecure(Clause.CreatePhraseDescription(strength, this.Randomness), includeSpacesBetweenWords));
        }
コード例 #5
0
        private static void TestTextualParsing(ReadablePassphraseGenerator generator, PhraseStrength strength)
        {
            Console.WriteLine("Testing parsing of textual phrase strength {0}...", strength);
            var description = Clause.CreatePhraseDescription(strength, generator.Randomness);
            var sb          = new StringBuilder();

            foreach (var c in description)
            {
                c.ToStringBuilder(sb);
            }
            var textualDescription = sb.ToString();

            Console.Write("    Generation OK.");
            var customStrength = Clause.CreateCollectionFromTextString(textualDescription);

            Console.Write(" Parsing OK.");
            generator.Generate(customStrength);
            Console.WriteLine(" Passphrase OK.");
        }
        /// <summary>
        /// Calculates the number of possible combinations of phrases based on the current dictionary and randomly choosing between the given phrase strengths.
        /// </summary>
        public PhraseCombinations CalculateCombinations(IEnumerable <PhraseStrength> strengths)
        {
            _ = strengths ?? throw new ArgumentNullException(nameof(strengths));

            if (strengths.Any(s => Clause.RandomMappings.ContainsKey(s) || s == PhraseStrength.Custom))
            {
                throw new ArgumentException("Random or Custom phrase strengths must be passed to the singular version.");
            }

            // Check all strengths and report min / max.
            // Avg is somewhat meaningless, but we average the log of it anyway.
            double min = Double.MaxValue, max = 0.0, acc = 0.0;

            foreach (var s in strengths)
            {
                var comb = this.CalculateCombinations(Clause.CreatePhraseDescription(s, this.Randomness));
                min  = Math.Min(min, comb.Shortest);
                max += comb.Longest;
                acc += comb.OptionalAverageAsEntropyBits;       // Max adds because of variations between phrases.
            }
            return(new PhraseCombinations(min, max, Math.Pow(2, acc / strengths.Count())));
        }
 /// <summary>
 /// Generates a single phrase based on the given phrase strength in a UTF8 <c>byte[]</c>.
 /// This is slightly slower than <c>Generate()</c> and allows deterministic destruction of the data, but is still unencrypted.
 /// </summary>
 /// <param name="strength">One of the predefined <c>PhraseStrength</c> enumeration members.</param>
 /// <param name="wordDelimiter">The string to place between each word in the passphrase.</param>
 public byte[] GenerateAsUtf8Bytes(PhraseStrength strength, string wordDelimiter)
 {
     return(GenerateAsUtf8Bytes(Clause.CreatePhraseDescription(strength, Randomness), wordDelimiter));
 }
 /// <summary>
 /// Generates a single phrase based on the given phrase strength in a UTF8 <c>byte[]</c>.
 /// This is slightly slower than <c>Generate()</c> and allows deterministic destruction of the data, but is still unencrypted.
 /// </summary>
 /// <param name="strength">One of the predefined <c>PhraseStrength</c> enumeration members.</param>
 /// <param name="includeSpacesBetweenWords">Include spaces between words (defaults to true).</param>
 public byte[] GenerateAsUtf8Bytes(PhraseStrength strength, bool includeSpacesBetweenWords)
 {
     return(GenerateAsUtf8Bytes(Clause.CreatePhraseDescription(strength, Randomness), includeSpacesBetweenWords));
 }
 /// <summary>
 /// Generates a single phrase based on the given phrase strength in a UTF8 <c>byte[]</c>.
 /// This is slightly slower than <c>Generate()</c> and allows deterministic destruction of the data, but is still unencrypted.
 /// </summary>
 public byte[] GenerateAsUtf8Bytes(PhraseStrength strength)
 {
     return(GenerateAsUtf8Bytes(Clause.CreatePhraseDescription(strength, Randomness), " "));
 }
 /// <summary>
 /// Generates a single phrase based on the given phrase strength in a <c>StringBuilder</c>.
 /// This is the fastest and least secure method.
 /// </summary>
 /// <param name="strength">One of the predefined <c>PhraseStrength</c> enumeration members (default: Random).</param>
 /// <param name="wordDelimiter">The string to place between each word in the passphrase (default: single space).</param>
 /// /// <param name="mutators">Applies one or more mutators to the passphrase after it is generated (default: none).</param>
 public String Generate(PhraseStrength strength = PhraseStrength.Random, string wordDelimiter = " ", IEnumerable <IMutator>?mutators = null)
 {
     return(Generate(Clause.CreatePhraseDescription(strength, Randomness), wordDelimiter, mutators));
 }
 /// <summary>
 /// Generates a single phrase as a <c>SecureString</c> based on the given phrase strength.
 /// This is the slowest and most secure method.
 /// </summary>
 /// <param name="strength">One of the predefined <c>PhraseStrength</c> enumeration members.</param>
 /// <param name="wordDelimiter">The string to place between each word in the passphrase.</param>
 public SecureString GenerateAsSecure(PhraseStrength strength, string wordDelimiter)
 {
     return(GenerateAsSecure(Clause.CreatePhraseDescription(strength, Randomness), wordDelimiter));
 }
 /// <summary>
 /// Generates a single phrase as a <c>SecureString</c> based on the given phrase strength.
 /// This is the slowest and most secure method.
 /// </summary>
 /// <param name="strength">One of the predefined <c>PhraseStrength</c> enumeration members.</param>
 /// <param name="includeSpacesBetweenWords">Include spaces between words (defaults to true).</param>
 public SecureString GenerateAsSecure(PhraseStrength strength, bool includeSpacesBetweenWords)
 {
     return(GenerateAsSecure(Clause.CreatePhraseDescription(strength, Randomness), includeSpacesBetweenWords));
 }
 /// <summary>
 /// Generates a single phrase as a <c>SecureString</c> based on the given phrase strength.
 /// This is the slowest and most secure method.
 /// </summary>
 public SecureString GenerateAsSecure(PhraseStrength strength)
 {
     return(GenerateAsSecure(Clause.CreatePhraseDescription(strength, Randomness), " "));
 }