Search Unity

Highscore table Help

Discussion in 'Scripting' started by SirMarley, Sep 2, 2014.

  1. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Hey guys...

    To be sincere, I have no idea how this works...

    I have this, to send the data to the SQL base:

    Code (CSharp):
    1. IEnumerator PostScores(string name, int score)
    2.     {
    3.         string hash = Md5Functions.Md5Sum(name + score + secretKey);
    4.  
    5.         string post_url = addScoreURL + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
    6.  
    7.         WWW hs_post = new WWW(post_url);
    8.         yield return hs_post;
    9.  
    10.         if (hs_post.error != null)
    11.         {
    12.             print("There was an error posting the high score: " + hs_post.error);
    13.         }
    14.     }
    Now, when I had this Md5Functions, it worked fine:

    Code (CSharp):
    1. public  static string Md5Sum(string strToEncrypt)
    2. {
    3.     System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
    4.     byte[] bytes = ue.GetBytes(strToEncrypt);
    5.     // encrypt bytes
    6.     System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    7.     byte[] hashBytes = md5.ComputeHash(bytes);
    8.     // Convert the encrypted bytes back to a string (base 16)
    9.     string hashString = "";
    10.     for (int i = 0; i < hashBytes.Length; i++)
    11.     {
    12.         hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
    13.     }
    14.     return hashString.PadLeft(32, '0');
    15. }
    But because I had to compile it to be upload to the Metro Store, I had to replace the Md5Functions for this code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. #if UNITY_METRO
    5. using Windows.Security.Cryptography;
    6. using Windows.Security.Cryptography.Core;
    7. using Windows.Storage.Streams;
    8. #else
    9. using System.Text;
    10. using System.Security.Cryptography;
    11. #endif
    12.  
    13. public static class Md5Functions
    14. {
    15.     static string md5val;
    16.  
    17.     static void Start()
    18.     {
    19.         md5val = Md5Sum("Hello World!");
    20.         Debug.Log(md5val);
    21.     }
    22.  
    23.     static void OnGUI()
    24.     {
    25.         GUILayout.Label(md5val);
    26.     }
    27.  
    28.     public static string Md5Sum(string strToEncrypt)
    29.     {
    30. #if UNITY_METRO
    31.        
    32.         // Convert the message string to binary data.
    33.         IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(strToEncrypt, BinaryStringEncoding.Utf8);
    34.        
    35.  
    36.         // Create a HashAlgorithmProvider object.
    37.         HashAlgorithmProvider objAlgProv = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
    38.  
    39.         // Demonstrate how to retrieve the name of the hashing algorithm.
    40.         string strAlgNameUsed = objAlgProv.AlgorithmName;
    41.  
    42.         // Hash the message.
    43.         IBuffer buffHash = objAlgProv.HashData(buffUtf8Msg);
    44.  
    45.         // Verify that the hash length equals the length specified for the algorithm.
    46.         if (buffHash.Length != objAlgProv.HashLength)
    47.             return null;
    48.  
    49.         // Convert the hash to a string (for display).
    50.         string strHashBase64 = CryptographicBuffer.EncodeToBase64String(buffHash);
    51.  
    52.         // Return the encoded string
    53.         return strHashBase64.PadLeft(32, '0');
    54. #else
    55.         UTF8Encoding ue = new UTF8Encoding();
    56.         byte[] bytes = ue.GetBytes(strToEncrypt);
    57.      
    58.         // encrypt bytes
    59.         MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    60.         byte[] hashBytes = md5.ComputeHash(bytes);
    61.      
    62.         // Convert the encrypted bytes back to a string (base 16)
    63.         string hashString = "";
    64.      
    65.         for (int i = 0; i < hashBytes.Length; i++)
    66.         {
    67.             hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
    68.         }
    69.      
    70.         return hashString.PadLeft(32, '0');
    71. #endif
    72.     }
    73. }
    And, of course, now, there is no data send to the SQL database...

    Can someone help me out, telling me what do I have to change either in the Md5 or in the sending data script, for it to work, please???
     
  2. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    The problem maybe here.... I guess...

    Code (CSharp):
    1. <?php
    2.         $db = mysql_connect('mysql', 'user', 'pass') or die('Could not connect: ' . mysql_error());
    3.         mysql_select_db('sql_db') or die('Could not select database');
    4.         // Strings must be escaped to prevent SQL injection attack.
    5.         $name = mysql_real_escape_string($_GET['name'], $db);
    6.         $score = mysql_real_escape_string($_GET['score'], $db);
    7.         $hash = $_GET['hash'];
    8.         $secretKey="mySecretKey"; # Change this value to match the value stored in the client javascript below
    9.  
    10.         $real_hash = md5($name . $score . $secretKey);
    11.         if($real_hash == $hash) {
    12.             // Send variables for the MySQL database class.
    13.             $query = "insert into scores values (NULL, '$name', '$score');";
    14.             $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    15.         }
    16. ?>
    So, how can I connect the Md5Function with the data sending script and the AddScores.php?

    (Of course, I have in my php the real user name, pass and sql db)