so many Sha256 functions? RRS feed

  • Question


    Hi all, i need to do a sha256, and i found the SHA256Cng, SHA256CryptoServiceProvider, SHA256Managed classes. so i was wondering which should i use?

    my requirements are that i have a string object, and i need to get the 32-byte digest as a 64 length upper-case hexadecimal string, as fast as possible. which class would be best for this requirement?

    • Moved by Paul Zhou Friday, January 21, 2011 2:34 AM not supported (From:Visual C# General)
    Saturday, January 15, 2011 3:28 PM

All replies

  • Try this one:

    using System.Security.Cryptography;


      public string sha256(string strPlain)


            //UnicodeEncoding UE = new UnicodeEncoding();   

            ASCIIEncoding UE = new ASCIIEncoding();

            byte[] HashValue, MessageBytes = UE.GetBytes(strPlain);


            SHA256Managed SHhash = new SHA256Managed();


            string strHex = "";


            HashValue = SHhash.ComputeHash(MessageBytes);

            foreach (byte b in HashValue)


                strHex += String.Format("{0:x2}", b);


            return strHex;


    Saturday, January 15, 2011 6:47 PM

    Hi thx for the help, btw i was under the impression that SHA256Cng is the "newer" one and thus more efficient, or am i wrong?

    Saturday, January 15, 2011 10:39 PM
  • In benchmarks I have seen managed is the slowest.  The CNG stands for crypto next gen and are new algorithms introduced with vista.





    As far as being more efficient performance wise not to sure.  They may be...  It is just a new api to replace existing CAPI.  Microsoft can't break people using old API so there is a new api that does the same thing.


    Sunday, January 16, 2011 2:02 AM

    ok if CNg means using some of the stuff already provided in vista does that mean that if i need this function in windows phone 7 (say im building a XNA game) then i couldn't use this function?

    Thursday, January 20, 2011 6:04 AM
  • Hi Pacerier,


    Welcome to the MSDN Forum!


    There is a better forum for your question:


    There are more related experts in that forum so that you may get more useful information.


    Btw, I’d like to move this thread to off-topic and suggest you to post a new thread in that forum.

    Thank you for your posting and understanding.

    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 21, 2011 2:33 AM
  • ok, thx for the reply
    Friday, January 21, 2011 2:59 PM