local n = 619155 local t = {} for i = 1, n do t[i] = string.char((i * 37) % 256) end local data = table.concat(t) local key = "0123456789abcdef0123456789abcdef" print("data", #data, "buffer", type(buffer), "crypt", type(crypt)) local enc = crypt.base64encode(data) print("enc", #enc) local t0 = os.clock() local dec = crypt.base64decode(enc) print("native_b64", string.format("%.3f", os.clock() - t0), #dec, dec == data) -- reference keystream table local KS = {} for i = 0, 511 do KS[i + 1] = string.char(bit32.bxor(string.byte(key, (i % #key) + 1), (i + 1) % 256)) end local KSS = table.concat(KS) -- buffer 4-byte version local t1 = os.clock() local src = buffer.fromstring(data) local dst = buffer.create(n) local ksb = buffer.fromstring(KSS) local i = 0 while i <= n - 4 do local a = buffer.readu32(src, i) local k = buffer.readu32(ksb, i % 256) local hi = math.floor((i + 1) / 256) % 256 local hi4 = hi + hi * 256 + hi * 65536 + hi * 16777216 buffer.writeu32(dst, i, bit32.bxor(a, k, hi4)) i = i + 4 end local out = buffer.tostring(dst) print("buffer_dec", string.format("%.3f", os.clock() - t1), #out) -- string.unpack 4-byte version local t2 = os.clock() local parts = {} local j = 0 while j <= n - 4 do local a = string.unpack("