Skip to content

Instantly share code, notes, and snippets.

@hanssens
Created March 2, 2012 17:18
Show Gist options
  • Save hanssens/1959763 to your computer and use it in GitHub Desktop.
Save hanssens/1959763 to your computer and use it in GitHub Desktop.
[C#] RenderHash() - String Extension for generating a (SHA1) hash
/// <summary>
/// Generates a (SHA1) hashed string, by using the passphrase as a poor man's salt.
/// </summary>
/// <param name="orderReference">(Optional) This reference is used for storing a (temporary) shipping order. You can decide for yourself which reference to use, but it needs to be unique. The reference is used for identifying the shipping order in the Paazl database.</param>
public static string RenderHash(this string value, string passPhrase)
{
// Append the passphrase to the value
value += passPhrase;
// Convert the string value to a byte array
// Note: I've used the UTF8 encoding here
byte[] buffer = Encoding.UTF8.GetBytes(value);
// And automagically convert it to a hashed string
SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment