Language: C#
HashedString static function using Base64
using System; using System.Security.Cryptography; using System.Text; using System.Web; public static string HashedString(string cleanString, string salt) { if (String.IsNullOrEmpty(cleanString)) { return String.Empty; } //Bytes to hash byte[] hashBytes; //No salt provided if (String.IsNullOrEmpty(salt)) { //Gets bytes of input hashBytes = Encoding.Unicode.GetBytes(cleanString); } else { //Gets bytes input byte[] inputBytes = Encoding.Unicode.GetBytes(cleanString); byte[] saltBytes = Convert.FromBase64String(salt); //Sets new byte array the size of input and salt combined hashBytes = new byte[inputBytes.Length + saltBytes.Length]; //Copies salt and intput byte array data into saltedPassword array Buffer.BlockCopy(saltBytes, 0, hashBytes, 0, saltBytes.Length); Buffer.BlockCopy(inputBytes, 0, hashBytes, saltBytes.Length, inputBytes.Length); } //Create hash algorithm HashAlgorithm hashAlgorithm = new SHA512Managed(); //Returns hash of input /salted input return Convert.ToBase64String((hashAlgorithm.ComputeHash(hashBytes))); }
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

