Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Generating SHA1 or MD5 hashes?

Discussion in 'Windows' started by pontori, Nov 7, 2013.

  1. pontori

    pontori

    Joined:
    Jun 10, 2013
    Posts:
    15
    On Windows Store apps, the Security.Cryptography has been moved from the System namespace to the Windows namespace, and it seems I can't access the Windows namespace from Unity. Has anybody found a way how to generate hashes on Windows Store apps?
     
  2. stephan7

    stephan7

    Joined:
    Jul 3, 2012
    Posts:
    44
    Something like the following should work.
    Alternatively, you could write a plugin.


    Code (csharp):
    1.  
    2. #if UNITY_METRO  !UNITY_EDITOR
    3. using Windows.Security.Cryptography;
    4. using Windows.Security.Cryptography.Core;
    5. using Windows.Storage.Streams;
    6. #endif


    Code (csharp):
    1.  
    2. #if UNITY_METRO  !UNITY_EDITOR
    3.     private string Encode(string str)
    4.     {
    5.                 HashAlgorithmProvider algorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
    6.         IBuffer buff = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
    7.         IBuffer hashed = algorithm.HashData(buff);
    8.         return CryptographicBuffer.EncodeToHexString(hashed);
    9.     }
    10. #else
    11.     private string Encode(string str)
    12.     {
    13.         return "use traditional method here";
    14.     }
    15. #endif
    16.  
    Stephan
    --
    http://www.software7.com
     
  3. pontori

    pontori

    Joined:
    Jun 10, 2013
    Posts:
    15
    Oh, nice! That worked perfectly. Thanks a ton!