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

need help with a Sha256 script

Discussion in 'Scripting' started by Sean-Powell, Jun 9, 2015.

  1. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    I was using this a a md5 script without any problems but then found out about all the problems with md5. So i changed it to a Sha256 encryption and i got the error

    Assets/Scripts/ButtonClicked.cs(56,17): error CS0246: The type or namespace name `SHA256CryptoServiceProvider' could not be found. Are you missing a using directive or an assembly reference?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Security.Cryptography;
    4. using System.Text;
    5. using UnityEngine.UI;
    6.  
    7. public class ButtonClicked : MonoBehaviour
    8. {
    9.     public Button b;
    10.     public InputField username = null;
    11.     public InputField password = null;
    12.  
    13.     Material errorMat;
    14.     Color Red;
    15.     MeshRenderer userinputRender;
    16.  
    17.     string encryptPass;
    18.     string inputText;
    19.  
    20.  
    21.     void Start()
    22.     {
    23.         var inputField = username.GetComponent<InputField> ();
    24.         inputField.onEndEdit.AddListener (UserNameInput);
    25.  
    26.         var passwordField = password.GetComponent<InputField> ();
    27.         passwordField.onEndEdit.AddListener (PasswordInput);
    28.  
    29.         CheckServer (inputText, encryptPass);
    30.     }
    31.  
    32.     public void UserNameInput(string input)
    33.     {
    34.         inputText = input;
    35.         if (inputText == "") {//checks if the username is left blank does not work
    36.             //usernameError();
    37.         } else {
    38.             Debug.Log (inputText);
    39.         }
    40.     }
    41.  
    42.     public void PasswordInput(string input){
    43.         string passText = input + inputText;
    44.         Debug.Log (passText);
    45.         encryptPass = passwordEncryption (passText);
    46.         Debug.Log (encryptPass);
    47.     }
    48.  
    49.     string passwordEncryption(string passwordString){
    50.         UTF8Encoding ue = new UTF8Encoding ();
    51.         byte[] bytes = ue.GetBytes (passwordString);
    52.  
    53.         //MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider ();
    54.         //byte[] hashBytes = md5.ComputeHash (bytes);
    55.  
    56.         SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider ();
    57.         byte[] hashBytes = sha256.ComputeHash(bytes);
    58.    
    59.         string hashString = "";
    60.  
    61.         for (int i = 0; i < hashBytes.Length; i++) {
    62.             hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
    63.         }
    64.  
    65.         return hashString.PadLeft(32, '0');
    66.     }
    67. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Hmm, that's strange. If you manually type in the entire namespace "path" does it find it? e.g. System.Security.Cryptography.SHA256CryptoServiceProvider
     
  3. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    yea i tried and that did not work either i even tried sha512 that did not work either
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    I wonder if this is a missing function in Unity's version of Mono.
     
  5. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    The md5 function works so the class somewhat works. Just md5 is not very secure.
     
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    MD5CryptoServiceProvider is provided in mscorlib.dll. However, SHA256CryptoServiceProvider is in System.Core.dll.

    Even though both of these classes are in the same namespace, you will also need to add System.Core.dll to your list of references.
     
  7. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    Thank you is the dll located in the same folder?
     
  8. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Not exactly.. They both live in the GAC

    mscorlib.dll is the main library for .net 2.0
    System.Core.dll essentially extends this library and shipped with .net 3.0

    .net 3.0 is actually a service pack for 2.0. Everything is backwards compatible and as far as the CLR is concerned, there are no fundamental changes to the way .net operates.

    Technically, you should already have a reference to System.Core if you are targeting version 3.0 or 3.5 of the .net framework.
     
  9. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    Ok thanks
     
  10. Nastomeya

    Nastomeya

    Joined:
    Mar 14, 2015
    Posts:
    9
  11. JijiB

    JijiB

    Joined:
    Apr 13, 2015
    Posts:
    5
    create a new abstract class and it will fix the error.

    Code (CSharp):
    1. using System.Runtime.InteropServices;
    2.  
    3. namespace System.Security.Cryptography
    4. {
    5.     [ComVisible(true)]
    6.     public abstract class SHA256 : HashAlgorithm
    7.     {
    8.         protected SHA256()
    9.         {
    10.             this.HashSizeValue = 256;
    11.         }
    12.  
    13. #pragma warning disable CS0436 // Type conflicts with imported type
    14.         public static new SHA256 Create()
    15. #pragma warning restore CS0436 // Type conflicts with imported type
    16.         {
    17.             return SHA256.Create("System.Security.Cryptography.SHA256");
    18.         }
    19.  
    20.         public static new SHA256 Create(string hashName)
    21.         {
    22.             return (SHA256)CryptoConfig.CreateFromName(hashName);
    23.         }
    24.     }
    25. }