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

Turn a string into SHA512

Discussion in 'Scripting' started by gsus725, Nov 20, 2013.

  1. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    250
    Does anyone know how to do this in Unity? I've been trying for 12 hours....Thanks. I have this message and a "secret key" and I need it turned into SHA512

    I really need working example code too

    I've gathered that it might have something to do with System.Security.Cryptography's SHA512Managed class, but I don't understand how to use that class at all it makes no sense to me that is why I need a working example of it turning a string into SHA512
     
    Last edited: Nov 20, 2013
  2. Sir-Tiddlesworth

    Sir-Tiddlesworth

    Joined:
    Oct 19, 2011
    Posts:
    908
    This hasn't been tested in Unity, but it does work in a regular C# console application.

    Code (csharp):
    1. using System;
    2. using System.Security.Cryptography;
    3. using System.Text;
    4.  
    5. namespace ConsoleApplication1 {
    6.  
    7.     class Program {
    8.  
    9.         static void Main (string [] args) {
    10.  
    11.             Console.Write (GetSHA512 ("Hello World!"));
    12.             Console.ReadLine ();
    13.         }
    14.  
    15.         private static string GetSHA512 (string String) {
    16.  
    17.             UnicodeEncoding ue = new UnicodeEncoding ();
    18.             byte [] hashValue;
    19.             byte [] message = ue.GetBytes (String);
    20.  
    21.             SHA512Managed hashString = new SHA512Managed ();
    22.             string hex = "";
    23.  
    24.             hashValue = hashString.ComputeHash (message);
    25.  
    26.             foreach (byte x in hashValue) {
    27.                 hex += String.Format ("{0:x2}", x);
    28.             }
    29.  
    30.             return hex;
    31.         }
    32.     }
    33. }
    34.  
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Above probably won't work in Unity

    Code (csharp):
    1. string s = "Hello World";
    2. byte[] bytes = System.Text.Encoding.ASCII.GetBytes( s );
    3. System.Security.Cryptography.SHA512 sha = System.Security.Cryptography.SHA512.Create();
    4. byte[] hash = sha.ComputeHash( bytes );
    5. string result = System.Text.Encoding.ASCII.GetString( hash );
    6. Debug.Log( result );
    7.  
     
  4. NewStaz

    NewStaz

    Joined:
    Mar 25, 2017
    Posts:
    1
    This works
    Code (CSharp):
    1. public static string GetSHA512(string text) {
    2.         SHA512 sha = new SHA512Managed();
    3.  
    4.         //compute hash from the bytes of text
    5.         sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(text));
    6.    
    7.         //get hash result after compute it
    8.         byte[] result = sha.Hash;
    9.  
    10.         StringBuilder strBuilder = new StringBuilder();
    11.         for (int i = 0; i < result.Length; i++)
    12.         {
    13.           //change it into 2 hexadecimal digits
    14.           //for each byte
    15.           strBuilder.Append(result[i].ToString("x2"));
    16.         }
    17.      
    18.       return strBuilder.ToString();
    19. }