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

SHA256Encrypt static function

291 Views
Copy Code Show/Hide Line Numbers
using System.Security.Cryptography;
using System.Text;
using System.Web;
 
public static string SHA256Encrypt(string source, int digitRandom)
{
    string outputHash = String.Empty;
 
    try
    {
        string hashResult = String.Empty;
        SHA256Managed sha = new SHA256Managed();
        byte[] Data = Encoding.UTF8.GetBytes(source);
        byte[] Salt = new byte[digitRandom];
        byte[] DataAndSalt = new byte[Data.Length + digitRandom];
        Array.Copy(Data, DataAndSalt, Data.Length);
        Array.Copy(Salt, 0, DataAndSalt, Data.Length, digitRandom);
 
        byte[] HashOut = sha.ComputeHash(DataAndSalt);
 
        //remove bad character in Url transmission "+/="
        hashResult = Convert.ToBase64String(HashOut);
        hashResult = hashResult.Replace("+", "");
        hashResult = hashResult.Replace("/", "");
        hashResult = hashResult.Replace("=", "");
            
        // final result
        outputHash = hashResult;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.Write(ex.Message);
        return String.Empty;
    }
 
    return outputHash;
}
by tomy ismail
  July 15, 2009 @ 10:29pm
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