C # get SHA256 code

Source: Internet
Author: User

1. If you want to get the SHA256 of a string , the code is as follows:

        public static string SHA256 (String str)        {            //If STR has Chinese, different encoding sha is different!!            byte[] sha256data = Encoding.UTF8.GetBytes (str);                       SHA256Managed Sha256 = new sha256managed ();            byte[] by = Sha256.computehash (sha256data);                        Return Bitconverter.tostring (by). Replace ("-", ""). ToLower ();            //return convert.tobase64string (by);                         /+        }

2. If you want to get the SHA256 of a file , the code is as follows:

  

        public string SHA256 ()        {                   string str = @ "C:\Users\ICCWDT_Driver\iccwdt.sys";            FileStream stream = new FileStream (str, filemode.open);            SHA256Managed Sha256 = new sha256managed ();            byte[] by = Sha256.computehash (stream);            Return Bitconverter.tostring (by). Replace ("-", ""). ToLower ();            //return convert.tobase64string (by);                         /+        }

3. By looking at the system's APIs, you can see that there are two parameters that can be passed in: Incoming byte[] type or stream type

  

         Summary:         //     computes the hash value of the specified byte array. ////        parameter:         //   buffer:        //     input to calculate its hash code.        ////        Returns the result:         //     computed hash code. ////        Exception:/         /   System.ArgumentNullException:        //     buffer is null. ////   system.objectdisposedexception:        //     This object has been disposed. Public byte[] ComputeHash (byte[] buffer);        //Summary:         //     calculates the hash value of the specified System.IO.Stream object. ////        parameter:         //   InputStream:        //     input to calculate its hash code.        ////        Returns the result:         //     computed hash code. ////        Exception:         //   system.objectdisposedexception:        //     This object has been disposed. Public byte[] ComputeHash (Stream inputstream);

  

C # get SHA256 code

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.