I ran into something very strange and would like to understand what's going on. For some reason, I get a different result if I set a temporary variable in lua to hold an intermediary result.
I have the following code:
local random = require("resty.random")
local token = ngx.encode_base64(random.bytes(32))
print("len(" .. string.len(token) .. ") " .. token) -- > len(43) OUOoBKfxLZDtE7yrHFzThF2e7dc6Wtjmzz3C6lQC67I
It doesn't return a valid base64 string. The = is missing and the string is only 43 characters.
But if I do the following, it works
local random = require("resty.random")
local bytes = random.bytes(32)
local token = ngx.encode_base64(bytes)
print("len(" .. string.len(token) .. ") " .. token) -- > len(44) 1E49IwlcsyfIBEwWBRXhTV2eFrc7QyYoFZ0kC1OsuTM=
The base64 string is valid. The string is 44 characters with = at the end.
What could be causing this. It's very strange to me that the result between both codes would be different. I've confirmed that to be the case on openresty 1.15.8.2 on both mac os x and ubuntu 16.04 lts.
random.bytes(32)gets garbage-collected beforengx.encode_base64()(function written in C) is finished. - Egor Skriptunoffencode_base64issue, as you generates random sequence and it depends on input. Can you reproduce this with fixed data? - Dariusngx.encode_base64((random.bytes(32))). May be there second return value and base64 produces url safe b64 - moteus