SHA1 encryption summary, sha1 Encryption
Public class Sha1Util
{
/// <Summary>
/// SHA1 encryption, returns an uppercase string
/// </Summary>
/// <Param name = "content"> strings to be encrypted </param>
/// <Returns> return 40 UTF-8 uppercase values </returns>
Public static string SHA1 (string content)
{
Return SHA1 (content, Encoding. UTF8 );
}
/// <Summary>
/// SHA1 encryption, returns an uppercase string
/// </Summary>
/// <Param name = "content"> strings to be encrypted </param>
/// <Param name = "encode"> specify the encryption code </param>
/// <Returns> returns a 40-bit uppercase string </returns>
Public static string SHA1 (string content, Encoding encode)
{
Try
{
SHA1 sha1 = new SHA1CryptoServiceProvider ();
Byte [] bytes_in = encode. GetBytes (content );
Byte [] bytes_out = sha1.ComputeHash (bytes_in );
Sha1.Dispose ();
String result = BitConverter. ToString (bytes_out );
Result = result. Replace ("-","");
Return result;
}
Catch (Exception ex)
{
Throw new Exception ("SHA1 encryption error:" + ex. Message );
}
}
# Region obtain the string encrypted by SHA1
Public string EncryptToSHA1 (string str)
{
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider ();
Byte [] str1 = Encoding. UTF8.GetBytes (str );
Byte [] str2 = sha1.ComputeHash (str1 );
Sha1.Clear ();
(Sha1 as IDisposable). Dispose ();
Return Convert. ToBase64String (str2 );
}
# Endregion
}
This article is a summary of many blogs. Thank you for your support.