mirror of
https://git.phreedom.club/localhost_frssoft/fediauth.git
synced 2025-01-08 00:51:14 +00:00
migrate to native minetest bitwise operations (except basexx)
Thanks https://forum.minetest.net/viewtopic.php?p=429240#p429240
This commit is contained in:
parent
09b949c755
commit
8bd6172234
|
@ -1,65 +1,14 @@
|
||||||
|
|
||||||
-- https://stackoverflow.com/a/25594410
|
|
||||||
local function bitxor(a,b)
|
|
||||||
local p,c=1,0
|
|
||||||
while a>0 and b>0 do
|
|
||||||
local ra,rb=a%2,b%2
|
|
||||||
if ra~=rb then c=c+p end
|
|
||||||
a,b,p=(a-ra)/2,(b-rb)/2,p*2
|
|
||||||
end
|
|
||||||
if a<b then a=b end
|
|
||||||
while a>0 do
|
|
||||||
local ra=a%2
|
|
||||||
if ra>0 then c=c+p end
|
|
||||||
a,p=(a-ra)/2,p*2
|
|
||||||
end
|
|
||||||
return c
|
|
||||||
end
|
|
||||||
|
|
||||||
local function bitor(a,b)
|
|
||||||
local p,c=1,0
|
|
||||||
while a+b>0 do
|
|
||||||
local ra,rb=a%2,b%2
|
|
||||||
if ra+rb>0 then c=c+p end
|
|
||||||
a,b,p=(a-ra)/2,(b-rb)/2,p*2
|
|
||||||
end
|
|
||||||
return c
|
|
||||||
end
|
|
||||||
|
|
||||||
-- https://stackoverflow.com/a/32387452
|
|
||||||
local function bitand(a, b)
|
|
||||||
local result = 0
|
|
||||||
local bitval = 1
|
|
||||||
while a > 0 and b > 0 do
|
|
||||||
if a % 2 == 1 and b % 2 == 1 then -- test the rightmost bits
|
|
||||||
result = result + bitval -- set the current bit
|
|
||||||
end
|
|
||||||
bitval = bitval * 2 -- shift left
|
|
||||||
a = math.floor(a/2) -- shift right
|
|
||||||
b = math.floor(b/2)
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
-- https://gist.github.com/mebens/938502
|
|
||||||
local function rshift(x, by)
|
|
||||||
return math.floor(x / 2 ^ by)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function lshift(x, by)
|
|
||||||
return x * 2 ^ by
|
|
||||||
end
|
|
||||||
|
|
||||||
-- big-endian uint64 of a number
|
-- big-endian uint64 of a number
|
||||||
function fediauth.write_uint64_be(v)
|
function fediauth.write_uint64_be(v)
|
||||||
local b1 = bitand( rshift(v, 56), 0xFF )
|
local b1 = bit.band( bit.rshift(v, 56), 0xFF )
|
||||||
local b2 = bitand( rshift(v, 48), 0xFF )
|
local b2 = bit.band( bit.rshift(v, 48), 0xFF )
|
||||||
local b3 = bitand( rshift(v, 40), 0xFF )
|
local b3 = bit.band( bit.rshift(v, 40), 0xFF )
|
||||||
local b4 = bitand( rshift(v, 32), 0xFF )
|
local b4 = bit.band( bit.rshift(v, 32), 0xFF )
|
||||||
local b5 = bitand( rshift(v, 24), 0xFF )
|
local b5 = bit.band( bit.rshift(v, 24), 0xFF )
|
||||||
local b6 = bitand( rshift(v, 16), 0xFF )
|
local b6 = bit.band( bit.rshift(v, 16), 0xFF )
|
||||||
local b7 = bitand( rshift(v, 8), 0xFF )
|
local b7 = bit.band( bit.rshift(v, 8), 0xFF )
|
||||||
local b8 = bitand( rshift(v, 0), 0xFF )
|
local b8 = bit.band( bit.rshift(v, 0), 0xFF )
|
||||||
return string.char(b1, b2, b3, b4, b5, b6, b7, b8)
|
return string.char(b1, b2, b3, b4, b5, b6, b7, b8)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -76,13 +25,13 @@ end
|
||||||
function fediauth.hmac(key, message)
|
function fediauth.hmac(key, message)
|
||||||
local i_key_pad = ""
|
local i_key_pad = ""
|
||||||
for i=1,64 do
|
for i=1,64 do
|
||||||
i_key_pad = i_key_pad .. string.char(bitxor(string.byte(key, i) or 0x00, string.byte(i_pad, i)))
|
i_key_pad = i_key_pad .. string.char(bit.bxor(string.byte(key, i) or 0x00, string.byte(i_pad, i)))
|
||||||
end
|
end
|
||||||
assert(#i_key_pad == 64)
|
assert(#i_key_pad == 64)
|
||||||
|
|
||||||
local o_key_pad = ""
|
local o_key_pad = ""
|
||||||
for i=1,64 do
|
for i=1,64 do
|
||||||
o_key_pad = o_key_pad .. string.char(bitxor(string.byte(key, i) or 0x00, string.byte(o_pad, i)))
|
o_key_pad = o_key_pad .. string.char(bit.bxor(string.byte(key, i) or 0x00, string.byte(o_pad, i)))
|
||||||
end
|
end
|
||||||
assert(#o_key_pad == 64)
|
assert(#o_key_pad == 64)
|
||||||
|
|
||||||
|
@ -129,12 +78,12 @@ function fediauth.generate_tfediauth(secret_b32, unix_time)
|
||||||
local hmac = fediauth.hmac(key, counter)
|
local hmac = fediauth.hmac(key, counter)
|
||||||
|
|
||||||
-- https://www.rfc-editor.org/rfc/rfc4226#section-5.4
|
-- https://www.rfc-editor.org/rfc/rfc4226#section-5.4
|
||||||
local offset = bitand(string.byte(hmac, #hmac), 0xF)
|
local offset = bit.band(string.byte(hmac, #hmac), 0xF)
|
||||||
local value = 0
|
local value = 0
|
||||||
value = bitor(value, string.byte(hmac, offset+4))
|
value = bit.bxor(value, string.byte(hmac, offset+4))
|
||||||
value = bitor(value, lshift(string.byte(hmac, offset+3), 8))
|
value = bit.bxor(value, bit.lshift(string.byte(hmac, offset+3), 8))
|
||||||
value = bitor(value, lshift(string.byte(hmac, offset+2), 16))
|
value = bit.bxor(value, bit.lshift(string.byte(hmac, offset+2), 16))
|
||||||
value = bitor(value, lshift(bitand(string.byte(hmac, offset+1), 0x7F), 24))
|
value = bit.bxor(value, bit.lshift(bit.band(string.byte(hmac, offset+1), 0x7F), 24))
|
||||||
local code = value % 10^6
|
local code = value % 10^6
|
||||||
local padded_code = left_pad("" .. code, "0", 6)
|
local padded_code = left_pad("" .. code, "0", 6)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue