otkalce

Authentication - cryptography methods

May 16th, 2023
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | Source Code | 0 0
  1. private static (byte[], string) GenerateSalt()
  2. {
  3.     // Generate salt
  4.     var salt = RandomNumberGenerator.GetBytes(128 / 8);
  5.     var b64Salt = Convert.ToBase64String(salt);
  6.  
  7.     return (salt, b64Salt);
  8. }
  9.  
  10. private static string CreateHash(string password, byte[] salt)
  11. {
  12.     // Create hash from password and salt
  13.     byte[] hash =
  14.         KeyDerivation.Pbkdf2(
  15.             password: password,
  16.             salt: salt,
  17.             prf: KeyDerivationPrf.HMACSHA256,
  18.             iterationCount: 100000,
  19.             numBytesRequested: 256 / 8);
  20.     string b64Hash = Convert.ToBase64String(hash);
  21.  
  22.     return b64Hash;
  23. }
  24.  
  25. private static string GenerateSecurityToken()
  26. {
  27.     byte[] securityToken = RandomNumberGenerator.GetBytes(256 / 8);
  28.     string b64SecToken = Convert.ToBase64String(securityToken);
  29.  
  30.     return b64SecToken;
  31. }
  32.  
Tags: auth-crypto
Add Comment
Please, Sign In to add comment