Esempio n. 1
0
        private void Initialize(bool bFullInitialize)
        {
            this.Clear();

            if (bFullInitialize == false)
            {
                return;
            }

            StringBuilder sbHighAnsi = new StringBuilder();

            for (char ch = '~'; ch < 255; ++ch)
            {
                sbHighAnsi.Append(ch);
            }
            m_strHighAnsi = sbHighAnsi.ToString();

            PwCharSet pcs = new PwCharSet(false);

            pcs.AddRange('!', '/');
            pcs.AddRange(':', '@');
            pcs.AddRange('[', '`');
            pcs.Remove(@"-_ ");
            pcs.Remove(PwCharSet.Brackets);
            m_strSpecial = pcs.ToString();
        }
Esempio n. 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 = Encoding.UTF8.GetChars(pbUTF8);

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

            PwCharSet pcs = pp.CharSet;

            pcs.Clear();

            PopulateCharSet(vChars, pcs);

            Array.Clear(vChars, 0, vChars.Length);
            Array.Clear(pbUTF8, 0, pbUTF8.Length);

            return(pp);
        }
Esempio n. 3
0
        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;
        }
        public static PwgError Generate(ProtectedString psOutBuffer,
                                        PwProfile pwProfile, CryptoRandomStream crsRandomSource)
        {
            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 = Encoding.UTF8.GetBytes(vGenerated);
            psOutBuffer.SetString(Encoding.UTF8.GetString(pbUTF8, 0, pbUTF8.Length));
            Array.Clear(pbUTF8, 0, pbUTF8.Length);
            Array.Clear(vGenerated, 0, vGenerated.Length);

            return(PwgError.Success);
        }
Esempio n. 5
0
        internal static bool PrepareCharSet(PwCharSet pwCharSet, PwProfile pwProfile)
        {
            uint cc = pwCharSet.Size;

            for (uint i = 0; i < cc; ++i)
            {
                char ch = pwCharSet[i];
                if ((ch == char.MinValue) || (ch == '\t') || (ch == '\r') ||
                    (ch == '\n') || char.IsSurrogate(ch))
                {
                    return(false);
                }
            }

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

            if (!string.IsNullOrEmpty(pwProfile.ExcludeCharacters))
            {
                pwCharSet.Remove(pwProfile.ExcludeCharacters);
            }

            return(true);
        }
		public static PwgError Generate(ProtectedString psOutBuffer,
			PwProfile pwProfile, CryptoRandomStream crsRandomSource)
		{
			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 = Encoding.UTF8.GetBytes(vGenerated);
			psOutBuffer.SetString(Encoding.UTF8.GetString(pbUTF8, 0, pbUTF8.Length));
			Array.Clear(pbUTF8, 0, pbUTF8.Length);
			Array.Clear(vGenerated, 0, vGenerated.Length);

			return PwgError.Success;
		}
Esempio n. 7
0
 internal static void PopulateCharSet(char[] vChars, PwCharSet pcs)
 {
     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 ((@"!#$%&'*+,./:;=?@^").IndexOf(ch) >= 0)
         {
             pcs.Add(pcs.SpecialChars);
         }
         else if (ch == ' ')
         {
             pcs.Add(' ');
         }
         else if (ch == '-')
         {
             pcs.Add('-');
         }
         else if (ch == '_')
         {
             pcs.Add('_');
         }
         else if (ch == '\"')
         {
             pcs.Add(pcs.SpecialChars);
         }
         else if (ch == '\\')
         {
             pcs.Add(pcs.SpecialChars);
         }
         else if ((@"()[]{}<>").IndexOf(ch) >= 0)
         {
             pcs.Add(PwCharSet.Brackets);
         }
         else if ((ch >= '~') && (ch <= 255))
         {
             pcs.Add(pcs.HighAnsiChars);
         }
         else
         {
             pcs.Add(ch);
         }
     }
 }
Esempio n. 8
0
        internal static char GenerateCharacter(PwCharSet pwCharSet,
                                               CryptoRandomStream crsRandomSource)
        {
            uint cc = pwCharSet.Size;

            if (cc == 0)
            {
                return(char.MinValue);
            }

            uint i = (uint)crsRandomSource.GetRandomUInt64(cc);

            return(pwCharSet[i]);
        }
Esempio n. 9
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. 10
0
 internal void UpdateCharSet(bool bSetXml)
 {
     if (bSetXml)
     {
         PwCharSet pcs = new PwCharSet(m_pwCharSet.ToString());
         m_strCharSetRanges     = pcs.PackAndRemoveCharRanges();
         m_strCharSetAdditional = pcs.ToString();
     }
     else
     {
         PwCharSet pcs = new PwCharSet(m_strCharSetAdditional);
         pcs.UnpackCharRanges(m_strCharSetRanges);
         m_pwCharSet = pcs;
     }
 }
Esempio n. 11
0
		internal static char GenerateCharacter(PwProfile pwProfile,
			PwCharSet pwCharSet, CryptoRandomStream crsRandomSource)
		{
			if(pwCharSet.Size == 0) return char.MinValue;

			ulong uIndex = crsRandomSource.GetRandomUInt64();
			uIndex %= (ulong)pwCharSet.Size;

			char ch = pwCharSet[(uint)uIndex];

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

			return ch;
		}
Esempio n. 12
0
        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());

            if (!PwGenerator.PrepareCharSet(pcs, pwProfile))
            {
                return(PwgError.InvalidCharSet);
            }

            char[] v = new char[pwProfile.Length];
            try
            {
                for (int i = 0; i < v.Length; ++i)
                {
                    char ch = PwGenerator.GenerateCharacter(pcs, crsRandomSource);
                    if (ch == char.MinValue)
                    {
                        return(PwgError.TooFewCharacters);
                    }

                    v[i] = ch;
                    if (pwProfile.NoRepeatingCharacters)
                    {
                        pcs.Remove(ch);
                    }
                }

                byte[] pbUtf8 = StrUtil.Utf8.GetBytes(v);
                psOut = new ProtectedString(true, pbUtf8);
                MemUtil.ZeroByteArray(pbUtf8);
            }
            finally { MemUtil.ZeroArray <char>(v); }

            return(PwgError.Success);
        }
Esempio n. 13
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. 14
0
        internal static char GenerateCharacter(PwProfile pwProfile,
                                               PwCharSet pwCharSet, CryptoRandomStream crsRandomSource)
        {
            if (pwCharSet.Size == 0)
            {
                return(char.MinValue);
            }

            ulong uIndex = crsRandomSource.GetRandomUInt64();

            uIndex %= (ulong)pwCharSet.Size;

            char ch = pwCharSet[(uint)uIndex];

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

            return(ch);
        }
        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)
                {
                    MemUtil.ZeroArray <char>(vGenerated);
                    return(PwgError.TooFewCharacters);
                }

                vGenerated[nIndex] = ch;
            }

            byte[] pbUtf8 = StrUtil.Utf8.GetBytes(vGenerated);
            psOut = new ProtectedString(true, pbUtf8);
            MemUtil.ZeroByteArray(pbUtf8);
            MemUtil.ZeroArray <char>(vGenerated);

            return(PwgError.Success);
        }
Esempio n. 16
0
 private void UpdateCharSet(bool bSetXml)
 {
     if(bSetXml)
     {
         PwCharSet pcs = new PwCharSet(m_pwCharSet.ToString());
         m_strCharSetRanges = pcs.PackAndRemoveCharRanges();
         m_strCharSetAdditional = pcs.ToString();
     }
     else
     {
         PwCharSet pcs = new PwCharSet(m_strCharSetAdditional);
         pcs.UnpackCharRanges(m_strCharSetRanges);
         m_pwCharSet = pcs;
     }
 }
Esempio n. 17
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 = Encoding.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 ((@"!#$%&'*+,./:;=?@^").IndexOf(ch) >= 0)
                {
                    pcs.Add(pcs.SpecialChars);
                }
                else if (ch == ' ')
                {
                    pcs.Add(' ');
                }
                else if (ch == '-')
                {
                    pcs.Add('-');
                }
                else if (ch == '_')
                {
                    pcs.Add('_');
                }
                else if (ch == '\"')
                {
                    pcs.Add(pcs.SpecialChars);
                }
                else if (ch == '\\')
                {
                    pcs.Add(pcs.SpecialChars);
                }
                else if ((@"()[]{}<>").IndexOf(ch) >= 0)
                {
                    pcs.Add(PwCharSet.Brackets);
                }
                else if ((ch >= '~') && (ch <= 255))
                {
                    pcs.Add(pcs.HighAnsiChars);
                }
                else
                {
                    pcs.Add(ch);
                }
            }

            Array.Clear(vChars, 0, vChars.Length);
            Array.Clear(pbUTF8, 0, pbUTF8.Length);

            return(pp);
        }
        private static bool ReadCustomCharSet(CharStream cs, PwCharSet pcsOut)
        {
            Debug.Assert(cs.PeekChar() != '[');             // Consumed already
            Debug.Assert(pcsOut.Size == 0);

            bool bAdd = true;

            while (true)
            {
                char ch = cs.ReadChar();
                if (ch == char.MinValue)
                {
                    return(false);
                }
                if (ch == ']')
                {
                    break;
                }

                if (ch == '\\')
                {
                    ch = cs.ReadChar();
                    if (ch == char.MinValue)
                    {
                        return(false);
                    }

                    if (bAdd)
                    {
                        pcsOut.Add(ch);
                    }
                    else
                    {
                        pcsOut.Remove(ch);
                    }
                }
                else if (ch == '^')
                {
                    if (bAdd)
                    {
                        bAdd = false;
                    }
                    else
                    {
                        return(false);                     // '^' toggles the mode only once
                    }
                }
                else
                {
                    PwCharSet pcs = new PwCharSet();
                    if (!pcs.AddCharSet(ch))
                    {
                        return(false);
                    }

                    if (bAdd)
                    {
                        pcsOut.Add(pcs.ToString());
                    }
                    else
                    {
                        pcsOut.Remove(pcs.ToString());
                    }
                }
            }

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

            string strPattern = pwProfile.Pattern;

            if (string.IsNullOrEmpty(strPattern))
            {
                return(PwgError.Success);
            }

            CharStream        cs          = new CharStream(strPattern);
            LinkedList <char> llGenerated = new LinkedList <char>();
            PwCharSet         pcs         = new PwCharSet();

            while (true)
            {
                char ch = cs.ReadChar();
                if (ch == char.MinValue)
                {
                    break;
                }

                pcs.Clear();

                if (ch == '\\')
                {
                    ch = cs.ReadChar();
                    if (ch == char.MinValue)
                    {
                        return(PwgError.InvalidPattern);
                    }

                    pcs.Add(ch);                     // Allow "{...}" support and char check
                }
                else if (ch == '[')
                {
                    if (!ReadCustomCharSet(cs, pcs))
                    {
                        return(PwgError.InvalidPattern);
                    }
                }
                else
                {
                    if (!pcs.AddCharSet(ch))
                    {
                        return(PwgError.InvalidPattern);
                    }
                }

                int nCount = 1;
                if (cs.PeekChar() == '{')
                {
                    nCount = ReadCount(cs);
                    if (nCount < 0)
                    {
                        return(PwgError.InvalidPattern);
                    }
                }

                for (int i = 0; i < nCount; ++i)
                {
                    if (!PwGenerator.PrepareCharSet(pcs, pwProfile))
                    {
                        return(PwgError.InvalidCharSet);
                    }
                    if (pwProfile.NoRepeatingCharacters)
                    {
                        foreach (char chUsed in llGenerated)
                        {
                            pcs.Remove(chUsed);
                        }
                    }

                    char chGen = PwGenerator.GenerateCharacter(pcs,
                                                               crsRandomSource);
                    if (chGen == char.MinValue)
                    {
                        return(PwgError.TooFewCharacters);
                    }

                    llGenerated.AddLast(chGen);
                }
            }

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

            char[] v = new char[llGenerated.Count];
            llGenerated.CopyTo(v, 0);

            if (pwProfile.PatternPermutePassword)
            {
                PwGenerator.Shuffle(v, crsRandomSource);
            }

            byte[] pbUtf8 = StrUtil.Utf8.GetBytes(v);
            psOut = new ProtectedString(true, pbUtf8);
            MemUtil.ZeroByteArray(pbUtf8);

            MemUtil.ZeroArray <char>(v);
            return(PwgError.Success);
        }
Esempio n. 20
0
        private void SetGenerationOptions(PwProfile opt)
        {
            bool bPrevInit = m_bBlockUIUpdate;
            m_bBlockUIUpdate = true;

            m_rbStandardCharSet.Checked = (opt.GeneratorType == PasswordGeneratorType.CharSet);
            m_rbPattern.Checked = (opt.GeneratorType == PasswordGeneratorType.Pattern);
            m_rbCustom.Checked = (opt.GeneratorType == PasswordGeneratorType.Custom);

            m_numGenChars.Value = opt.Length;

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

            m_cbUpperCase.Checked = pcs.RemoveIfAllExist(PwCharSet.UpperCase);
            m_cbLowerCase.Checked = pcs.RemoveIfAllExist(PwCharSet.LowerCase);
            m_cbDigits.Checked = pcs.RemoveIfAllExist(PwCharSet.Digits);
            m_cbSpecial.Checked = pcs.RemoveIfAllExist(PwCharSet.SpecialChars);
            m_cbHighAnsi.Checked = pcs.RemoveIfAllExist(PwCharSet.HighAnsiChars);
            m_cbMinus.Checked = pcs.RemoveIfAllExist("-");
            m_cbUnderline.Checked = pcs.RemoveIfAllExist("_");
            m_cbSpace.Checked = pcs.RemoveIfAllExist(" ");
            m_cbBrackets.Checked = pcs.RemoveIfAllExist(PwCharSet.Brackets);

            m_tbCustomChars.Text = pcs.ToString();

            m_tbPattern.Text = opt.Pattern;
            m_cbPatternPermute.Checked = opt.PatternPermutePassword;

            m_cbEntropy.Checked = opt.CollectUserEntropy;
            m_cbExcludeLookAlike.Checked = opt.ExcludeLookAlike;
            m_cbNoRepeat.Checked = opt.NoRepeatingCharacters;
            m_tbExcludeChars.Text = opt.ExcludeCharacters;

            SelectCustomGenerator(opt.CustomAlgorithmUuid, opt.CustomAlgorithmOptions);

            m_bBlockUIUpdate = bPrevInit;
        }
Esempio n. 21
0
		internal void Initialize(bool bFullInitialize)
		{
			this.Clear();

			if(bFullInitialize == false) return;

			StringBuilder sbHighAnsi = new StringBuilder();
			for(char ch = '~'; ch < 255; ++ch)
				sbHighAnsi.Append(ch);
			m_strHighAnsi = sbHighAnsi.ToString();

			PwCharSet pcs = new PwCharSet(false);
			pcs.AddRange('!', '/');
			pcs.AddRange(':', '@');
			pcs.AddRange('[', '`');
			pcs.Remove(@"-_ ");
			pcs.Remove(PwCharSet.Brackets);
			m_strSpecial = pcs.ToString();
		}
        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);
            MemUtil.ZeroArray <char>(vArray);
            vGenerated.Clear();

            return(PwgError.Success);
        }
Esempio n. 23
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. 24
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. 25
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);
                }
            }

            MemUtil.ZeroArray <char>(vChars);
            MemUtil.ZeroByteArray(pbUtf8);
            return(pp);
        }
		public static PwgError Generate(ProtectedString psOutBuffer,
			PwProfile pwProfile, CryptoRandomStream crsRandomSource)
		{
			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 == '[')
				{
					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 = Encoding.UTF8.GetBytes(vArray);
			psOutBuffer.SetString(Encoding.UTF8.GetString(pbUtf8, 0, pbUtf8.Length));
			Array.Clear(pbUtf8, 0, pbUtf8.Length);
			Array.Clear(vArray, 0, vArray.Length);
			vGenerated.Clear();

			return PwgError.Success;
		}