- Home /
Can't Encode MD5
Hello, I have a script that passes plain text to this script (code below) which sends the data to a php file which processes the plain text file and encrypts it with MD5. After encryption it is sent back to the file below, which is returned back to the original script. However, I'm getting a lot of errors. My errors are dealing with me being able to access the file from the code below. The file isn't atttached to any object. But I can successfully utilize the main function Encrypt from the original script. The problem occurs when I get tot eh StartCoroutine, it says I can't do the Coroutine because its non static. How do I fix this?
using UnityEngine;
using System.Collections;
using System.Security.Cryptography;
public class EncryptData : MonoBehaviour
{
public float transparent;
private string url;
public WWW w;
public WWWForm loginform = new WWWForm();
public string formText;
public string text;
// Use this for initialization
static void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
public static string Encrypt (string originaltext)
{
text = originaltext;
StartCoroutine(EncryptText());
print("string is " + formText);
return(formText);
}
IEnumerator EncryptText()
{
url = "EncryptData.php?&text=" + WWW.EscapeURL (text);
w = new WWW (url);
yield return w;
if (w.error != null) {
print (w.error);
}
if (w.error == null) {
formText = w.text;
w.Dispose ();
formText.Trim();
StopAllCoroutines();
}
}
}
Just to check on a couple of things: you do know that $$anonymous$$D5 is a hash and not encryption? You can see the potential security issues of passing your plaintext over the web to obtain this hash? It is possible to compute this hash on the client.
I'm not sending plaintext because the hashed password are already in a database. I'm simply trying to hash it before I send it. But, when I tried to do it through c# md5 hashing, I somehow ended up with a different hash. That is why I'm accessing a local php file on the user's computer that would hash it the same way as in the database and return that to unity file above which would then be sent to another unity script that checks the database.
That's happened before. Take a look at this question and answer on stack overflow:
How does that apply, if you don't $$anonymous$$d me asking? His problem was caused by using two different inputs. I used the same input, I even tried trimmed and not trimmed.
There's always the possibility that you've made a similar error. I happen to have some md5 code running in Unity. Throw me a typical sample string and we'll see if behaviour is consistent.
Answer by phodges · Nov 11, 2012 at 08:33 PM
As we've established in the comments, the questioner was attempting to use www as a workaround to obtain a valid hash of a string. This is not necessary and the following sample code demonstrates how it might be done:
public static string ComputeHash(string s){
// Form hash
System.Security.Cryptography.MD5 h = System.Security.Cryptography.MD5.Create();
byte[] data = h.ComputeHash(System.Text.Encoding.Default.GetBytes(s));
// Create string representation
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < data.Length; ++i) {
sb.Append(data[i].ToString("x2"));
}
return sb.ToString();
}
Okay well I tried it, and unfortunately it still produces a different hash then what's in the database. $$anonymous$$aybe I'm not doing the concatenation correctly. In php I can just do ($salt + $password). I did the same in c#, does that not work?
I know the php md5 returns a hex variation of the bytes. Is there a similar function I can do in c#?
Okay using a different resource I was able to calculate the salt. But can't get a proper encryption when I do (salt+password);
The example function I provided creates a string representation of the hex. If you just want the raw bytes then skip the StringBuilder step. Tell me more about your salting method and I may be able to help. You could also edit your question to include code.
I think I found the best solution. Encrypt it once a.k.a the salt, send that and then encrypt it again on the other side.
Answer by Julien-Lynge · Nov 10, 2012 at 11:41 PM
You can't create coroutines from static functions, because when you're in the static function you don't have a reference to an actual instance of the monobehaviour script. You have two ways to fix it:
-If your class is a singleton (you have a single instance if it in-scene), include an Instance variable that points to the instance in scene. You can set it from the Start() or Awake() functions, e.g.
public static EncryptData Instance;
void Start()
{
Instance = this;
}
Or, you could use something like TaskManager, which gives you much more flexibility over using coroutines:
http://forum.unity3d.com/threads/94220-A-more-flexible-coroutine-interface
Thanks for the response. So I need to change this line of my code? public static class EncryptData : $$anonymous$$onoBehaviour
Okay I added the variable but am still getting but one error. On line 5 which is my class definition, that the static class can't derive from monobehavior