Jump to content

Encryption method


BigBrother

Recommended Posts

I wrote a function which hashes a string and automatically creates a random key (salt) for you. It's pretty simple, and you can also provide your own salt generator function to use instead of the built-in one:

function md5_salt(str, static_salt, salt_func) 
    assert(str, "You need to pass a string to hash as first argument!") 
  
    local salt__new = salt_func 
    if not salt__new then 
        salt__new = function() 
            math.randomseed(getTickCount()) 
  
            local salt = {} 
            for i = 1, 80 do 
                salt[i] = string.char(math.random(126)) 
            end 
            return table.concat(salt, "") 
        end 
    end 
     
    if not static_salt or static_salt:len() == 0 then 
        outputDebugString("md5_salt: It is recommended that you use a static salt.") 
        static_salt = "" 
    end 
    local salt = salt__new() 
    return md5(("%s%s%s"):format(str, static_salt, salt)), salt 
end 

Example (using static salt with built-in salt generator):

local hash, salt = md5_salt("I'm an awesome string.", "in|  {}D(p?ok+9UzVi:_'UTG{[s")outputChatBox("Hash: " .. hash)outputChatBox("Salt: " .. salt)

Another example (using static salt with a different salt generator function):

local function salt__new()    math.randomseed(getTickCount())        local salt = {}    for i = 1, 20 do         salt[i] = string.char(math.random(63) * 2)    end    return table.concat(salt, "")end local hash, salt = md5_salt("I'm an awesome string.", "in|  {}D(p?ok+9UzVi:_'UTG{[s", salt__new)outputChatBox("Hash: " .. hash)outputChatBox("Salt: " .. salt)

Link to comment

improved ixjf's code

function md5_salt(str, static_salt, salt_func) 
    assert(str, "You need to pass a string to hash as first argument!") 
  
    local salt__new = salt_func or function() 
            math.randomseed(getTickCount()) 
  
            local salt = {} 
            for i = 1, 80 do 
                salt[i] = string.char(math.random(126)) 
            end 
            return table.concat(salt, "") 
        end 
    end 
     
    if not static_salt or static_salt:len() == 0 then 
        outputDebugString("md5_salt: It is recommended that you use a salt.") 
        static_salt = "" 
    end 
    local salt = salt__new() 
    return md5(("%s%s%s"):format(str, static_salt, salt)), salt 
end 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...