1. Post #1
    Gold Member
    Gbps's Avatar
    December 2008
    3,641 Posts
    Does anyone know of any which are freely avaliable? It doesn't have to be too complicated.

  2. Post #2
    Gold Member
    Nevec's Avatar
    June 2008
    1,093 Posts
    A really simple one is the rot function:

    local function Rotate( str, num )
    	
    	return ( str:gsub( ".", function( char )
    	
    		local byte = char:byte( ) + num;
    		
    		if( byte < 0 ) then
    			
    			byte = 255 + byte;
    			
    		elseif( byte > 255 ) then
    			
    			byte = byte - 255;
    			
    		end
    		
    		return string.char( byte );
    		
    	end ) );
    	
    end
    
    function string:encrypt( code )
    	
    	return Rotate( self, code or 20 );
    	
    end
    
    function string:decrypt( code )
    	
    	return Rotate( self, code or -20 );
    	
    end

    You can probably find something more complex here.
    Reply With Quote Edit / Delete Reply Latvia Show Events Disagree Disagree x 1Informative Informative x 1 (list)

  3. Post #3
    Gold Member
    aVoN's Avatar
    December 2005
    2,880 Posts
    But be warned: rot brings no safety at all.

    Better use other ciphers.
    I found this one here http://luaforge.net/projects/aeslua/
    But it still requires bitlib which is a binary module.
    Reply With Quote Edit / Delete Reply Germany Show Events Informative Informative x 1 (list)

  4. Post #4
    |FlapJack|'s Avatar
    July 2009
    6,138 Posts
    I've got a Sha1 encryption algorithm in pure Lua - but obviously it can't be decrypted. I've also got Fla1 which I wrote myself, it looks impossible to crack but there's thousands of collisions. I'll post the Sha1 algorithm when I find it, Fla1 if requested.

  5. Post #5
    Gold Member
    aVoN's Avatar
    December 2005
    2,880 Posts
    I think you even could emulate the bitwise operations in GMod with bare lua, so my posted link to the lua-AES implementation should work.

    Easiest way for bitwise: Get a char, make it a number-stream of ones and zeroes and use bitwise operations on it.

  6. Post #6
    |FlapJack|'s Avatar
    July 2009
    6,138 Posts
    Found the Sha1 (including bitlib in Lua)

    Not by me, obviously.

    -------------------------------------------------
    ---      *** BitLibEmu for Lua ***            ---
    -------------------------------------------------
    --- Author:  Martin Huesser                   ---
    --- Date:    2008-06-16                       ---
    --- License: You may use this code in your    ---
    ---          projects as long as this header  ---
    ---          stays intact.                    ---
    -------------------------------------------------
    
    local mod   = math.fmod
    local floor = math.floor
    bit = {}
    
    ----------------------------------------
    
    local function cap(x)
    	return mod(x,4294967296)
    end
    
    ----------------------------------------
    
    function bit.bnot(x)
    	return 4294967295-cap(x)
    end
    
    ----------------------------------------
    
    function bit.lshift(x,n)
    	return cap(cap(x)*2^n)
    end
    
    ----------------------------------------
    
    function bit.rshift(x,n)
    	return floor(cap(x)/2^n)
    end
    
    ----------------------------------------
    
    function bit.band(x,y)
    	local z,i,j = 0,1
    	for j = 0,31 do
    		if (mod(x,2)==1 and mod(y,2)==1) then
    			z = z + i
    		end
    		x = bit.rshift(x,1)
    		y = bit.rshift(y,1)
    		i = i*2
    	end
    	return z
    end
    
    ----------------------------------------
    
    function bit.bor(x,y)
    	local z,i,j = 0,1
    	for j = 0,31 do
    		if (mod(x,2)==1 or mod(y,2)==1) then
    			z = z + i
    		end
    		x = bit.rshift(x,1)
    		y = bit.rshift(y,1)
    		i = i*2
    	end
    	return z
    end
    
    ----------------------------------------
    
    function bit.bxor(x,y)
    	local z,i,j = 0,1
    	for j = 0,31 do
    		if (mod(x,2)~=mod(y,2)) then
    			z = z + i
    		end
    		x = bit.rshift(x,1)
    		y = bit.rshift(y,1)
    		i = i*2
    	end
    	return z
    end
    
    -------------------------------------------------
    ---      *** SHA-1 algorithm for Lua ***      ---
    -------------------------------------------------
    --- Author:  Martin Huesser                   ---
    --- Date:    2008-06-16                       ---
    --- License: You may use this code in your    ---
    ---          projects as long as this header  ---
    ---          stays intact.                    ---
    -------------------------------------------------
    
    local strlen  = string.len
    local strchar = string.char
    local strbyte = string.byte
    local strsub  = string.sub
    local floor   = math.floor
    local bnot    = bit.bnot
    local band    = bit.band
    local bor     = bit.bor
    local bxor    = bit.bxor
    local shl     = bit.lshift
    local shr     = bit.rshift
    local h0, h1, h2, h3, h4
    
    -------------------------------------------------
    
    local function LeftRotate(val, nr)
    	return shl(val, nr) + shr(val, 32 - nr)
    end
    
    -------------------------------------------------
    
    local function ToHex(num)
    	local i, d
    	local str = ""
    	for i = 1, 8 do
    		d = band(num, 15)
    		if (d < 10) then
    			str = strchar(d + 48) .. str
    		else
    			str = strchar(d + 87) .. str
    		end
    		num = floor(num / 16)
    	end
    	return str
    end
    
    -------------------------------------------------
    
    local function PreProcess(str)
    	local bitlen, i
    	local str2 = ""
    	bitlen = strlen(str) * 8
    	str = str .. strchar(128)
    	i = 56 - band(strlen(str), 63)
    	if (i < 0) then
    		i = i + 64
    	end
    	for i = 1, i do
    		str = str .. strchar(0)
    	end
    	for i = 1, 8 do
    		str2 = strchar(band(bitlen, 255)) .. str2
    		bitlen = floor(bitlen / 256)
    	end
    	return str .. str2
    end
    
    -------------------------------------------------
    
    local function MainLoop(str)
    	local a, b, c, d, e, f, k, t
    	local i, j
    	local w = {}
    	while (str ~= "") do
    		for i = 0, 15 do
    			w[i] = 0
    			for j = 1, 4 do
    				w[i] = w[i] * 256 + strbyte(str, i * 4 + j)
    			end
    		end
    		for i = 16, 79 do
    			w[i] = LeftRotate(bxor(bxor(w[i - 3], w[i - 8]), bxor(w[i - 14], w[i - 16])), 1)
    		end
    		a = h0
    		b = h1
    		c = h2
    		d = h3
    		e = h4
    		for i = 0, 79 do
    			if (i < 20) then
    				f = bor(band(b, c), band(bnot(b), d))
    				k = 1518500249
    			elseif (i < 40) then
    				f = bxor(bxor(b, c), d)
    				k = 1859775393
    			elseif (i < 60) then
    				f = bor(bor(band(b, c), band(b, d)), band(c, d))
    				k = 2400959708
    			else
    				f = bxor(bxor(b, c), d)
    				k = 3395469782
    			end
    			t = LeftRotate(a, 5) + f + e + k + w[i]	
    			e = d
    			d = c
    			c = LeftRotate(b, 30)
    			b = a
    			a = t
    		end
    		h0 = band(h0 + a, 4294967295)
    		h1 = band(h1 + b, 4294967295)
    		h2 = band(h2 + c, 4294967295)
    		h3 = band(h3 + d, 4294967295)
    		h4 = band(h4 + e, 4294967295)
    		str = strsub(str, 65)
    	end
    end
    
    -------------------------------------------------
    
    function Sha1(str)
    	str = PreProcess(str)
    	h0  = 1732584193
    	h1  = 4023233417
    	h2  = 2562383102
    	h3  = 0271733878
    	h4  = 3285377520
    	MainLoop(str)
    	return  ToHex(h0) ..
    		ToHex(h1) ..
    		ToHex(h2) ..
    		ToHex(h3) ..
    		ToHex(h4)
    end
    
    -------------------------------------------------
    -------------------------------------------------
    -------------------------------------------------

  7. Post #7
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    SHA is not encryption. It is a hashing function.

    If you want encryption, it would all depend on the situation you want to use it in.

    local function XOR(a, b)
    	return (a | b) - (a & b)
    end

    That's as simple as it gets.

    a is your number and b is the key.

    local encrypted = XOR(1337, 83830)
    
    encrypted == 82511
    
    local decrypted = XOR(82511, 83830)
    
    decrypted == 1337
    

    This is the most basic encryption. It will provide you will little protection.

  8. Post #8
    Gold Member
    aVoN's Avatar
    December 2005
    2,880 Posts
    @haza
    Are there bitwise operations in GMod now? I thought there aren't.

    If not, they can be implemented in a slow manner mapping chars to a boolean table in bare lua and doing the operations then in lua.

  9. Post #9
    Reply With Quote Edit / Delete Reply United Kingdom Show Events Informative Informative x 1 (list)

  10. Post #10
    Box collector
    haza55's Avatar
    October 2005
    545 Posts
    @haza
    Are there bitwise operations in GMod now? I thought there aren't.

    If not, they can be implemented in a slow manner mapping chars to a boolean table in bare lua and doing the operations then in lua.
    They have been in gmod for awhile, just not exclusive or which is (a | b) - (a & b).

    EDIT:
    Bit shifting sucks thou, barely works. But that can all be done like this:
    local function BitShiftLeft(num, bits)
    	return num * 2 ^ bits
    end
    
    local function BitShiftRight(num, bits)
    	return math.floor(num / (2 ^ bits))
    end