Skip to content

Instantly share code, notes, and snippets.

@hancheester
Created October 14, 2023 12:27
Show Gist options
  • Save hancheester/8101a9ad3a2331753ce2e688579bd597 to your computer and use it in GitHub Desktop.
Save hancheester/8101a9ad3a2331753ce2e688579bd597 to your computer and use it in GitHub Desktop.
To hash password
using System.Security.Cryptography;
using System.Text;
namespace PasswordHasher
{
internal class Program
{
static void Main(string[] args)
{
const int keySize = 64;
const int iterations = 350000;
HashAlgorithmName hashAlgorithm = HashAlgorithmName.SHA512;
var saltBytes = RandomNumberGenerator.GetBytes(keySize);
Console.Write("Enter password: ");
var password = Console.ReadLine();
var hashBytes = Rfc2898DeriveBytes.Pbkdf2(
Encoding.UTF8.GetBytes(password!),
saltBytes,
iterations,
hashAlgorithm,
keySize);
Console.WriteLine();
var salt = Convert.ToHexString(saltBytes);
var hash = Convert.ToHexString(hashBytes);
Console.WriteLine($"Salt: \n{salt}");
Console.WriteLine();
Console.WriteLine($"Hash: \n{hash}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment