Esempio n. 1
0
        internal static void PrepareCharSet(PwCharSet pwCharSet, PwProfile pwProfile)
        {
            pwCharSet.Remove(PwCharSet.Invalid);

            if (pwProfile.ExcludeLookAlike)
            {
                pwCharSet.Remove(PwCharSet.LookAlike);
            }

            if (pwProfile.ExcludeCharacters.Length > 0)
            {
                pwCharSet.Remove(pwProfile.ExcludeCharacters);
            }
        }
Esempio n. 2
0
 private void UpdateCharSet(bool bSetXml)
 {
     if (bSetXml)
     {
         PwCharSet pcs = new PwCharSet(passwordCharSet.ToString());
         characterSetRanges     = pcs.PackAndRemoveCharRanges();
         characterSetAdditional = pcs.ToString();
     }
     else
     {
         PwCharSet pcs = new PwCharSet(characterSetAdditional);
         pcs.UnpackCharRanges(characterSetRanges);
         passwordCharSet = pcs;
     }
 }
Esempio n. 3
0
        private void Initialize(bool bFullInitialize)
        {
            Clear();

            if (!bFullInitialize)
            {
                return;
            }

            if (m_strHighAnsi == null)
            {
                StringBuilder sbHighAnsi = new StringBuilder();
                // [U+0080, U+009F] are C1 control characters,
                // U+00A0 is non-breaking space
                for (char ch = '\u00A1'; ch <= '\u00AC'; ++ch)
                {
                    sbHighAnsi.Append(ch);
                }
                // U+00AD is soft hyphen (format character)
                for (char ch = '\u00AE'; ch < '\u00FF'; ++ch)
                {
                    sbHighAnsi.Append(ch);
                }
                sbHighAnsi.Append('\u00FF');

                m_strHighAnsi = sbHighAnsi.ToString();
            }

            if (m_strSpecial == null)
            {
                PwCharSet pcs = new PwCharSet(false);
                pcs.AddRange('!', '/');
                pcs.AddRange(':', '@');
                pcs.AddRange('[', '`');
                pcs.Add(@"|~");
                pcs.Remove(@"-_ ");
                pcs.Remove(PwCharSet.Brackets);

                m_strSpecial = pcs.ToString();
            }
        }
Esempio n. 4
0
        internal static char GenerateCharacter(PwProfile pwProfile,
                                               PwCharSet pwCharSet, CryptoRandomStream crsRandomSource)
        {
            if (pwCharSet.Size == 0)
            {
                return(char.MinValue);
            }

            var index = crsRandomSource.GetRandomUInt64();

            index %= pwCharSet.Size;

            var ch = pwCharSet[(uint)index];

            if (pwProfile.NoRepeatingCharacters)
            {
                pwCharSet.Remove(ch);
            }

            return(ch);
        }
Esempio n. 5
0
        public static ProtectedString Generate(
            int passwordLength,
            bool useUpperCase         = false,
            bool useDigits            = false,
            bool useSpecialCharacters = false)
        {
            ProtectedString psOut;
            var             pwCharSet = new PwCharSet(PwCharSet.LowerCase
                                                      + (useSpecialCharacters ? PwCharSet.SpecialChars : string.Empty)
                                                      + (useUpperCase ? PwCharSet.UpperCase : string.Empty)
                                                      + (useDigits ? PwCharSet.Digits : string.Empty));
            var pwProfile = new PwProfile {
                ExcludeLookAlike = true, Length = (uint)passwordLength, CharSet = pwCharSet
            };

            Debug.Assert(pwProfile != null);

            var crs = CreateCryptoStream(GetEntropyBytes());
            var e   = PwgError.Success;

            if (pwProfile.GeneratorType == PasswordGeneratorType.CharSet)
            {
                e = CharSetBasedGenerator.Generate(out psOut, pwProfile, crs);
            }
            else if (pwProfile.GeneratorType == PasswordGeneratorType.Pattern)
            {
                e = PatternBasedGenerator.Generate(out psOut, pwProfile, crs);
            }
            else
            {
                psOut = ProtectedString.Empty;
            }

            if (e != PwgError.Success)
            {
                throw new Exception("Password generation failed");
            }

            return(psOut);
        }
        internal static PwgError Generate(out ProtectedString psOut,
                                          PwProfile pwProfile, CryptoRandomStream crsRandomSource)
        {
            psOut = ProtectedString.Empty;
            if (pwProfile.Length == 0)
            {
                return(PwgError.Success);
            }

            PwCharSet pcs = new PwCharSet(pwProfile.CharSet.ToString());

            char[] vGenerated = new char[pwProfile.Length];

            PwGenerator.PrepareCharSet(pcs, pwProfile);

            for (int nIndex = 0; nIndex < (int)pwProfile.Length; ++nIndex)
            {
                char ch = PwGenerator.GenerateCharacter(pwProfile, pcs,
                                                        crsRandomSource);

                if (ch == char.MinValue)
                {
                    Array.Clear(vGenerated, 0, vGenerated.Length);
                    return(PwgError.TooFewCharacters);
                }

                vGenerated[nIndex] = ch;
            }

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

            return(PwgError.Success);
        }
        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);
        }