Is there a built-in lua hashing function?

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
Sgraffite
Posts: 38
Joined: Wed Jul 09, 2008 12:03 pm

Is there a built-in lua hashing function?

#1 Post by Sgraffite » Fri Jul 11, 2008 3:52 pm

I was wondering if MicroMacro/lua has an included hashing function akin to MD5, SHA1, etc? Or do I need to get a 3rd party library to do it?

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Is there a built-in lua hashing function?

#2 Post by Administrator » Fri Jul 11, 2008 5:28 pm

No, Lua does not natively have any sort of hashing function. For what purpose do you need it?

Typically, there's two types of hashing that are commonly used. One is generating a random hash to be used in place of a password (For, say, a login mechanism. You wouldn't want to store an actual password on the client side, so a server-generated random hash is fine). The other is when hashing a string always generates the same result.

A random generated hash can be made like this:

Code: Select all

-- Generates a random hash with the specified length.
-- If length is less than or equal to zero, or unspecified, 32 bytes will be assumed.
function random_hash(len)
  if( len == nil or len <= 0 ) then len = 32; end;
  local holder = ""; -- a string for holding our hash temporarily


  -- create an array of all acceptable characters that can be put in our hash.
  hash_chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E",
                "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
                "U", "V", "W", "X", "Y", "Z"};

  for i = 1, len do
    local index = math.random(1, #hash_chars);
    holder = holder .. hash_chars[index];
  end

  return holder;
end
Then use it like this:

Code: Select all

printf("Hash: %s", random_hash(16));
To generate a 16 byte random hash.


Now, the later type of hash also has two parts: reversible and one-way. A "reversible hash" would be more like an encryption or compression type function, where a one-way hash is like MD5. MicroMacro has a Rijndael encryption algorithm in it that could be used to create a reversible hash. As long as they don't have the key to it, it would not entirely be reversible.

Sgraffite
Posts: 38
Joined: Wed Jul 09, 2008 12:03 pm

Re: Is there a built-in lua hashing function?

#3 Post by Sgraffite » Fri Jul 11, 2008 5:45 pm

I was going to make a sort of pixel checksum to make sure I know if I ever switch targets, so the hash does not need to be reversible. Similar to getPixelChecksum() in autoit. I may not need this if target id stays the same after a character is killed and ressed repeatedly, but I have not tested that yet.

Basically I set it up where I have 3 healers, all who res a single level 1 char which I kill over and over. The problem arises when the character I'm killing gets disconnected as I will then auto-target and being slaughtering off the healers, which I don't want because then I have to run them all back.

So to prevent this I need a way that stops the script if the target ever changes.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: Is there a built-in lua hashing function?

#4 Post by Administrator » Fri Jul 11, 2008 6:03 pm

Essentially, you would be converting your bytes back into a 4-byte integer. That can be done pretty simply with math. Typically, the order is RGBA, but since we don't need the Alpha channel, we'll just kind of ignore it.

Code: Select all

function getPixelInt(hdc, x, y)
  local r, g, b = getPixel(hdc, x, y);
  local retval = (r * 256 * 256) + (g * 256) + b;
  return retval;
end
The reason I chose to return the three color channels as opposed to a single integer color value is because of slight variations. It's simple to set an allowed range for red to be +-10 on a red variable, however, it's not so easy to do if it's a single integer. I'd still suggest doing something like this instead:

Code: Select all

-- returns true if the two colors are within 'delta' variation
-- else, returns false
function compareColors(r1, g1, b1, r2, g2, b2, delta)
  -- if delta was not set, set it to 10.
  if( delta == nil ) then delta = 10; end;

  if( math.abs(r2 - r1) <= delta and
      math.abs(g2 - g1) <= delta and
      math.abs(b2 - b2) <= delta )
    return true;
  else
    return false;
end

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests