# signature { license=0, seed=457995617, key="39992fd58410b0a43e77964cb724f233" }
local sha1 = {
	_VERSION		 = "sha1 1.0.5",
	_URL			 = "https://github.com/kikito/sha.lua",
	_DESCRIPTION = [[
SHA-1 secure hash computation in Lua (5.2)
Based on code originally by Jeffrey Friedl (http://regex.info/blog/lua/sha1)
And modified by Eike Decker - (http://cube3d.de/uploads/Main/sha1.txt)
And modified by Oliver Kroth to use the Lua 5.2 bit32 and md5.exor functions.
]],
	_LICENSE = [[
MIT LICENSE

Copyright (c) 2013 Enrique Garcia Cota + Eike Decker + Jeffrey Friedl

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
}

local md5 = require("md5") -- for the exor


-- local storing of global functions (minor speedup)
local char,format,rep = string.char,string.format,string.rep

local band	= bit32.band
local bor	= bit32.bor
local bxor	= bit32.bxor
local bnot	= bit32.bnot
local brotl = bit32.lrotate
local shr   = bit32.rshift
local shl   = bit32.lshift

-- merge 4 bytes to an 32 bit word
local function bytes_to_w32(a,b,c,d) return bor( shl(a,24), shl(b,16), shl(c,8), d) end

local function w32_to_bin(w32) return char( band(255,shr(w32,24)),band(255,shr(w32,16)),band(255,shr(w32,8)),band(255,w32)) end


-- calculating the SHA1 for some text
function sha1.sha1(msg,hex)
	local H0,H1,H2,H3,H4 = 0x67452301,0xEFCDAB89,0x98BADCFE,0x10325476,0xC3D2E1F0
	local msg_len_in_bits = #msg * 8

	local non_zero_message_bytes = #msg +1 +8 -- the +1 is the appended bit 1, the +8 are for the final appended length
	local current_mod = non_zero_message_bytes % 64
	local second_append = current_mod>0 and rep(char(0), 64 - current_mod) or ""

	-- now to append the length as a 64-bit number.
	local B1 = band( 0xFF, shr(msg_len_in_bits,24))
	local B2 = band( 0xFF, shr(msg_len_in_bits,16))
	local B3 = band( 0xFF, shr(msg_len_in_bits,8))
	local B4 = band( 0xFF,     msg_len_in_bits)

	local L64 = "\0\0\0\0" .. char(B1) .. char(B2) .. char(B3) .. char(B4) --	low 32 bits

	-- append a '1' bit plus seven '0' bits plus more 0-bits plus 64 bit length
	msg = msg .. "\x80" .. second_append .. L64

	assert(#msg % 64 == 0)

	local chunks = #msg / 64

	local W = { }
	local start, A, B, C, D, E, f, K, TEMP
	local chunk = 0

	while chunk < chunks do
		-- break chunk up into W[0] through W[15]
		start,chunk = chunk * 64 + 1,chunk + 1

		for t = 0, 15 do
			W[t] = bytes_to_w32(msg:byte(start, start + 3))
			start = start + 4
		end

		-- build W[16] through W[79]
		for t = 16, 79 do
			-- For t = 16 to 79 let Wt = S1(Wt-3 XOR Wt-8 XOR Wt-14 XOR Wt-16).
			W[t] = brotl(bxor( W[t-3], W[t-8], W[t-14], W[t-16] ), 1 )
		end

		A,B,C,D,E = H0,H1,H2,H3,H4

		for t = 0, 79 do
			if t <= 19 then
				-- (B AND C) OR ((NOT B) AND D)
				f = bor(band(B, C), band(bnot(B), D))
				K = 0x5A827999
			elseif t <= 39 then
				-- B XOR C XOR D
				f = bxor(B, C, D)
				K = 0x6ED9EBA1
			elseif t <= 59 then
				-- (B AND C) OR (B AND D) OR (C AND D
				f = bor(band(B, C), band(B, D), band(C, D))
				K = 0x8F1BBCDC
			else
				-- B XOR C XOR D
				f = bxor(B, C, D)
				K = 0xCA62C1D6
			end

			-- TEMP = S5(A) + ft(B,C,D) + E + Wt + Kt;
			A,B,C,D,E = bor(brotl(A, 5)+ f+ E+ W[t]+ K), A, brotl(B,30), C, D
		end
		-- Let H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
		H0,H1,H2,H3,H4 = bor(H0+A), bor(H1+B), bor(H2+C), bor(H3+D), bor(H4+E)
	end
	local out = format("%08x%08x%08x%08x%08x",H0, H1, H2, H3, H4)
	if hex then return out end
	return ( out:gsub('..', function(x) return string.char(tonumber(x, 16)) end) )
end

setmetatable(sha1, {__call = function(_,msg,hex) return sha1.sha1(msg,hex) end })
sha1.BLOCK_SIZE = 64

return sha1
