Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
quick and easy way to hash strings? — Gideros Forum

quick and easy way to hash strings?

piepie Member
edited February 2013 in General questions
Hi, I'm sorry, I am a bit confused (and I am still a beginner to coding):

I am looking for a simple way to check the value of a saved string when it's loaded:

I suppose that this should be the workflow/logic:

On the "save side":

1)hash my string
2)append my hash(let's call it hash1) to the end of the string (maybe after some keyword to easily retrieve it by pattern find/parsing)
3)Save mystringvalue+hash1 to file.

On the load side:
1)load save file
2)Parse my string to get mystringvalue + hash1
3)hash2 = hash of mystringvalue (trimmed of hash1)
4)If hash 1 == hash2 then proceed.

I found this topic:

http://giderosmobile.com/forum/discussion/1227/md5-using-gideros-bitop-plugin

I didn't had a chance to try this method yet, but it seems to be a bit complex to me (unless I take it as a dogma.. :) ) and reading the included comments it seems to be a workaround more than being a "best practice" to do string hashing.

Is there any other method to achieve what I am trying to achieve?

Maybe something "easy" like:

string.md5(mystring)= hash1


Thank you in advance,

have a nice day!
:)
Tagged:

Comments

  • Hi, I'm sorry, I am a bit confused (and I am still a beginner to coding):

    I am looking for a simple way to check the value of a saved string when it's loaded:

    Can you elaborate by what you mean when you say "check the value", can't you just do a string compare? eg.

    if myString == "hello" then ... end ???

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • @techdojo thank you for your quick reply.


    I suppose that I will do this on my "load side" (step #4) comparing hash1 with hash2.

    But I am stuck on a previous step:
    how to quick hash a string?

    since I'd like to send "mystring" to another device (in the near future, not right now :) ) I'd like to be sure that the loaded string is exactly the same I saved.

    Does this make sense? :)
    Maybe I am thinking from the wrong point of view? (I am learning, any smarter suggestion is welcome)

    thank you very much :)
  • techdojotechdojo Guru
    Accepted Answer
    A quick google for "hash a string in lua" found this http://lua-users.org/wiki/SecureHashAlgorithm.
    Although I'm still not sure why you'd want to do this. Why not use tables instead.

    eg.
     local myString = "This is my string, that I want to generate a hash for!"
     local myTable = {}
     
     myTable[myString] = "An entry in the table, Lua already generates a hash of the string"
     
      if myTable[myString] then 
          print("Already set")
     end
    If you want to share a bit more about what your trying to achieve them I'm sure you'll get some pointers to guide you in the right way.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • Thank you again, I didn't find the lua secure hash algoritm on my googling.
    I'm going to try this asap.

    I'm sorry if I wasn't understable before :) I'll try to explain better:

    I am still making tests on features I'd like to implement in my first (and hope not last) game made with gideros.

    Basically I need to send player_1 turn (saved as a json string because it seems to be easier to manage than lua tables) to player_2 device.

    I was looking for a hash to add at the end of my "player_1 turn" string, to check if it has been sent correctly on player_2 device.

    Thank you very much.
    I will ask again if I'll get stuck :)




  • Ah I see, you might want to check out the Gideros multiplayer extension (search the forum),

    Depending on how your actually sending data (Wifi or Bluetooth) you might want to implement this kind of checking automatically at a lower level so as not to interfere with your game logic.

    Do you know if these kind of transmission errors are common, you might find that the lower level transport infrastructure automatically checks data validity and resends packets anyway.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • Thank you, I didn't know that :)
    But yesterday I came across the lua socket tutorial explaining that TCP already does some kind of integrity check on packets, so I should be safe if I choose this protocol.

    I think that this "hash thing" will come in handy next time (next steps), perhaps to check if the package is coming from a single, certain device.

    Thank you, I will check the multiplayer game extension for sure.
Sign In or Register to comment.