public static string EncodePassword(string pass, string saltBase64) { byte[] bytes = Encoding.Unicode.GetBytes(pass); byte[] src = Convert.FromBase64String(saltBase64); byte[] dst = new byte[src.Length + bytes.Length]; System.Buffer.BlockCopy(src, 0, dst, 0, src.Length); System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length); HashAlgorithm algorithm = HashAlgorithm.Create("SHA1"); byte[] inArray = algorithm.ComputeHash(dst); return Convert.ToBase64String(inArray); }
And if you have to create a new Salt, just use:
public string CreateSalt(int saltLength) { //Create and populate random byte array byte[] randomArray = new byte[length]; string randomString; //Create random salt and convert to string RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetBytes(randomArray); randomString = Convert.ToBase64String(randomArray); return randomString; }
Resources: forums.asp.net, Peter Stathakos Blog
No comments:
Post a Comment