Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kmccmk9 · Nov 10, 2012 at 10:56 PM · c#staticfunctions

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();
         }
     }
 }
Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phodges · Nov 11, 2012 at 08:41 AM 0
Share

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.

avatar image kmccmk9 · Nov 11, 2012 at 07:47 PM 0
Share

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.

avatar image phodges · Nov 11, 2012 at 07:49 PM 0
Share

That's happened before. Take a look at this question and answer on stack overflow:

http://stackoverflow.com/questions/190198/why-isnt-my-net-calculated-md5-hash-equivalent-to-the-hash-calculated-on-a-web

avatar image kmccmk9 · Nov 11, 2012 at 07:58 PM 0
Share

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.

avatar image phodges · Nov 11, 2012 at 08:07 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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();
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kmccmk9 · Nov 16, 2012 at 12:28 AM 0
Share

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?

avatar image kmccmk9 · Nov 16, 2012 at 12:30 AM 0
Share

I know the php md5 returns a hex variation of the bytes. Is there a similar function I can do in c#?

avatar image kmccmk9 · Nov 16, 2012 at 12:58 AM 0
Share

Okay using a different resource I was able to calculate the salt. But can't get a proper encryption when I do (salt+password);

avatar image phodges · Nov 16, 2012 at 07:42 AM 0
Share

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.

avatar image kmccmk9 · Nov 16, 2012 at 08:47 PM 0
Share

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.

Show more comments
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kmccmk9 · Nov 11, 2012 at 08:07 AM 0
Share

Thanks for the response. So I need to change this line of my code? public static class EncryptData : $$anonymous$$onoBehaviour

avatar image kmccmk9 · Nov 11, 2012 at 08:13 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling non-static from static function C# 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I make one input activate multiple functions by context? 1 Answer

an object reference is required for the non static field error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges