local function Q(Q,T,S,w)local t=block_size_for_HMAC[Q]if not t then error("\085\110\107\110\111\119\110 \104\097\115\104 \102\117\110\099\116\105\111\110",2)end if#T>t then T=string.gsub(Q(T),"\037\120\037\120",HexToBinFunction)end local j=(Q())(pad_and_xor(T,t,54))local A local function V(S)if not S then A=A or Q(pad_and_xor(T,t,92)..string.gsub(j(),"\037\120\037\120",HexToBinFunction))return A elseif A then error("\065\100\100\105\110\103 \109\111\114\101 \099\104\117\110\107\115 \105\115 \110\111\116 \097\108\108\111\119\101\100 \097\102\116\101\114 \114\101\099\101\105\118\105\110\103 \116\104\101 \114\101\115\117\108\116",2)else j(S)return V end end if S then local Q=(V(S))()return w and string.gsub(Q,"\037\120\037\120",BinaryStringMap)or Q else return V end end local T={md5=md5,sha1=sha1,sha224=function(Q)return sha256ext(224,Q)end;sha256=function(Q)return sha256ext(256,Q)end,sha512_224=function(Q)return sha512ext(224,Q)end;sha512_256=function(Q)return sha512ext(256,Q)end;sha384=function(Q)return sha512ext(384,Q)end,sha512=function(Q)return sha512ext(512,Q)end;sha3_224=function(Q)return keccak(144.0,28.0,false,Q)end,sha3_256=function(Q)return keccak(136.0,32.0,false,Q)end,sha3_384=function(Q)return keccak(104.0,48.0,false,Q)end;sha3_512=function(Q)return keccak(72.0,64.0,false,Q)end,shake128=function(Q,T)return keccak(168.0,T,true,Q)end;shake256=function(Q,T)return keccak(136.0,T,true,Q)end;hmac=Q;hex_to_bin=hex2bin,base64_to_bin=base642bin;bin_to_base64=bin2base64;base64_encode=Base64.Encode,base64_decode=Base64.Decode}block_size_for_HMAC={[T.md5]=64,[T.sha1]=64;[T.sha224]=64,[T.sha256]=64;[T.sha512_224]=128,[T.sha512_256]=128,[T.sha384]=128,[T.sha512]=128;[T.sha3_224]=144.0,[T.sha3_256]=136.0;[T.sha3_384]=104.0;[T.sha3_512]=72.0}return T
-- Update 02.14.20 - added AsBinary for easy GameAnalytics replacement.
local function hmac(hash_func, key, message, AsBinary)
	-- Create an instance (private objects for current calculation)
	local block_size = block_size_for_HMAC[hash_func]
	if not block_size then
		error("Unknown hash function", 2)
	end

	if #key > block_size then
		key = string.gsub(hash_func(key), "%x%x", HexToBinFunction)
		--key = hex2bin(hash_func(key))
	end

	local append = hash_func()(pad_and_xor(key, block_size, 0x36))
	local result

	local function partial(message_part)
		if not message_part then
			result = result
				or hash_func(pad_and_xor(key, block_size, 0x5C) .. (string.gsub(append(), "%x%x", HexToBinFunction)))
			return result
		elseif result then
			error("Adding more chunks is not allowed after receiving the result", 2)
		else
			append(message_part)
			return partial
		end
	end

	if message then
		-- Actually perform calculations and return the HMAC of a message
		local FinalMessage = partial(message)()
		return AsBinary and (string.gsub(FinalMessage, "%x%x", BinaryStringMap)) or FinalMessage
	else
		-- Return function for chunk-by-chunk loading of a message
		-- User should feed every chunk of the message as single argument to this function and finally get HMAC by invoking this function without an argument
		return partial
	end
end

local sha = {
	md5 = md5,
	sha1 = sha1,
	-- SHA2 hash functions:
	sha224 = function(message)
		return sha256ext(224, message)
	end,

	sha256 = function(message)
		return sha256ext(256, message)
	end,

	sha512_224 = function(message)
		return sha512ext(224, message)
	end,

	sha512_256 = function(message)
		return sha512ext(256, message)
	end,

	sha384 = function(message)
		return sha512ext(384, message)
	end,

	sha512 = function(message)
		return sha512ext(512, message)
	end,

	-- SHA3 hash functions:
	sha3_224 = function(message)
		return keccak((1600 - 2 * 224) / 8, 224 / 8, false, message)
	end,

	sha3_256 = function(message)
		return keccak((1600 - 2 * 256) / 8, 256 / 8, false, message)
	end,

	sha3_384 = function(message)
		return keccak((1600 - 2 * 384) / 8, 384 / 8, false, message)
	end,

	sha3_512 = function(message)
		return keccak((1600 - 2 * 512) / 8, 512 / 8, false, message)
	end,

	shake128 = function(message, digest_size_in_bytes)
		return keccak((1600 - 2 * 128) / 8, digest_size_in_bytes, true, message)
	end,

	shake256 = function(message, digest_size_in_bytes)
		return keccak((1600 - 2 * 256) / 8, digest_size_in_bytes, true, message)
	end,

	-- misc utilities:
	hmac = hmac, -- HMAC(hash_func, key, message) is applicable to any hash function from this module except SHAKE*
	hex_to_bin = hex2bin, -- converts hexadecimal representation to binary string
	base64_to_bin = base642bin, -- converts base64 representation to binary string
	bin_to_base64 = bin2base64,
	base64_encode = Base64.Encode,
	base64_decode = Base64.Decode,
	-- converts binary string to base64 representation
}

block_size_for_HMAC = {
	[sha.md5] = 64,
	[sha.sha1] = 64,
	[sha.sha224] = 64,
	[sha.sha256] = 64,
	[sha.sha512_224] = 128,
	[sha.sha512_256] = 128,
	[sha.sha384] = 128,
	[sha.sha512] = 128,
	[sha.sha3_224] = (1600 - 2 * 224) / 8,
	[sha.sha3_256] = (1600 - 2 * 256) / 8,
	[sha.sha3_384] = (1600 - 2 * 384) / 8,
	[sha.sha3_512] = (1600 - 2 * 512) / 8,
}

return sha
