Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Konscious.Security.Cryptography;
  7. using System.Security.Cryptography;
  8. using System.Diagnostics;
  9.  
  10. namespace Argon2
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. Run();
  17. }
  18.  
  19. private static byte[] CreateSalt()
  20. {
  21. var buffer = new byte[64];
  22. var rng = new RNGCryptoServiceProvider();
  23. rng.GetBytes(buffer);
  24. return buffer;
  25. }
  26.  
  27. private static byte[] HashPassword(string password, byte[] salt)
  28. {
  29. var argon2 = new Argon2id(Encoding.UTF8.GetBytes(password));
  30.  
  31. argon2.Salt = salt;
  32. argon2.DegreeOfParallelism = 8; // four cores
  33. argon2.Iterations = 4;
  34. argon2.MemorySize = 1024 * 1024; //1GB
  35.  
  36. return argon2.GetBytes(64);
  37. }
  38.  
  39. private static bool VerifyHash(string password, byte[] salt, byte[] hash)
  40. {
  41. var newHash = HashPassword(password, salt);
  42. return hash.SequenceEqual(newHash);
  43. }
  44.  
  45. public static void Run()
  46. {
  47. var password = "SutPikOgDø123!@";
  48. var stopwatch = Stopwatch.StartNew();
  49.  
  50. Console.WriteLine($"Creating hash for password '{ password }' .");
  51.  
  52. var salt = CreateSalt();
  53. Console.WriteLine($"Using salt '{ Convert.ToBase64String(salt) }' .");
  54.  
  55. var hash = HashPassword(password, salt);
  56. Console.WriteLine($"Hash is '{ Convert.ToBase64String(hash) }' .");
  57.  
  58. stopwatch.Stop();
  59. Console.WriteLine($" Process took { stopwatch.ElapsedMilliseconds / 1024.0 } s");
  60.  
  61. stopwatch = Stopwatch.StartNew();
  62. Console.WriteLine($"Verifying hash....");
  63.  
  64. var success = VerifyHash(password, salt, hash);
  65. Console.WriteLine(success ? "Success!" : "Failure!");
  66.  
  67. stopwatch.Stop();
  68. Console.WriteLine($"Process took { stopwatch.ElapsedMilliseconds / 1024.0 } s");
  69.  
  70. Console.ReadLine();
  71. }
  72.  
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement