Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using SHA1 hashes instead of MD5 hashes

Discussion in 'Windows' started by Nadan, Nov 20, 2014.

  1. Nadan

    Nadan

    Joined:
    Jan 20, 2013
    Posts:
    341
    Hi,

    I have used these instructions to generate an MD5 hash for an input string.

    http://wiki.unity3d.com/index.php/MD5

    It seems that MD5 is not working on Windows Phone so I've changed it to SHA1 with the given instructions:

    "You can use SHA1CryptoServiceProvider instead of MD5CryptoServiceProvider if you want to create SHA1 hashes instead of MD5 hashes."

    Code (JavaScript):
    1. #pragma strict
    2. static function Md5Sum(strToEncrypt: String)
    3. {
    4.    var encoding = System.Text.UTF8Encoding();
    5.    var bytes = encoding.GetBytes(strToEncrypt);
    6.    // encrypt bytes
    7.    var md5 = System.Security.Cryptography.SHA1CryptoServiceProvider();
    8.    var hashBytes:byte[] = md5.ComputeHash(bytes);
    9.    // Convert the encrypted bytes back to a string (base 16)
    10.    var hashString = "";
    11.    for (var i = 0; i < hashBytes.Length; i++)
    12.    {
    13.      hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, "0"[0]);
    14.    }
    15.    return hashString.PadLeft(32, "0"[0]);
    16. }
    I have platform set to Windows Phone 8 and on the Unity Editor when I press play it works fine.

    But when I try it on my device and choose Build And Run. I get this error:

    Error building Player: Exception: Error: type `System.Security.Cryptography.SHA1CryptoServiceProvider` doesn't exist in target framework. It is referenced from Assembly-UnityScript at System.String md5functions::Md5Sum(System.String).

    Error: type `System.Security.Cryptography.SHA1CryptoServiceProvider` doesn't exist in target framework. It is referenced from Assembly-UnityScript at System.String md5functions::Md5Sum(System.String).

    Error: method `System.Void System.Security.Cryptography.SHA1CryptoServiceProvider::.ctor()` doesn't exist in target framework. It is referenced from Assembly-UnityScript at System.String md5functions::Md5Sum(System.String).

    Any help what to do?
     
    Last edited: Nov 20, 2014
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
  3. Nadan

    Nadan

    Joined:
    Jan 20, 2013
    Posts:
    341