How to hash a password in C#

1 Answer

0 votes
using System;
using System.Security.Cryptography;

class Program
{
    static void Main()
    {
        string password = "SecurePassword123!@";

        string storedHash = HashPassword(password);
        Console.Write("Hash: ");
        Console.WriteLine(storedHash);
    }

    // Hash a password using PBKDF2-HMAC-SHA256
    public static string HashPassword(string password) {
        byte[] salt = new byte[16];

        using (var rng = RandomNumberGenerator.Create()) {
            rng.GetBytes(salt);
        }

        var pbkdf2 = new Rfc2898DeriveBytes(
            password,
            salt,
            100000,
            HashAlgorithmName.SHA256
        );

        byte[] hash = pbkdf2.GetBytes(32);

        return Convert.ToBase64String(salt) + ":" + Convert.ToBase64String(hash);
    }
}


/*
run:

Hash: kq9Gd7YprY1PDpyAj0BObg==:n6o8DIsdhsiFnR9jq93PR6oUBBk68rwYCZGCpgzeP9s=

*/

 



answered 16 hours ago by avibootz

Related questions

1 answer 8 views
1 answer 8 views
1 answer 10 views
1 answer 197 views
197 views asked Sep 14, 2017 by avibootz
1 answer 190 views
190 views asked Sep 14, 2017 by avibootz
...