otkalce

Password Hash Provider

Apr 7th, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | Source Code | 0 0
  1. public class PasswordHashProvider
  2. {
  3.     public static string GetSalt()
  4.     {
  5.         byte[] salt = RandomNumberGenerator.GetBytes(128 / 8); // divide by 8 to convert bits to bytes
  6.         string b64Salt = Convert.ToBase64String(salt);
  7.  
  8.         return b64Salt;
  9.     }
  10.  
  11.     public static string GetHash(string password, string b64salt)
  12.     {
  13.         byte[] salt = Convert.FromBase64String(b64salt);
  14.  
  15.         byte[] hash =
  16.             KeyDerivation.Pbkdf2(
  17.                 password: password,
  18.                 salt: salt,
  19.                 prf: KeyDerivationPrf.HMACSHA256,
  20.                 iterationCount: 100000,
  21.                 numBytesRequested: 256 / 8);
  22.         string b64Hash = Convert.ToBase64String(hash);
  23.  
  24.         return b64Hash;
  25.     }
  26. }
  27.  
Tags: cryptography
Add Comment
Please, Sign In to add comment