SHA1 encryption summary, sha1 Encryption

Source: Internet
Author: User
Tags sha1 encryption

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.