aboutsummaryrefslogtreecommitdiffstats
path: root/server/resty/session/encoders/base16.lua
blob: 552f50eb9d460f482813aea3484613e1634f3404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
local to_hex   = require "resty.string".to_hex

local tonumber = tonumber
local gsub     = string.gsub
local char     = string.char

local function chr(c)
    return char(tonumber(c, 16) or 0)
end

local encoder = {}

function encoder.encode(value)
    if not value then
        return nil, "unable to base16 encode value"
    end

    return to_hex(value)
end

function encoder.decode(value)
    if not value then
        return nil, "unable to base16 decode value"
    end

    return (gsub(value, "..", chr))
end

return encoder