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 ProtectedString CreatePassword() { ProtectedString pw = new ProtectedString(); PwProfile pf = new PwProfile(); pf.GeneratorType = PasswordGeneratorType.Pattern; //use 256bit Hex-Key-Profile pf.Pattern = "h{64}"; CustomPwGeneratorPool pwGenPool = new CustomPwGeneratorPool(); PwgError perr = PwGenerator.Generate(out pw, pf, null, pwGenPool); if (perr != PwgError.Success) { throw new Exception("Error while creating new password!"); } return(pw); }
public static PwgError Generate(ProtectedString psOutBuffer, PwProfile pwProfile, byte[] pbUserEntropy, CustomPwGeneratorPool pwAlgorithmPool) { Debug.Assert(psOutBuffer != null); if(psOutBuffer == null) throw new ArgumentNullException("psOutBuffer"); Debug.Assert(pwProfile != null); if(pwProfile == null) throw new ArgumentNullException("pwProfile"); psOutBuffer.Clear(); CryptoRandomStream crs = CreateCryptoStream(pbUserEntropy); PwgError e = PwgError.Unknown; if(pwProfile.GeneratorType == PasswordGeneratorType.CharSet) e = CharSetBasedGenerator.Generate(psOutBuffer, pwProfile, crs); else if(pwProfile.GeneratorType == PasswordGeneratorType.Pattern) e = PatternBasedGenerator.Generate(psOutBuffer, pwProfile, crs); else if(pwProfile.GeneratorType == PasswordGeneratorType.Custom) e = GenerateCustom(psOutBuffer, pwProfile, crs, pwAlgorithmPool); else { Debug.Assert(false); } 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; }