Advertisement
AyrA

PBKDF2 for 1 second

Jan 31st, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. //Number of default iterations. Somewhere between 50k and 500k is good to test
  2. const int DefaultIterations = 100000;
  3. const int NumBytes = 32;
  4.  
  5. //Initialize Generator
  6. var Gen = new System.Security.Cryptography.Rfc2898DeriveBytes("SomePassword", 16, DefaultIterations);
  7. //Measure Time
  8. var DT = DateTime.UtcNow;
  9. Gen.GetBytes(NumBytes);
  10. var Runtime = DateTime.UtcNow.Subtract(DT).TotalMilliseconds;
  11. //Calculate iterations for 1 second duration and round it to thousands.
  12. var ProposedIterations = (int)(DefaultIterations / Runtime) * 1000;
  13.  
  14. //Write Statistics
  15. Console.WriteLine("Initial Measurement:");
  16. Console.WriteLine("Time (msec):        {0}", (int)Runtime);
  17. Console.WriteLine("Iterations:         {0}", DefaultIterations);
  18. Console.WriteLine("Proposed for 1 sec: {0}", ProposedIterations);
  19.  
  20. //Verify proposed iterations by doing it again
  21. Console.WriteLine("Verifying...");
  22. //Reinitialize Generator with proposed value for iterations
  23. Gen = new System.Security.Cryptography.Rfc2898DeriveBytes("SomePassword", 16, ProposedIterations);
  24. DT = DateTime.UtcNow;
  25. Gen.GetBytes(NumBytes);
  26. Runtime = DateTime.UtcNow.Subtract(DT).TotalMilliseconds;
  27.  
  28. //Write new Statistics
  29. Console.WriteLine("Time (msec):        {0}", (int)Runtime);
  30. Console.WriteLine("Iterations:         {0}", ProposedIterations);
  31. Console.WriteLine("Proposed for 1 sec: {0}", (int)(ProposedIterations / Runtime) * 1000 /*round to 1000*/);
  32.  
  33. //If everything went alright,
  34. //the new proposed value should almost match the first proposed value (5000 diff max).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement