Guest User

Untitled

a guest
Nov 26th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.Extensions.Options;
  3. using System;
  4. using System.Diagnostics;
  5.  
  6. namespace ConsoleApp2
  7. {
  8. public static class Program
  9. {
  10. private const int IterationCount = 100000;
  11.  
  12. public static void Main(string[] args)
  13. {
  14. Console.WriteLine("Start.");
  15.  
  16. if(!int.TryParse(args[0], out var iterations))
  17. iterations = IterationCount;
  18.  
  19. const string password = "test";
  20.  
  21. var sw = new Stopwatch();
  22. sw.Start();
  23.  
  24. var hasher = new PasswordHasher(PasswordHasherCompatibilityMode.IdentityV3, iterations);
  25.  
  26. var user = new User { Id = 1, Name = "Dvorak" };
  27.  
  28. var hash = hasher.HashPassword(
  29. user,
  30. password);
  31.  
  32. Console.WriteLine("time: {0}", DateTime.Now);
  33.  
  34. Console.WriteLine("hash: {0}", hash);
  35.  
  36. var verificationResult = hasher.VerifyHashedPassword(null, hash, password);
  37.  
  38. Console.WriteLine("success: {0}", verificationResult == PasswordVerificationResult.Success);
  39.  
  40. sw.Stop();
  41. Console.WriteLine("Took {0} milliseconds..", sw.ElapsedMilliseconds);
  42.  
  43. //Console.ReadLine();
  44. }
  45. }
  46.  
  47. internal sealed class PasswordHasher : PasswordHasher<object>
  48. {
  49. public PasswordHasher(PasswordHasherCompatibilityMode? compatMode = null, int? iterCount = null)
  50. : base(BuildOptions(compatMode, iterCount))
  51. {
  52. }
  53.  
  54. private static IOptions<PasswordHasherOptions> BuildOptions(PasswordHasherCompatibilityMode? compatMode, int? iterCount)
  55. {
  56. var options = new PasswordHasherOptionsAccessor();
  57. if (compatMode != null)
  58. {
  59. options.Value.CompatibilityMode = (PasswordHasherCompatibilityMode)compatMode;
  60. }
  61. if (iterCount != null)
  62. {
  63. options.Value.IterationCount = (int)iterCount;
  64. }
  65. //Assert.NotNull(options.Value.Rng); // should have a default value
  66. //options.Value.Rng = new SequentialRandomNumberGenerator();
  67. return options;
  68. }
  69.  
  70. private class PasswordHasherOptionsAccessor : IOptions<PasswordHasherOptions>
  71. {
  72. public PasswordHasherOptions Value { get; } = new PasswordHasherOptions();
  73. }
  74. }
  75. }
Add Comment
Please, Sign In to add comment