Пример #1
0
        public static PwgError Generate(out ProtectedString psOut,
                                        PwProfile pwProfile, byte[] pbUserEntropy,
                                        CustomPwGeneratorPool pwAlgorithmPool)
        {
            Debug.Assert(pwProfile != null);
            if (pwProfile == null)
            {
                throw new ArgumentNullException("pwProfile");
            }

            CryptoRandomStream crs = CreateCryptoStream(pbUserEntropy);
            PwgError           e   = PwgError.Unknown;

            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;
            }

            return(e);
        }
Пример #2
0
        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);
        }