Hash Password in C#? Bcrypt/PBKDF2

前端 未结 9 1028
傲寒
傲寒 2020-12-22 20:06

I looked up msdn and other resources on how to do this but i came up with no clear solutions. This is the best i found http://blogs.msdn.com/b/shawnfa/archive/2004/04/14/gen

9条回答
  •  轮回少年
    2020-12-22 20:34

    Microsoft has a page up with sample code using PBKDF2 for anyone using .Net Core:

    Hash passwords in ASP.NET Core

    From the article:

    using System;
    using System.Security.Cryptography;
    using Microsoft.AspNetCore.Cryptography.KeyDerivation;
    
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.Write("Enter a password: ");
            string password = Console.ReadLine();
    
            // generate a 128-bit salt using a secure PRNG
            byte[] salt = new byte[128 / 8];
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(salt);
            }
            Console.WriteLine($"Salt: {Convert.ToBase64String(salt)}");
    
            // derive a 256-bit subkey (use HMACSHA1 with 10,000 iterations)
            string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                password: password,
                salt: salt,
                prf: KeyDerivationPrf.HMACSHA1,
                iterationCount: 10000,
                numBytesRequested: 256 / 8));
            Console.WriteLine($"Hashed: {hashed}");
        }
    }
    
    /*
     * SAMPLE OUTPUT
     *
     * Enter a password: Xtw9NMgx
     * Salt: NZsP6NnmfBuYeJrrAKNuVQ==
     * Hashed: /OOoOer10+tGwTRDTrQSoeCxVTFr6dtYly7d0cPxIak=
     */
    

提交回复
热议问题