//------------------------------------------------------- public Word ApplyScript(Word password) { password.AppendLog("Info: ApplyScript to '{0}'\r\n", password.Text); LanguageType language = (LanguageType)Enum.Parse(typeof(LanguageType), Preset.ScriptLanguage); if (engine == null || engine.SourceCode != Preset.ScriptCode || engine.Language != language || Preset.ScriptEntry != Preset.ScriptEntry) { engine = new Controls.DynamicCompile.DcEngine(Preset.ScriptCode, language, Preset.ScriptEntry); engine.CurrentAssembly = null; } string result = (string)engine.Run(this, password); if (result != null) password.Chars = result.ToCharArray(); return password; }
char[] FilterChars(Word password, char[] chars) { if (Preset.FilterRemember) chars = Filters[FilterName.Remember].GetChars(this, password, chars); if (Preset.FilterKeyboard) chars = Filters[FilterName.Keyboard].GetChars(this, password, chars); if (Preset.FilterEnforce) chars = Filters[FilterName.Enforce].GetChars(this, password, chars); if (Preset.FilterPhone) chars = Filters[FilterName.Phone].GetChars(this, password, chars); if (Preset.FilterAscii) chars = Filters[FilterName.Ascii].GetChars(this, password, chars); if (Preset.FilterChars) chars = Filters[FilterName.Chars].GetChars(this, password, chars); return chars; }
/// </summary> /// Generate new password. /// </summary> public Word NewPassword() { var password = new Word(); password.AppendLog("Info: Password log started.\r\n"); // Maximum number of attempts to generate password. var maxTries = 4; for (var a = 1; a <= maxTries; a++) { password.AppendLog("Info: Attempt {0} to generate password.\r\n", a); var charsToUse = Preset.GetChars(); if (charsToUse.Length == 0) { password.AppendLog("Error: chars list is empty!\r\n"); } else { //password.AppendLog("Info: Chars To Use: {0}\r\n", new string(charsToUse)); // Now we can create our password. for (var i = 0; i < this.Preset.PasswordLength; i++) { // Apply filters and get list of available chars. var filteredChars = FilterChars(password, charsToUse); // If filters left some chars then... if (filteredChars.Length > 0) { // Calculate password strength (10^x). var strength = Math.Log(filteredChars.Length) / Math.Log(10); password.Strength += strength; // Get random char. var charPosition = rnd.Next(filteredChars.Length); var filteredChar = filteredChars[charPosition]; var chars = password.Chars.ToList(); chars.Add(filteredChar); password.Chars = chars.ToArray(); } else { //password.AppendLog("Warning: filteredChars.length == 0\r\n"); break; } } } // If we reached required password length then... if (password.Chars.Length == Preset.PasswordLength) { break; } } // Apply regular expressions to password. if (Preset.RegexEnabled) password = ApplyRegex(password); // Apply script to password. if (Preset.ScriptEnabled) password = ApplyScript(password); // If password is empty then... if (password.Chars.Length != Preset.PasswordLength) { // Add error message. password.AppendLog("Error: all attempts to generate password was failed. Please relax password settings.\r\n"); } return password; }
Word ApplyRegex(Word password) { try { Regex rx = new Regex(Preset.RegexPatternFind); var s = new string(password.Chars); password.Chars = rx.Replace(s, Preset.RegexPatternReplace).ToCharArray(); } catch (Exception ex) { password.AppendLog(ex.Message); } return password; }
/// </summary> /// Generate new password. /// </summary> public Word NewPassword() { var password = new Word(); password.AppendLog("Info: Password log started.\r\n"); // Maximum number of attempts to generate password. //password.AppendLog("Info: Attempt {0} to generate password.\r\n", a); var types = new List <CharType>(); var charsCount = 0; if (Preset.UseUppercase) { charsCount += Preset.CharsUppercase.Length; for (var i = 0; i < Preset.RatioUppercase; i++) { types.Add(CharType.Uppercase); } } if (Preset.UseLowercase) { charsCount += Preset.CharsLowercase.Length; for (var i = 0; i < Preset.RatioLowercase; i++) { types.Add(CharType.Lowercase); } } if (Preset.UseNumbers) { charsCount += Preset.CharsNumbers.Length; for (var i = 0; i < Preset.RatioNumbers; i++) { types.Add(CharType.Number); } } if (Preset.UseSymbols) { charsCount += Preset.CharsSymbols.Length; for (var i = 0; i < Preset.RatioSymbols; i++) { types.Add(CharType.Symbol); } } if (Preset.UseExtra) { charsCount += Preset.CharsExtra.Length; for (var i = 0; i < Preset.RatioExtra; i++) { types.Add(CharType.Extra); } } // Types must contains types of all chars. if (types.Count == 0) { password.AppendLog("Error: chars list is empty!\r\n"); return(password); } var list = new Dictionary <CharType, string> { { CharType.Uppercase, Preset.CharsUppercase }, { CharType.Lowercase, Preset.CharsLowercase }, { CharType.Number, Preset.CharsNumbers }, { CharType.Symbol, Preset.CharsSymbols }, { CharType.Extra, Preset.CharsExtra } }; //password.AppendLog("Info: Chars To Use: {0}\r\n", new string(charsToUse)); // Now we can create our password. for (var i = 0; i < Preset.PasswordLength; i++) { var charsToGenerate = Preset.PasswordLength - i; var missing = new List <CharType>(); if (Preset.UseUppercase && !password.Chars.Intersect(Preset.CharsUppercase).Any()) { missing.Add(CharType.Uppercase); } if (Preset.UseLowercase && !password.Chars.Intersect(Preset.CharsLowercase).Any()) { missing.Add(CharType.Lowercase); } if (Preset.UseNumbers && !password.Chars.Intersect(Preset.CharsNumbers).Any()) { missing.Add(CharType.Number); } if (Preset.UseSymbols && !password.Chars.Intersect(Preset.CharsSymbols).Any()) { missing.Add(CharType.Symbol); } if (Preset.UseExtra && !password.Chars.Intersect(Preset.CharsExtra).Any()) { missing.Add(CharType.Extra); } // Get random type according to rate. var charType = types[rnd.Next(types.Count)]; // Override with missing type. if (missing.Count > 0 && charsToGenerate <= missing.Count) { charType = missing.FirstOrDefault(); } var chars = list[charType].ToArray(); // Apply filters and get list of available chars. var filteredChars = FilterChars(password, chars); // If filters left some chars then... if (filteredChars.Length > 0) { // Calculate password strength (10^x). var strength = Math.Log(charsCount) / Math.Log(10); password.Strength += strength; // Get random char. var charPosition = rnd.Next(filteredChars.Length); var filteredChar = filteredChars[charPosition]; password.AppendChar(filteredChar); } else { //password.AppendLog("Warning: filteredChars.length == 0\r\n"); break; } } // If no filter enabled then randomize chars to make sure that missing chars are not at the end. if (!Preset.FilterRemember && !Preset.FilterKeyboard && !Preset.FilterPhone) { Shuffle(password.Chars); } // Apply regular expressions to password. if (Preset.RegexEnabled) { password = ApplyRegex(password); } // Apply script to password. if (Preset.ScriptEnabled) { password = ApplyScript(password); } // If password is empty then... if (password.Chars.Length != Preset.PasswordLength) { // Add error message. password.AppendLog("Error: all attempts to generate password was failed. Please relax password settings.\r\n"); } return(password); }