CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

HashedString static function using Base64

253 Views
Copy Code Show/Hide Line Numbers
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)));
}
by tomy ismail
  July 15, 2009 @ 10:32pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate