public static PwgError Generate(out ProtectedString psOut, PwProfile pwProfile, byte[] pbUserEntropy, CustomPwGeneratorPool pwAlgorithmPool) { Debug.Assert(pwProfile != null); if (pwProfile == null) { throw new ArgumentNullException("pwProfile"); } PwgError e = PwgError.Unknown; CryptoRandomStream crs = null; byte[] pbKey = null; try { crs = CreateRandomStream(pbUserEntropy, out pbKey); 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 if (pwProfile.GeneratorType == PasswordGeneratorType.Custom) { e = GenerateCustom(out psOut, pwProfile, crs, pwAlgorithmPool); } else { Debug.Assert(false); psOut = ProtectedString.Empty; } } finally { if (crs != null) { crs.Dispose(); } if (pbKey != null) { MemUtil.ZeroByteArray(pbKey); } } return(e); }
private static PwgError GenerateCustom(out ProtectedString psOut, PwProfile pwProfile, CryptoRandomStream crs, CustomPwGeneratorPool pwAlgorithmPool) { psOut = ProtectedString.Empty; Debug.Assert(pwProfile.GeneratorType == PasswordGeneratorType.Custom); if (pwAlgorithmPool == null) { return(PwgError.UnknownAlgorithm); } string strID = pwProfile.CustomAlgorithmUuid; if (string.IsNullOrEmpty(strID)) { Debug.Assert(false); return(PwgError.UnknownAlgorithm); } byte[] pbUuid = Convert.FromBase64String(strID); PwUuid uuid = new PwUuid(pbUuid); CustomPwGenerator pwg = pwAlgorithmPool.Find(uuid); if (pwg == null) { Debug.Assert(false); return(PwgError.UnknownAlgorithm); } ProtectedString pwd = pwg.Generate(pwProfile.CloneDeep(), crs); if (pwd == null) { return(PwgError.Unknown); } psOut = pwd; return(PwgError.Success); }