/// <summary>
        ///     Initializes a new instance of the <see cref="PasswordHasher" /> class.
        /// </summary>
        /// <param name="optionsValue">The options.</param>
        // ReSharper disable once UnusedMember.Global
        public PasswordHasher(PasswordHasherOptions optionsValue)
        {
            var options = optionsValue ?? new PasswordHasherOptions();

            this.compatibilityMode = options.CompatibilityMode;
            switch (this.compatibilityMode)
            {
            case PasswordHasherCompatibilityMode.IdentityV2:
                // nothing else to do
                break;

            case PasswordHasherCompatibilityMode.IdentityV3:
                this.iterCount = options.IterationCount;
                if (this.iterCount < 1)
                {
                    throw new InvalidOperationException("The iteration count must be a positive integer.");
                }

                break;

            default:
                throw new InvalidOperationException("The provided PasswordHasherCompatibilityMode is invalid.");
            }

            this.rng = options.Rng;
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new instance of <see cref="PasswordHasher{User}"/>.
        /// </summary>
        /// <param name="optionsAccessor">The options for this instance.</param>
        public PasswordHasherService(IOptions <PasswordHasherOptions> optionsAccessor = null)
        {
            var options = optionsAccessor?.Value ?? new PasswordHasherOptions();

            _compatibilityMode = options.CompatibilityMode;
            switch (_compatibilityMode)
            {
            case PasswordHasherCompatibilityMode.IdentityV2:
                // nothing else to do
                break;

            case PasswordHasherCompatibilityMode.IdentityV3:
                _iterCount = options.IterationCount;
                if (_iterCount < 1)
                {
                    throw new InvalidOperationException("InvalidPasswordHasherIterationCount");
                }
                break;

            default:
                throw new InvalidOperationException("InvalidPasswordHasherCompatibilityMode");
            }

            _rng = RandomNumberGenerator.Create();
        }
Esempio n. 3
0
    /// <summary>
    /// Creates a new instance of <see cref="PasswordHasher{TUser}"/>.
    /// </summary>
    /// <param name="optionsAccessor">The options for this instance.</param>
    public PasswordHasher(IOptions <PasswordHasherOptions>?optionsAccessor = null)
    {
        var options = optionsAccessor?.Value ?? new PasswordHasherOptions();

        _compatibilityMode = options.CompatibilityMode;
        switch (_compatibilityMode)
        {
        case PasswordHasherCompatibilityMode.IdentityV2:
            // nothing else to do
            break;

        case PasswordHasherCompatibilityMode.IdentityV3:
            _iterCount = options.IterationCount;
            if (_iterCount < 1)
            {
                throw new InvalidOperationException(Resources.InvalidPasswordHasherIterationCount);
            }
            break;

        default:
            throw new InvalidOperationException(Resources.InvalidPasswordHasherCompatibilityMode);
        }

        _rng = options.Rng;
    }
Esempio n. 4
0
        /// <summary>
        ///     Constructs a PasswordHasher using the specified options
        /// </summary>
        /// <param name="options"></param>
        public PasswordHasher(IOptions <PasswordHasherOptions> options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _compatibilityMode = options.Options.CompatibilityMode;
            switch (_compatibilityMode)
            {
            case PasswordHasherCompatibilityMode.IdentityV2:
                // nothing else to do
                break;

            case PasswordHasherCompatibilityMode.IdentityV3:
                _iterCount = options.Options.IterationCount;
                if (_iterCount < 1)
                {
                    throw new InvalidOperationException(Resources.InvalidPasswordHasherIterationCount);
                }
                break;

            default:
                throw new InvalidOperationException(Resources.InvalidPasswordHasherCompatibilityMode);
            }

            _rng = options.Options.Rng;
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a new instance of <see cref="PasswordHasher{TUser}"/>.
        /// </summary>
        /// <param name="optionsAccessor">The options for this instance.</param>
        public V2PasswordHasher(IOptions <PasswordHasherOptions> optionsAccessor = null)
        {
            var options = new V2PasswordHasherOptions();

            _compatibilityMode = options.CompatibilityMode;
            switch (_compatibilityMode)
            {
            case PasswordHasherCompatibilityMode.IdentityV2:
                // nothing else to do
                break;

            case PasswordHasherCompatibilityMode.IdentityV3:
                _iterCount = options.IterationCount;
                if (_iterCount < 1)
                {
                    throw new InvalidOperationException("The iteration count must be a positive integer. Error when the iteration count is < 1.");
                }
                break;

            default:
                throw new InvalidOperationException("The provided PasswordHasherCompatibilityMode is invalid. Error when the password hasher doesn't understand the format it's being asked to produce");
            }

            _rng = options.Rng;
        }
        public PasswordService(IOptions <PasswordHasherOptions> optionsAccessor = null)
        {
            var options = optionsAccessor?.Value ?? new PasswordHasherOptions();

            _iterCount         = 10000;
            _compatibilityMode = PasswordHasherCompatibilityMode.IdentityV3;

            _rng = RandomNumberGenerator.Create();
        }
 public AspNetIdentityPasswordHasher(
     int iterations,
     PasswordHasherCompatibilityMode compatibilityMode = PasswordHasherCompatibilityMode.IdentityV3
     )
     : base(Options.Create(new PasswordHasherOptions() {
     IterationCount    = iterations,
     CompatibilityMode = compatibilityMode
 }))
 {
 }
        //private pwdType _pwdType;

        //First PassThrough Will Always Encrypt With V3 On Plain Text
        public string HashPassword(UserDto u, string db, string p)
        {
            _compatibilityMode = VerifyHashedPassword(u, db, p);
            if (_compatibilityMode == PasswordHasherCompatibilityMode.IdentityV2)
            {
                return(Convert.ToBase64String(HashPasswordV2(p, _rng)));
            }
            else
            {
                return(Convert.ToBase64String(HashPasswordV3(p, _rng)));
            }
            //return "";
        }
        public void FullRoundTrip(PasswordHasherCompatibilityMode compatMode)
        {
            // Arrange
            var hasher = new PasswordHasher(compatMode: compatMode);

            // Act & assert - success case
            var hashedPassword = hasher.HashPassword(null, "password 1");
            var successResult  = hasher.VerifyHashedPassword(null, hashedPassword, "password 1");

            Assert.Equal(PasswordVerificationResult.Success, successResult);

            // Act & assert - failure case
            var failedResult = hasher.VerifyHashedPassword(null, hashedPassword, "password 2");

            Assert.Equal(PasswordVerificationResult.Failed, failedResult);
        }
Esempio n. 10
0
        public PersonPasswordHasher(IOptions <PersonPasswordHasherOptions> optionsAccessor = null)
        {
            var options = optionsAccessor?.Value ?? new PersonPasswordHasherOptions();

            _compatibilityMode = options.CompatibilityMode;
            _rng = options.Rng;
            switch (_compatibilityMode)
            {
            case PasswordHasherCompatibilityMode.IdentityV2:
                break;

            case PasswordHasherCompatibilityMode.IdentityV3:
                _iterCount = options.IterationCount;
                if (_iterCount < 1)
                {
                    throw new InvalidOperationException("Count Error");
                }
                break;

            default:
                throw new InvalidOperationException("Invalid Identity version");
            }
        }
Esempio n. 11
0
        public PasswordHasher(PasswordHasherCompatibilityMode compatibilityMode = PasswordHasherCompatibilityMode.IdentityV3, int iterationCount = 10000, RandomNumberGenerator rng = null)
        {
            _compatibilityMode = compatibilityMode;
            switch (_compatibilityMode)
            {
            case PasswordHasherCompatibilityMode.IdentityV2:
                // nothing else to do
                break;

            case PasswordHasherCompatibilityMode.IdentityV3:
                if (iterationCount < 1)
                {
                    throw new InvalidOperationException("The iteration count must be a positive integer.");
                }
                _iterCount = iterationCount;
                break;

            default:
                throw new InvalidOperationException("The provided PasswordHasherCompatibilityMode is invalid.");
            }

            _rng = rng ?? RandomNumberGenerator.Create();
        }
Esempio n. 12
0
        public SimplePasswordEncryption(IOptions <PasswordHasherOptions> optionsAccessor = null)
        {
            var options = optionsAccessor?.Value ?? new PasswordHasherOptions();

            _compatibilityMode = options.CompatibilityMode;
            switch (_compatibilityMode)
            {
            case PasswordHasherCompatibilityMode.IdentityV2:
                break;

            case PasswordHasherCompatibilityMode.IdentityV3:
                _iterCount = options.IterationCount;
                if (_iterCount < 1)
                {
                    throw new InvalidOperationException();
                }
                break;

            default:
                throw new InvalidOperationException();
            }

            _randomNumberGenerator = RandomNumberGenerator.Create();
        }
Esempio n. 13
0
 /// <summary>
 /// Creates a new instance of <see cref="PasswordHasher{TUser}"/>.
 /// </summary>
 /// <param name="options">The options for this instance.</param>
 public PasswordHasher()
 {
     _compatibilityMode = PasswordHasherCompatibilityMode.IdentityV3;
     _iterCount         = 10000;
 }