将中间结果存储在LuaOpenResty中的临时变量中会得到不同的结果
我遇到了一件非常奇怪的事情,想知道发生了什么。出于某种原因,如果我在lua中设置一个临时变量来保存中间结果,我会得到不同的结果 我有以下代码:将中间结果存储在LuaOpenResty中的临时变量中会得到不同的结果,lua,openresty,Lua,Openresty,我遇到了一件非常奇怪的事情,想知道发生了什么。出于某种原因,如果我在lua中设置一个临时变量来保存中间结果,我会得到不同的结果 我有以下代码: local random = require("resty.random") local token = ngx.encode_base64(random.bytes(32)) print("len(" .. string.len(token) .. ") " .. token) -- > len(43) OUOoBKfxLZDtE7yrHFzT
local random = require("resty.random")
local token = ngx.encode_base64(random.bytes(32))
print("len(" .. string.len(token) .. ") " .. token) -- > len(43) OUOoBKfxLZDtE7yrHFzThF2e7dc6Wtjmzz3C6lQC67I
它不返回有效的base64字符串。缺少=且字符串仅为43个字符
但是如果我做了下面的事情,它就会起作用
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=
base64字符串是有效的。该字符串为44个字符,结尾为=
这可能是什么原因造成的。我很奇怪,两种代码之间的结果会不同。我已经在MacOSX和Ubuntu16.04LTS上的OpenResty1.15.8.2上证实了这一点 有第二个参数,该参数打开无填充模式。所以我认为随机函数返回第二个真值。我建议试着像调用
ngx.encode\u base64((random.bytes(32))
可能,由random.bytes(32)
返回的垃圾回收对象在ngx.encode\u base64()
之前得到垃圾回收(用C编写的函数)已完成。这可能是encode\u base64
问题,因为您生成的是随机序列,它取决于输入。您可以使用固定数据复制此问题吗?请尝试ngx.encode\u base64((random.bytes(32))
。可能存在第二个返回值,并且base64生成url安全的b64