Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.AspNetCore.Identity;
- using Microsoft.Extensions.Options;
- using System;
- using System.Diagnostics;
- namespace ConsoleApp2
- {
- public static class Program
- {
- private const int IterationCount = 100000;
- public static void Main(string[] args)
- {
- Console.WriteLine("Start.");
- if(!int.TryParse(args[0], out var iterations))
- iterations = IterationCount;
- const string password = "test";
- var sw = new Stopwatch();
- sw.Start();
- var hasher = new PasswordHasher(PasswordHasherCompatibilityMode.IdentityV3, iterations);
- var user = new User { Id = 1, Name = "Dvorak" };
- var hash = hasher.HashPassword(
- user,
- password);
- Console.WriteLine("time: {0}", DateTime.Now);
- Console.WriteLine("hash: {0}", hash);
- var verificationResult = hasher.VerifyHashedPassword(null, hash, password);
- Console.WriteLine("success: {0}", verificationResult == PasswordVerificationResult.Success);
- sw.Stop();
- Console.WriteLine("Took {0} milliseconds..", sw.ElapsedMilliseconds);
- //Console.ReadLine();
- }
- }
- internal sealed class PasswordHasher : PasswordHasher<object>
- {
- public PasswordHasher(PasswordHasherCompatibilityMode? compatMode = null, int? iterCount = null)
- : base(BuildOptions(compatMode, iterCount))
- {
- }
- private static IOptions<PasswordHasherOptions> BuildOptions(PasswordHasherCompatibilityMode? compatMode, int? iterCount)
- {
- var options = new PasswordHasherOptionsAccessor();
- if (compatMode != null)
- {
- options.Value.CompatibilityMode = (PasswordHasherCompatibilityMode)compatMode;
- }
- if (iterCount != null)
- {
- options.Value.IterationCount = (int)iterCount;
- }
- //Assert.NotNull(options.Value.Rng); // should have a default value
- //options.Value.Rng = new SequentialRandomNumberGenerator();
- return options;
- }
- private class PasswordHasherOptionsAccessor : IOptions<PasswordHasherOptions>
- {
- public PasswordHasherOptions Value { get; } = new PasswordHasherOptions();
- }
- }
- }
Add Comment
Please, Sign In to add comment