local function O(O,z)local s=sha2_H_ext256[O]local c,N=0,""local B=table.create(8)B[1],B[2],B[3],B[4],B[5],B[6],B[7],B[8]=s[1],s[2],s[3],s[4],s[5],s[6],s[7],s[8]local function u(z)if z then local O=#z if N then c=c+O local s=0 local x=#N if N~=""and x+O>=64 then s=64-x sha256_feed_64(B,N..string.sub(z,1,s),0,64)N=""end local y=O-s local b=y%64 sha256_feed_64(B,z,s,y-b)N=N..string.sub(z,(O+1)-b)return u else 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)end else if N then local z=table.create(10)z[1]=N z[2]="\128"z[3]=string.rep("\000",(-9-c)%64+1)N=nil c=c*(8/TWO56_POW_7)for O=4,10,1 do c=(c%1)*256 z[O]=string.char(math.floor(c))end z=table.concat(z)sha256_feed_64(B,z,0,#z)local s=O/32 for O=1,s,1 do B[O]=string.format("\037\048\056\120",B[O]%4294967296)end B=table.concat(B,"",1,s)end return B end end if z then return(u(z))()else return u end end local function z(O,z)local s,c,N,B=0,"",table.pack(table.unpack(sha2_H_ext512_lo[O])),not HEX64 and table.pack(table.unpack(sha2_H_ext512_hi[O]))local function u(z)if z then local O=#z if c then s=s+O local x=0 if c~=""and#c+O>=128 then x=128-#c sha512_feed_128(N,B,c..string.sub(z,1,x),0,128)c=""end local y=O-x local b=y%128 sha512_feed_128(N,B,z,x,y-b)c=c..string.sub(z,(O+1)-b)return u else 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)end else if c then local z=table.create(3)z[1]=c z[2]="\128"z[3]=string.rep("\000",(-17-s)%128+9)c=nil s=s*(8/TWO56_POW_7)for O=4,10,1 do s=(s%1)*256 z[O]=string.char(math.floor(s))end z=table.concat(z)sha512_feed_128(N,B,z,0,#z)local u=math.ceil(O/64)if HEX64 then for O=1,u,1 do N[O]=HEX64(N[O])end else for O=1,u,1 do N[O]=string.format("\037\048\056\120",B[O]%4294967296)..string.format("\037\048\056\120",N[O]%4294967296)end B=nil end N=string.sub(table.concat(N,"",1,u),1,O/4)end return N end end if z then return(u(z))()else return u end end local function s(O)local z,s,c=table.create(4),0,""z[1],z[2],z[3],z[4]=md5_sha1_H[1],md5_sha1_H[2],md5_sha1_H[3],md5_sha1_H[4]local function N(O)if O then local B=#O if c then s=s+B local u=0 if c~=""and#c+B>=64 then u=64-#c md5_feed_64(z,c..string.sub(O,1,u),0,64)c=""end local x=B-u local y=x%64 md5_feed_64(z,O,u,x-y)c=c..string.sub(O,(B+1)-y)return N else 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)end else if c then local O=table.create(3)O[1]=c O[2]="\128"O[3]=string.rep("\000",(-9-s)%64)c=nil s=s*8 for z=4,11,1 do local c=s%256 O[z]=string.char(c)s=(s-c)/256 end O=table.concat(O)md5_feed_64(z,O,0,#O)for O=1,4,1 do z[O]=string.format("\037\048\056\120",z[O]%4294967296)end z=string.gsub(table.concat(z),"\040\046\046\041\040\046\046\041\040\046\046\041\040\046\046\041","\037\052\037\051\037\050\037\049")end return z end end if O then return(N(O))()else return N end end local function c(O)local z,s,c=table.pack(table.unpack(md5_sha1_H)),0,""local function N(O)if O then local B=#O if c then s=s+B local u=0 if c~=""and#c+B>=64 then u=64-#c sha1_feed_64(z,c..string.sub(O,1,u),0,64)c=""end local x=B-u local y=x%64 sha1_feed_64(z,O,u,x-y)c=c..string.sub(O,(B+1)-y)return N else 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)end else if c then local O=table.create(10)O[1]=c O[2]="\128"O[3]=string.rep("\000",(-9-s)%64+1)c=nil s=s*(8/TWO56_POW_7)for z=4,10,1 do s=(s%1)*256 O[z]=string.char(math.floor(s))end O=table.concat(O)sha1_feed_64(z,O,0,#O)for O=1,5,1 do z[O]=string.format("\037\048\056\120",z[O]%4294967296)end z=table.concat(z)end return z end end if O then return(N(O))()else return N end end local function N(O,z,s,c)if type(z)~="\110\117\109\098\101\114"then error("\065\114\103\117\109\101\110\116 \'\100\105\103\101\115\116\095\115\105\122\101\095\105\110\095\098\121\116\101\115\' \109\117\115\116 \098\101 \097 \110\117\109\098\101\114",2)end local N,B,u="",table.create(25,0),hi_factor_keccak==0 and table.create(25,0)local x local function y(c)if c then local z=#c if N then local s=0 if N~=""and#N+z>=O then s=O-#N keccak_feed(B,u,N..string.sub(c,1,s),0,O,O)N=""end local x=z-s local b=x%O keccak_feed(B,u,c,s,x-b,O)N=N..string.sub(c,(z+1)-b)return y else 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)end else if N then local c=s and 31 or 6 N=N..(#N+1==O and string.char(c+128)or string.char(c)..(string.rep("\000",(-2-#N)%O).."\128"))keccak_feed(B,u,N,0,#N,O)N=nil local y=0 local b=math.floor(O/8)local p={}local function i(O)if y>=b then keccak_feed(B,u,"\000\000\000\000\000\000\000\000",0,8,8)y=0 end O=math.floor(math.min(O,b-y))if hi_factor_keccak~=0 then for O=1,O,1 do p[O]=HEX64(B[((y+O)-1)+lanes_index_base])end else for O=1,O,1 do p[O]=string.format("\037\048\056\120",u[y+O]%4294967296)..string.format("\037\048\056\120",B[y+O]%4294967296)end end y=y+O return string.gsub(table.concat(p,"",1,O),"\040\046\046\041\040\046\046\041\040\046\046\041\040\046\046\041\040\046\046\041\040\046\046\041\040\046\046\041\040\046\046\041","\037\056\037\055\037\054\037\053\037\052\037\051\037\050\037\049"),O*8 end local e={}local H,I="",0 local function A(O)O=O or 1 if O<=I then I=I-O local z=O*2 local s=string.sub(H,1,z)H=string.sub(H,z+1)return s end local z=0 if I>0 then z=1 e[z]=H O=O-I end while O>=8 do local s,c=i(O/8)z=z+1 e[z]=s O=O-c end if O>0 then H,I=i(1)z=z+1 e[z]=A(O)else H,I="",0 end return table.concat(e,"",1,z)end if z<0 then x=A else x=A(z)end end return x end end if c then return(y(c))()else return y end end local function B(O)return string.char(tonumber(O,16))end local function u(O)return string.gsub(O,"\037\120\037\120",B)end local x={["\043"]=62,["-"]=62,[62]="\043",["\047"]=63,_=63;[63]="\047",["\061"]=-1;["\046"]=-1,[-1]="\061"}local y=0 for O,z in ipairs({"\065\090","\097\122","\048\057"})do for O=string.byte(z),string.byte(z,2),1 do local z=string.char(O)x[z]=y x[y]=z y=y+1 end end local function b(O)local z=#O local s=table.create(math.ceil(z/3))local c=0 for z=1,#O,3 do local N,B,u,y=string.byte(string.sub(O,z,z+2).."\000",1,-1)c=c+1 s[c]=x[math.floor(N/4)]..(x[(N%4)*16+math.floor(B/16)]..(x[u and(B%16)*4+math.floor(u/64)or-1]..x[y and u%64 or-1]))end return table.concat(s)end local function p(O)local z,s={},3 for O,c in string.gmatch(string.gsub(O,"\037\115\043",""),"\040\041\040\046\041")do local N=x[c]if N<0 then s=s-1 N=0 end local B=O%4 if B>0 then z[-B]=N else local O=z[-1]*4+math.floor(z[-2]/16)local c=(z[-2]%16)*16+math.floor(z[-3]/4)local B=(z[-3]%4)*64+N z[#z+1]=string.sub(string.char(O,c,B),1,s)end end return table.concat(z)end local i

local function sha256ext(width, message)
	-- Create an instance (private objects for current calculation)
	local Array256 = sha2_H_ext256[width] -- # == 8
	local length, tail = 0, ""
	local H = table.create(8)
	H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8] = Array256[1], Array256[2], Array256[3], Array256[4], Array256[5], Array256[6], Array256[7], Array256[8]

	local function partial(message_part)
		if message_part then
			local partLength = #message_part
			if tail then
				length = length + partLength
				local offs = 0
				local tailLength = #tail
				if tail ~= "" and tailLength + partLength >= 64 then
					offs = 64 - tailLength
					sha256_feed_64(H, tail .. string.sub(message_part, 1, offs), 0, 64)
					tail = ""
				end

				local size = partLength - offs
				local size_tail = size % 64
				sha256_feed_64(H, message_part, offs, size - size_tail)
				tail = tail .. string.sub(message_part, partLength + 1 - size_tail)
				return partial
			else
				error("Adding more chunks is not allowed after receiving the result", 2)
			end
		else
			if tail then
				local final_blocks = table.create(10) --{tail, "\128", string.rep("\0", (-9 - length) % 64 + 1)}
				final_blocks[1] = tail
				final_blocks[2] = "\128"
				final_blocks[3] = string.rep("\0", (-9 - length) % 64 + 1)

				tail = nil
				-- Assuming user data length is shorter than (TWO_POW_53)-9 bytes
				-- Anyway, it looks very unrealistic that someone would spend more than a year of calculations to process TWO_POW_53 bytes of data by using this Lua script :-)
				-- TWO_POW_53 bytes = TWO_POW_56 bits, so "bit-counter" fits in 7 bytes
				length = length * (8 / TWO56_POW_7) -- convert "byte-counter" to "bit-counter" and move decimal point to the left
				for j = 4, 10 do
					length = length % 1 * 256
					final_blocks[j] = string.char(math.floor(length))
				end

				final_blocks = table.concat(final_blocks)
				sha256_feed_64(H, final_blocks, 0, #final_blocks)
				local max_reg = width / 32
				for j = 1, max_reg do
					H[j] = string.format("%08x", H[j] % 4294967296)
				end

				H = table.concat(H, "", 1, max_reg)
			end

			return H
		end
	end

	if message then
		-- Actually perform calculations and return the SHA256 digest of a message
		return partial(message)()
	else
		-- Return function for chunk-by-chunk loading
		-- User should feed every chunk of input data as single argument to this function and finally get SHA256 digest by invoking this function without an argument
		return partial
	end
end

local function sha512ext(width, message)

	-- Create an instance (private objects for current calculation)
	local length, tail, H_lo, H_hi = 0, "", table.pack(table.unpack(sha2_H_ext512_lo[width])), not HEX64 and table.pack(table.unpack(sha2_H_ext512_hi[width]))

	local function partial(message_part)
		if message_part then
			local partLength = #message_part
			if tail then
				length = length + partLength
				local offs = 0
				if tail ~= "" and #tail + partLength >= 128 then
					offs = 128 - #tail
					sha512_feed_128(H_lo, H_hi, tail .. string.sub(message_part, 1, offs), 0, 128)
					tail = ""
				end

				local size = partLength - offs
				local size_tail = size % 128
				sha512_feed_128(H_lo, H_hi, message_part, offs, size - size_tail)
				tail = tail .. string.sub(message_part, partLength + 1 - size_tail)
				return partial
			else
				error("Adding more chunks is not allowed after receiving the result", 2)
			end
		else
			if tail then
				local final_blocks = table.create(3) --{tail, "\128", string.rep("\0", (-17-length) % 128 + 9)}
				final_blocks[1] = tail
				final_blocks[2] = "\128"
				final_blocks[3] = string.rep("\0", (-17 - length) % 128 + 9)

				tail = nil
				-- Assuming user data length is shorter than (TWO_POW_53)-17 bytes
				-- TWO_POW_53 bytes = TWO_POW_56 bits, so "bit-counter" fits in 7 bytes
				length = length * (8 / TWO56_POW_7) -- convert "byte-counter" to "bit-counter" and move floating point to the left
				for j = 4, 10 do
					length = length % 1 * 256
					final_blocks[j] = string.char(math.floor(length))
				end

				final_blocks = table.concat(final_blocks)
				sha512_feed_128(H_lo, H_hi, final_blocks, 0, #final_blocks)
				local max_reg = math.ceil(width / 64)

				if HEX64 then
					for j = 1, max_reg do
						H_lo[j] = HEX64(H_lo[j])
					end
				else
					for j = 1, max_reg do
						H_lo[j] = string.format("%08x", H_hi[j] % 4294967296) .. string.format("%08x", H_lo[j] % 4294967296)
					end

					H_hi = nil
				end

				H_lo = string.sub(table.concat(H_lo, "", 1, max_reg), 1, width / 4)
			end

			return H_lo
		end
	end

	if message then
		-- Actually perform calculations and return the SHA512 digest of a message
		return partial(message)()
	else
		-- Return function for chunk-by-chunk loading
		-- User should feed every chunk of input data as single argument to this function and finally get SHA512 digest by invoking this function without an argument
		return partial
	end
end

local function md5(message)

	-- Create an instance (private objects for current calculation)
	local H, length, tail = table.create(4), 0, ""
	H[1], H[2], H[3], H[4] = md5_sha1_H[1], md5_sha1_H[2], md5_sha1_H[3], md5_sha1_H[4]

	local function partial(message_part)
		if message_part then
			local partLength = #message_part
			if tail then
				length = length + partLength
				local offs = 0
				if tail ~= "" and #tail + partLength >= 64 then
					offs = 64 - #tail
					md5_feed_64(H, tail .. string.sub(message_part, 1, offs), 0, 64)
					tail = ""
				end

				local size = partLength - offs
				local size_tail = size % 64
				md5_feed_64(H, message_part, offs, size - size_tail)
				tail = tail .. string.sub(message_part, partLength + 1 - size_tail)
				return partial
			else
				error("Adding more chunks is not allowed after receiving the result", 2)
			end
		else
			if tail then
				local final_blocks = table.create(3) --{tail, "\128", string.rep("\0", (-9 - length) % 64)}
				final_blocks[1] = tail
				final_blocks[2] = "\128"
				final_blocks[3] = string.rep("\0", (-9 - length) % 64)
				tail = nil
				length = length * 8 -- convert "byte-counter" to "bit-counter"
				for j = 4, 11 do
					local low_byte = length % 256
					final_blocks[j] = string.char(low_byte)
					length = (length - low_byte) / 256
				end

				final_blocks = table.concat(final_blocks)
				md5_feed_64(H, final_blocks, 0, #final_blocks)
				for j = 1, 4 do
					H[j] = string.format("%08x", H[j] % 4294967296)
				end

				H = string.gsub(table.concat(H), "(..)(..)(..)(..)", "%4%3%2%1")
			end

			return H
		end
	end

	if message then
		-- Actually perform calculations and return the MD5 digest of a message
		return partial(message)()
	else
		-- Return function for chunk-by-chunk loading
		-- User should feed every chunk of input data as single argument to this function and finally get MD5 digest by invoking this function without an argument
		return partial
	end
end

local function sha1(message)
	-- Create an instance (private objects for current calculation)
	local H, length, tail = table.pack(table.unpack(md5_sha1_H)), 0, ""

	local function partial(message_part)
		if message_part then
			local partLength = #message_part
			if tail then
				length = length + partLength
				local offs = 0
				if tail ~= "" and #tail + partLength >= 64 then
					offs = 64 - #tail
					sha1_feed_64(H, tail .. string.sub(message_part, 1, offs), 0, 64)
					tail = ""
				end

				local size = partLength - offs
				local size_tail = size % 64
				sha1_feed_64(H, message_part, offs, size - size_tail)
				tail = tail .. string.sub(message_part, partLength + 1 - size_tail)
				return partial
			else
				error("Adding more chunks is not allowed after receiving the result", 2)
			end
		else
			if tail then
				local final_blocks = table.create(10) --{tail, "\128", string.rep("\0", (-9 - length) % 64 + 1)}
				final_blocks[1] = tail
				final_blocks[2] = "\128"
				final_blocks[3] = string.rep("\0", (-9 - length) % 64 + 1)
				tail = nil

				-- Assuming user data length is shorter than (TWO_POW_53)-9 bytes
				-- TWO_POW_53 bytes = TWO_POW_56 bits, so "bit-counter" fits in 7 bytes
				length = length * (8 / TWO56_POW_7) -- convert "byte-counter" to "bit-counter" and move decimal point to the left
				for j = 4, 10 do
					length = length % 1 * 256
					final_blocks[j] = string.char(math.floor(length))
				end

				final_blocks = table.concat(final_blocks)
				sha1_feed_64(H, final_blocks, 0, #final_blocks)
				for j = 1, 5 do
					H[j] = string.format("%08x", H[j] % 4294967296)
				end

				H = table.concat(H)
			end

			return H
		end
	end

	if message then
		-- Actually perform calculations and return the SHA-1 digest of a message
		return partial(message)()
	else
		-- Return function for chunk-by-chunk loading
		-- User should feed every chunk of input data as single argument to this function and finally get SHA-1 digest by invoking this function without an argument
		return partial
	end
end

local function keccak(block_size_in_bytes, digest_size_in_bytes, is_SHAKE, message)
	-- "block_size_in_bytes" is multiple of 8
	if type(digest_size_in_bytes) ~= "number" then
		-- arguments in SHAKE are swapped:
		--    NIST FIPS 202 defines SHAKE(message,num_bits)
		--    this module   defines SHAKE(num_bytes,message)
		-- it's easy to forget about this swap, hence the check
		error("Argument 'digest_size_in_bytes' must be a number", 2)
	end

	-- Create an instance (private objects for current calculation)
	local tail, lanes_lo, lanes_hi = "", table.create(25, 0), hi_factor_keccak == 0 and table.create(25, 0)
	local result

	--~     pad the input N using the pad function, yielding a padded bit string P with a length divisible by r (such that n = len(P)/r is integer),
	--~     break P into n consecutive r-bit pieces P0, ..., Pn-1 (last is zero-padded)
	--~     initialize the state S to a string of b 0 bits.
	--~     absorb the input into the state: For each block Pi,
	--~         extend Pi at the end by a string of c 0 bits, yielding one of length b,
	--~         XOR that with S and
	--~         apply the block permutation f to the result, yielding a new state S
	--~     initialize Z to be the empty string
	--~     while the length of Z is less than d:
	--~         append the first r bits of S to Z
	--~         if Z is still less than d bits long, apply f to S, yielding a new state S.
	--~     truncate Z to d bits
	local function partial(message_part)
		if message_part then
			local partLength = #message_part
			if tail then
				local offs = 0
				if tail ~= "" and #tail + partLength >= block_size_in_bytes then
					offs = block_size_in_bytes - #tail
					keccak_feed(lanes_lo, lanes_hi, tail .. string.sub(message_part, 1, offs), 0, block_size_in_bytes, block_size_in_bytes)
					tail = ""
				end

				local size = partLength - offs
				local size_tail = size % block_size_in_bytes
				keccak_feed(lanes_lo, lanes_hi, message_part, offs, size - size_tail, block_size_in_bytes)
				tail = tail .. string.sub(message_part, partLength + 1 - size_tail)
				return partial
			else
				error("Adding more chunks is not allowed after receiving the result", 2)
			end
		else
			if tail then
				-- append the following bits to the message: for usual SHA3: 011(0*)1, for SHAKE: 11111(0*)1
				local gap_start = is_SHAKE and 31 or 6
				tail = tail .. (#tail + 1 == block_size_in_bytes and string.char(gap_start + 128) or string.char(gap_start) .. string.rep("\0", (-2 - #tail) % block_size_in_bytes) .. "\128")
				keccak_feed(lanes_lo, lanes_hi, tail, 0, #tail, block_size_in_bytes)
				tail = nil

				local lanes_used = 0
				local total_lanes = math.floor(block_size_in_bytes / 8)
				local qwords = {}

				local function get_next_qwords_of_digest(qwords_qty)
					-- returns not more than 'qwords_qty' qwords ('qwords_qty' might be non-integer)
					-- doesn't go across keccak-buffer boundary
					-- block_size_in_bytes is a multiple of 8, so, keccak-buffer contains integer number of qwords
					if lanes_used >= total_lanes then
						keccak_feed(lanes_lo, lanes_hi, "\0\0\0\0\0\0\0\0", 0, 8, 8)
						lanes_used = 0
					end

					qwords_qty = math.floor(math.min(qwords_qty, total_lanes - lanes_used))
					if hi_factor_keccak ~= 0 then
						for j = 1, qwords_qty do
							qwords[j] = HEX64(lanes_lo[lanes_used + j - 1 + lanes_index_base])
						end
					else
						for j = 1, qwords_qty do
							qwords[j] = string.format("%08x", lanes_hi[lanes_used + j] % 4294967296) .. string.format("%08x", lanes_lo[lanes_used + j] % 4294967296)
						end
					end

					lanes_used = lanes_used + qwords_qty
					return string.gsub(table.concat(qwords, "", 1, qwords_qty), "(..)(..)(..)(..)(..)(..)(..)(..)", "%8%7%6%5%4%3%2%1"), qwords_qty * 8
				end

				local parts = {} -- digest parts
				local last_part, last_part_size = "", 0

				local function get_next_part_of_digest(bytes_needed)
					-- returns 'bytes_needed' bytes, for arbitrary integer 'bytes_needed'
					bytes_needed = bytes_needed or 1
					if bytes_needed <= last_part_size then
						last_part_size = last_part_size - bytes_needed
						local part_size_in_nibbles = bytes_needed * 2
						local result = string.sub(last_part, 1, part_size_in_nibbles)
						last_part = string.sub(last_part, part_size_in_nibbles + 1)
						return result
					end

					local parts_qty = 0
					if last_part_size > 0 then
						parts_qty = 1
						parts[parts_qty] = last_part
						bytes_needed = bytes_needed - last_part_size
					end

					-- repeats until the length is enough
					while bytes_needed >= 8 do
						local next_part, next_part_size = get_next_qwords_of_digest(bytes_needed / 8)
						parts_qty = parts_qty + 1
						parts[parts_qty] = next_part
						bytes_needed = bytes_needed - next_part_size
					end

					if bytes_needed > 0 then
						last_part, last_part_size = get_next_qwords_of_digest(1)
						parts_qty = parts_qty + 1
						parts[parts_qty] = get_next_part_of_digest(bytes_needed)
					else
						last_part, last_part_size = "", 0
					end

					return table.concat(parts, "", 1, parts_qty)
				end

				if digest_size_in_bytes < 0 then
					result = get_next_part_of_digest
				else
					result = get_next_part_of_digest(digest_size_in_bytes)
				end

			end

			return result
		end
	end

	if message then
		-- Actually perform calculations and return the SHA3 digest of a message
		return partial(message)()
	else
		-- Return function for chunk-by-chunk loading
		-- User should feed every chunk of input data as single argument to this function and finally get SHA3 digest by invoking this function without an argument
		return partial
	end
end

local function HexToBinFunction(hh)
	return string.char(tonumber(hh, 16))
end

local function hex2bin(hex_string)
	return (string.gsub(hex_string, "%x%x", HexToBinFunction))
end

local base64_symbols = {
	["+"] = 62, ["-"] = 62, [62] = "+";
	["/"] = 63, ["_"] = 63, [63] = "/";
	["="] = -1, ["."] = -1, [-1] = "=";
}

local symbol_index = 0
for j, pair in ipairs{"AZ", "az", "09"} do
	for ascii = string.byte(pair), string.byte(pair, 2) do
		local ch = string.char(ascii)
		base64_symbols[ch] = symbol_index
		base64_symbols[symbol_index] = ch
		symbol_index = symbol_index + 1
	end
end

local function bin2base64(binary_string)
	local stringLength = #binary_string
	local result = table.create(math.ceil(stringLength / 3))
	local length = 0

	for pos = 1, #binary_string, 3 do
		local c1, c2, c3, c4 = string.byte(string.sub(binary_string, pos, pos + 2) .. '\0', 1, -1)
		length = length + 1
		result[length] =
			base64_symbols[math.floor(c1 / 4)] ..
			base64_symbols[c1 % 4 * 16 + math.floor(c2 / 16)] ..
			base64_symbols[c3 and c2 % 16 * 4 + math.floor(c3 / 64) or -1] ..
			base64_symbols[c4 and c3 % 64 or -1]
	end

	return table.concat(result)
end

local function base642bin(base64_string)
	local result, chars_qty = {}, 3
	for pos, ch in string.gmatch(string.gsub(base64_string, "%s+", ""), "()(.)") do
		local code = base64_symbols[ch]
		if code < 0 then
			chars_qty = chars_qty - 1
			code = 0
		end

		local idx = pos % 4
		if idx > 0 then
			result[-idx] = code
		else
			local c1 = result[-1] * 4 + math.floor(result[-2] / 16)
			local c2 = (result[-2] % 16) * 16 + math.floor(result[-3] / 4)
			local c3 = (result[-3] % 4) * 64 + code
			result[#result + 1] = string.sub(string.char(c1, c2, c3), 1, chars_qty)
		end
	end

	return table.concat(result)
end

local block_size_for_HMAC -- this table will be initialized at the end of the module