locked
About SHA1 RRS feed

  • Question

  • WPF, I can do this below:

    ....

    SHA1 sha = new SHA1CryptoServiceProvider();

    byte[] hash = sha.ComputeHash(mergedArray);

    string token = Convert.ToBase64String(hash);

    req.Headers.Add("X-DTVToken", token);

    ...

    But Store apps, I can't use SHA1, how can I do to replace above codes equally?

    Thursday, May 9, 2013 1:46 PM

Answers

  • You can use SHA-1 and even higher SHA hash algorithms than in WPF like SHA512 which is great for bank apps.

    string originalString = "abc";
    
                HashAlgorithmProvider hashCompute = HashAlgorithmProvider.OpenAlgorithm("SHA1");
                var bytes = Encoding.UTF8.GetBytes(originalString);
                IBuffer buffHash = hashCompute.HashData(bytes.AsBuffer());
    
                var array = buffHash.ToArray();
                var result = Convert.ToBase64String(array);

    This is a base64 of "abc" string.
    You should look at this sample as how you can do this and if you're an app with high security risks, you should handle arrays, buffers and similar a bit more carefully.

    PS: You can support MD5, SHA512 and other hash algorithms by simply replacing "SHA1" with preferred algorithm.
    Which is much better than in WPF and has similar API as Bouncy Castle (a huge crypto library for C# and Java) and Android. 

    • Proposed as answer by Jernej Kavka Thursday, May 9, 2013 10:20 PM
    • Marked as answer by Maxlovexxx Monday, May 13, 2013 4:44 AM
    Thursday, May 9, 2013 10:20 PM

All replies

  • You can use SHA-1 and even higher SHA hash algorithms than in WPF like SHA512 which is great for bank apps.

    string originalString = "abc";
    
                HashAlgorithmProvider hashCompute = HashAlgorithmProvider.OpenAlgorithm("SHA1");
                var bytes = Encoding.UTF8.GetBytes(originalString);
                IBuffer buffHash = hashCompute.HashData(bytes.AsBuffer());
    
                var array = buffHash.ToArray();
                var result = Convert.ToBase64String(array);

    This is a base64 of "abc" string.
    You should look at this sample as how you can do this and if you're an app with high security risks, you should handle arrays, buffers and similar a bit more carefully.

    PS: You can support MD5, SHA512 and other hash algorithms by simply replacing "SHA1" with preferred algorithm.
    Which is much better than in WPF and has similar API as Bouncy Castle (a huge crypto library for C# and Java) and Android. 

    • Proposed as answer by Jernej Kavka Thursday, May 9, 2013 10:20 PM
    • Marked as answer by Maxlovexxx Monday, May 13, 2013 4:44 AM
    Thursday, May 9, 2013 10:20 PM
  • Or you can replace "SHA1" with one of the enumeration values of HashAlgotihmNames .
    Saturday, October 26, 2013 12:17 PM