Difference between revisions of "Snippets"

From SolarStrike wiki
Jump to: navigation, search
(Random hash generator)
m (Random hash generator)
Line 8: Line 8:
  
 
'''random_hash()'''
 
'''random_hash()'''
 +
  
 
'''Description: '''
 
'''Description: '''

Revision as of 09:25, 17 July 2008

Note:

Code posted here is considered Public Domain unless otherwise noted. Be aware of this before posting your code! You may release your code here only under the following licenses: Public Domain, BSD, GPL, or LGPL.


Random hash generator

random_hash(len)

random_hash()


Description: Generates a random filled hash containing numbers and (capitol) letters. It will generate and return a hash that is 'len' characters long. If 'len' is not specified, then a 32 character hash will be assumed.

Purpose: Random hashes are, often, used in place of passwords. It is considered more secure than storing a reversible hash simply because it cannot be reversed (as it contains no real information). For example, after a successful login, a server might generate a random hash for that specific user. The client (user) will now use the random hash for future authentication instead of the password.

License: Public Domain

Author: "Elverion"

-- 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.
  local 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