Created
January 29, 2017 19:35
-
-
Save CloudyWater/3114921e087c24d48952ab13697e326d to your computer and use it in GitHub Desktop.
the DatabaseConnection class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Data; | |
using System.Data.SqlClient; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class DatabaseConnection : MonoBehaviour | |
{ | |
private const string SECRET_KEY = "secretkey"; | |
private const string ADD_USER_URL = "http://localhost/AddUser.php?"; | |
public void CreateAccount (string email, string username, string password) | |
{ | |
StartCoroutine (PostCreateAccount (email, username, password)); | |
} | |
IEnumerator PostCreateAccount (string email, string username, string password) | |
{ | |
using (MD5 md5Hash = MD5.Create ()) | |
{ | |
string encryptedPass = Crypto.Encrypt (password); | |
string hash = GetMd5Hash (md5Hash, email + username + encryptedPass + SECRET_KEY); | |
string postUrl = ADD_USER_URL + "email=" + WWW.EscapeURL (email) + "&username=" + WWW.EscapeURL (username) + | |
"&password=" + WWW.EscapeURL (encryptedPass) + "&hash=" + WWW.EscapeURL(hash); | |
WWW hsPost = new WWW (postUrl); | |
yield return hsPost; | |
if (hsPost.error != null) | |
{ | |
Debug.Log ("Error adding user: " + hsPost.error); | |
} | |
else | |
{ | |
Debug.Log ("Returned Data: " + hsPost.text); | |
} | |
} | |
} | |
private string GetMd5Hash (MD5 md5Hash, string input) | |
{ | |
byte [] data = md5Hash.ComputeHash (Encoding.UTF8.GetBytes (input)); | |
StringBuilder builder = new StringBuilder (); | |
for (int i = 0; i < data.Length; i++) | |
{ | |
builder.Append (data [i].ToString ("x2")); | |
} | |
return builder.ToString (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment