Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Konscious.Security.Cryptography;
- using System.Security.Cryptography;
- using System.Diagnostics;
- namespace Argon2
- {
- class Program
- {
- static void Main(string[] args)
- {
- Run();
- }
- private static byte[] CreateSalt()
- {
- var buffer = new byte[64];
- var rng = new RNGCryptoServiceProvider();
- rng.GetBytes(buffer);
- return buffer;
- }
- private static byte[] HashPassword(string password, byte[] salt)
- {
- var argon2 = new Argon2id(Encoding.UTF8.GetBytes(password));
- argon2.Salt = salt;
- argon2.DegreeOfParallelism = 8; // four cores
- argon2.Iterations = 4;
- argon2.MemorySize = 1024 * 1024; //1GB
- return argon2.GetBytes(64);
- }
- private static bool VerifyHash(string password, byte[] salt, byte[] hash)
- {
- var newHash = HashPassword(password, salt);
- return hash.SequenceEqual(newHash);
- }
- public static void Run()
- {
- var password = "SutPikOgDø123!@";
- var stopwatch = Stopwatch.StartNew();
- Console.WriteLine($"Creating hash for password '{ password }' .");
- var salt = CreateSalt();
- Console.WriteLine($"Using salt '{ Convert.ToBase64String(salt) }' .");
- var hash = HashPassword(password, salt);
- Console.WriteLine($"Hash is '{ Convert.ToBase64String(hash) }' .");
- stopwatch.Stop();
- Console.WriteLine($" Process took { stopwatch.ElapsedMilliseconds / 1024.0 } s");
- stopwatch = Stopwatch.StartNew();
- Console.WriteLine($"Verifying hash....");
- var success = VerifyHash(password, salt, hash);
- Console.WriteLine(success ? "Success!" : "Failure!");
- stopwatch.Stop();
- Console.WriteLine($"Process took { stopwatch.ElapsedMilliseconds / 1024.0 } s");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement