/// <summary> /// Extract settings from a compiled string (created by the <c>Compile</c> /// member function). /// </summary> /// <param name="strCompiled">Compiled string generated by <c>Compile</c>.</param> public static CharSpaces UnserializeFromString(string strCompiled) { Debug.Assert((strCompiled != null) && (strCompiled.Length == SpacesCount)); if (strCompiled == null) { throw new ArgumentNullException("strCompiled"); } if (strCompiled.Length < SpacesCount) { throw new ArgumentException(); } CharSpaces cs = new CharSpaces(); int nIndex = -1; cs.UpperCase = (strCompiled[++nIndex] == '1'); cs.LowerCase = (strCompiled[++nIndex] == '1'); cs.Numeric = (strCompiled[++nIndex] == '1'); cs.Underline = (strCompiled[++nIndex] == '1'); cs.Minus = (strCompiled[++nIndex] == '1'); cs.Space = (strCompiled[++nIndex] == '1'); cs.Special = (strCompiled[++nIndex] == '1'); cs.Brackets = (strCompiled[++nIndex] == '1'); cs.HighANSI = (strCompiled[++nIndex] == '1'); Debug.Assert((nIndex + 1) <= SpacesCount); return(cs); }
/// <summary> /// Convert the options of a <c>GenerationOptions</c> object to a string. /// </summary> /// <returns>String representing the current options.</returns> public static string SerializeToString(PasswordGenerationOptions gopt) { Debug.Assert(gopt != null); if (gopt == null) { throw new ArgumentNullException("gopt"); } StringBuilder sb = new StringBuilder(); sb.Append(gopt.m_strProfileName); sb.Append(FieldSeparator); sb.Append(((uint)gopt.m_gtType).ToString()); sb.Append(FieldSeparator); sb.Append(gopt.m_bCollectEntropy ? '1' : '0'); sb.Append(FieldSeparator); sb.Append(gopt.m_uPwLen.ToString()); sb.Append(FieldSeparator); sb.Append(CharSpaces.SerializeToString(gopt.m_charSpaces)); sb.Append(FieldSeparator); sb.Append(gopt.m_customCharSet.GetAsString().Replace(FieldSeparator, string.Empty)); sb.Append(FieldSeparator); sb.Append(gopt.m_strPattern.Replace(FieldSeparator, string.Empty)); return(sb.ToString()); }
private static CustomCharSet CreateCustomCharSet(CharSpaces opt) { CustomCharSet ccs = new CustomCharSet(); if (opt.UpperCase) { ccs.Add(CharSetUpper); } if (opt.LowerCase) { ccs.Add(CharSetLower); } if (opt.Numeric) { ccs.Add(CharSetNumeric); } if (opt.Underline) { ccs.Add('_'); } if (opt.Minus) { ccs.Add('-'); } if (opt.Space) { ccs.Add(' '); } if (opt.Special) { ccs.Add(@"!#$%&'*+,./:;=?@^"); ccs.Add('\"'); ccs.Add('\\'); } if (opt.Brackets) { ccs.Add(@"()[]{}<>"); } if (opt.HighANSI) { for (char ch = '~'; ch < 255; ++ch) { ccs.Add(ch); } } return(ccs); }
/// <summary> /// Convert the current settings to a string. /// </summary> /// <returns>String representing the options.</returns> public static string SerializeToString(CharSpaces cs) { Debug.Assert(cs != null); if (cs == null) { throw new ArgumentNullException("cs"); } StringBuilder sb = new StringBuilder(); sb.Append(cs.UpperCase ? '1' : '0'); sb.Append(cs.LowerCase ? '1' : '0'); sb.Append(cs.Numeric ? '1' : '0'); sb.Append(cs.Underline ? '1' : '0'); sb.Append(cs.Minus ? '1' : '0'); sb.Append(cs.Space ? '1' : '0'); sb.Append(cs.Special ? '1' : '0'); sb.Append(cs.Brackets ? '1' : '0'); sb.Append(cs.HighANSI ? '1' : '0'); return(sb.ToString()); }
/// <summary> /// Convert a serialized <c>GenerationOptions</c> string to an object. /// </summary> /// <param name="strSource">String to unserialize.</param> /// <returns>Unserialized object representing the string.</returns> public static PasswordGenerationOptions UnserializeFromString(string strSource) { Debug.Assert(strSource != null); if (strSource == null) { throw new ArgumentNullException("strSource"); } PasswordGenerationOptions gopt = new PasswordGenerationOptions(); string[] vParts = strSource.Split(new string[] { FieldSeparator }, StringSplitOptions.None); Debug.Assert(vParts.Length == 7); if (vParts.Length < 7) { return(gopt); } gopt.m_strProfileName = vParts[0]; uint u; uint.TryParse(vParts[1], out u); gopt.m_gtType = (PasswordGeneratorType)u; uint.TryParse(vParts[2], out u); gopt.m_bCollectEntropy = (u == 1); uint.TryParse(vParts[3], out u); gopt.m_uPwLen = u; gopt.m_charSpaces = CharSpaces.UnserializeFromString(vParts[4]); gopt.m_customCharSet.Set(vParts[5]); gopt.m_strPattern = vParts[6]; return(gopt); }