Beispiel #1
0
        internal static PwgError Generate(out ProtectedString psOut,
                                          PwProfile pwProfile, CryptoRandomStream crsRandomSource)
        {
            psOut = ProtectedString.Empty;
            LinkedList <char> vGenerated = new LinkedList <char>();
            PwCharSet         pcsCurrent = new PwCharSet();
            PwCharSet         pcsCustom  = new PwCharSet();
            PwCharSet         pcsUsed    = new PwCharSet();
            bool bInCharSetDef           = false;

            string strPattern = ExpandPattern(pwProfile.Pattern);

            if (strPattern.Length == 0)
            {
                return(PwgError.Success);
            }

            CharStream csStream = new CharStream(strPattern);
            char       ch       = csStream.ReadChar();

            while (ch != char.MinValue)
            {
                pcsCurrent.Clear();

                bool bGenerateChar = false;

                if (ch == '\\')
                {
                    ch = csStream.ReadChar();
                    if (ch == char.MinValue)                    // Backslash at the end
                    {
                        vGenerated.AddLast('\\');
                        break;
                    }

                    if (bInCharSetDef)
                    {
                        pcsCustom.Add(ch);
                    }
                    else
                    {
                        vGenerated.AddLast(ch);
                        pcsUsed.Add(ch);
                    }
                }
                else if (ch == '^')
                {
                    ch = csStream.ReadChar();
                    if (ch == char.MinValue)                    // ^ at the end
                    {
                        vGenerated.AddLast('^');
                        break;
                    }

                    if (bInCharSetDef)
                    {
                        pcsCustom.Remove(ch);
                    }
                }
                else if (ch == '[')
                {
                    pcsCustom.Clear();
                    bInCharSetDef = true;
                }
                else if (ch == ']')
                {
                    pcsCurrent.Add(pcsCustom.ToString());

                    bInCharSetDef = false;
                    bGenerateChar = true;
                }
                else if (bInCharSetDef)
                {
                    if (pcsCustom.AddCharSet(ch) == false)
                    {
                        pcsCustom.Add(ch);
                    }
                }
                else if (pcsCurrent.AddCharSet(ch) == false)
                {
                    vGenerated.AddLast(ch);
                    pcsUsed.Add(ch);
                }
                else
                {
                    bGenerateChar = true;
                }

                if (bGenerateChar)
                {
                    PwGenerator.PrepareCharSet(pcsCurrent, pwProfile);

                    if (pwProfile.NoRepeatingCharacters)
                    {
                        pcsCurrent.Remove(pcsUsed.ToString());
                    }

                    char chGen = PwGenerator.GenerateCharacter(pwProfile,
                                                               pcsCurrent, crsRandomSource);

                    if (chGen == char.MinValue)
                    {
                        return(PwgError.TooFewCharacters);
                    }

                    vGenerated.AddLast(chGen);
                    pcsUsed.Add(chGen);
                }

                ch = csStream.ReadChar();
            }

            if (vGenerated.Count == 0)
            {
                return(PwgError.Success);
            }

            char[] vArray = new char[vGenerated.Count];
            vGenerated.CopyTo(vArray, 0);

            if (pwProfile.PatternPermutePassword)
            {
                PwGenerator.ShufflePassword(vArray, crsRandomSource);
            }

            byte[] pbUtf8 = StrUtil.Utf8.GetBytes(vArray);
            psOut = new ProtectedString(true, pbUtf8);
            MemUtil.ZeroByteArray(pbUtf8);
            Array.Clear(vArray, 0, vArray.Length);
            vGenerated.Clear();

            return(PwgError.Success);
        }
Beispiel #2
0
        public static PwProfile DeriveFromPassword(ProtectedString psPassword)
        {
            PwProfile pp = new PwProfile();

            Debug.Assert(psPassword != null); if (psPassword == null)
            {
                return(pp);
            }

            byte[] pbUtf8 = psPassword.ReadUtf8();
            char[] vChars = StrUtil.Utf8.GetChars(pbUtf8);

            pp.GeneratorType = PasswordGeneratorType.CharSet;
            pp.Length        = (uint)vChars.Length;

            PwCharSet pcs = pp.CharSet;

            pcs.Clear();

            foreach (char ch in vChars)
            {
                if ((ch >= 'A') && (ch <= 'Z'))
                {
                    pcs.Add(PwCharSet.UpperCase);
                }
                else if ((ch >= 'a') && (ch <= 'z'))
                {
                    pcs.Add(PwCharSet.LowerCase);
                }
                else if ((ch >= '0') && (ch <= '9'))
                {
                    pcs.Add(PwCharSet.Digits);
                }
                else if (PwCharSet.SpecialChars.IndexOf(ch) >= 0)
                {
                    pcs.Add(PwCharSet.SpecialChars);
                }
                else if (ch == ' ')
                {
                    pcs.Add(' ');
                }
                else if (ch == '-')
                {
                    pcs.Add('-');
                }
                else if (ch == '_')
                {
                    pcs.Add('_');
                }
                else if (PwCharSet.Brackets.IndexOf(ch) >= 0)
                {
                    pcs.Add(PwCharSet.Brackets);
                }
                else if (PwCharSet.HighAnsiChars.IndexOf(ch) >= 0)
                {
                    pcs.Add(PwCharSet.HighAnsiChars);
                }
                else
                {
                    pcs.Add(ch);
                }
            }

            Array.Clear(vChars, 0, vChars.Length);
            MemUtil.ZeroByteArray(pbUtf8);
            return(pp);
        }