public PasswordRestriction( SymbolsType acceptedTypes, int passwordMinLength = DefaultMinLength, int passwordMaxLength = DefaultMaxLength) { Contract.Assert(passwordMinLength > 0); Contract.Assert(passwordMaxLength >= passwordMinLength); Contract.Assert(acceptedTypes.Count() <= passwordMaxLength); AcceptedTypes = acceptedTypes; PasswordMaxLength = passwordMaxLength; PasswordMinLength = passwordMinLength; }
private static string GenerateRestPart( SymbolsType acceptedTypes, byte[] restPartNumbers, long length) { var passwordPartBuilder = new StringBuilder(); var acceptedSymbols = acceptedTypes.GetSymbols(); for (var i = 0; i < length; i++) { var symbolNumber = restPartNumbers[i] % acceptedSymbols.Length; passwordPartBuilder.Append(acceptedSymbols[symbolNumber]); } return passwordPartBuilder.ToString(); }
private static string GenerateMandatoryPart( SymbolsType acceptedTypes, byte[] passwordSymbolsNumbers) { var passwordPartBuilder = new StringBuilder(); for (var i = 0; acceptedTypes != 0; i++) { var randomTypeNumber = passwordSymbolsNumbers[2 * i]; var randomSymbolNumber = passwordSymbolsNumbers[2 * i + 1]; var unusedTypes = acceptedTypes.GetFlags().ToArray(); var usedType = unusedTypes[randomTypeNumber % unusedTypes.Length]; var usedSymbols = usedType.GetSymbols(); passwordPartBuilder.Append(usedSymbols[randomSymbolNumber % usedSymbols.Length]); acceptedTypes = TurnOffFlag(acceptedTypes, usedType); } return passwordPartBuilder.ToString(); }
private static SymbolsType TurnOffFlag(SymbolsType flags, SymbolsType flag) => flags & ~flag;
public static IEnumerable <SymbolsType> GetFlags(this SymbolsType input) { return(AllTypes().Where(type => input.HasFlag(type))); }
public static long Count(this SymbolsType type) { return(Enumerable.Range(0, 64).Sum(number => ((long)type >> number) & 1)); }