Mercurial > prosody-hg
comparison util/paseto.lua @ 12838:2e71b76ac299
util.paseto: Stricter base64 decoding, as per spec
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 13 Jan 2023 14:35:01 +0000 |
| parents | 0b68b021ce46 |
| children | 7db1c1da7bfd |
comparison
equal
deleted
inserted
replaced
| 12837:d3ae47d8a7a7 | 12838:2e71b76ac299 |
|---|---|
| 12 | 12 |
| 13 local b64url_rep = { ["+"] = "-", ["/"] = "_", ["="] = "", ["-"] = "+", ["_"] = "/" }; | 13 local b64url_rep = { ["+"] = "-", ["/"] = "_", ["="] = "", ["-"] = "+", ["_"] = "/" }; |
| 14 local function b64url(data) | 14 local function b64url(data) |
| 15 return (s_gsub(base64_encode(data), "[+/=]", b64url_rep)); | 15 return (s_gsub(base64_encode(data), "[+/=]", b64url_rep)); |
| 16 end | 16 end |
| 17 | |
| 18 local valid_tails = { | |
| 19 nil; -- Always invalid | |
| 20 "^.[AQgw]$"; -- b??????00 | |
| 21 "^..[AQgwEUk0IYo4Mcs8]$"; -- b????0000 | |
| 22 } | |
| 23 | |
| 17 local function unb64url(data) | 24 local function unb64url(data) |
| 25 local rem = #data%4; | |
| 26 if data:sub(-1,-1) == "=" or rem == 1 or (rem > 1 and not data:sub(-rem):match(valid_tails[rem])) then | |
| 27 return nil; | |
| 28 end | |
| 18 return base64_decode(s_gsub(data, "[-_]", b64url_rep).."=="); | 29 return base64_decode(s_gsub(data, "[-_]", b64url_rep).."=="); |
| 19 end | 30 end |
| 20 | 31 |
| 21 local function le64(n) | 32 local function le64(n) |
| 22 return s_pack("<I8", bit.band(n, 0x7F)); | 33 return s_pack("<I8", bit.band(n, 0x7F)); |
