在 lua openresty 中将中间结果存储在临时变量中会给出不同的结果
Storing intermediary result in a temporary variable in lua openresty gives a different result
我 运行 非常感兴趣,运行 想了解发生了什么。出于某种原因,如果我在 lua 中设置一个临时变量来保存中间结果,我会得到不同的结果。
我有以下代码:
local random = require("resty.random")
local token = ngx.encode_base64(random.bytes(32))
print("len(" .. string.len(token) .. ") " .. token) -- > len(43) OUOoBKfxLZDtE7yrHFzThF2e7dc6Wtjmzz3C6lQC67I
它 return 不是有效的 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 个字符,末尾为 =。
可能是什么原因造成的。两种代码的结果不同,这让我感到非常震惊运行。我已经确认 mac os x 和 ubuntu 16.04 lts 上的 openresty 1.15.8.2 都是这种情况。
encode_base64 有第二个参数,它打开无填充模式。所以我认为你的随机函数 returns 第二个真值。我建议尝试将其命名为 ngx.encode_base64((random.bytes(32))
我 运行 非常感兴趣,运行 想了解发生了什么。出于某种原因,如果我在 lua 中设置一个临时变量来保存中间结果,我会得到不同的结果。
我有以下代码:
local random = require("resty.random")
local token = ngx.encode_base64(random.bytes(32))
print("len(" .. string.len(token) .. ") " .. token) -- > len(43) OUOoBKfxLZDtE7yrHFzThF2e7dc6Wtjmzz3C6lQC67I
它 return 不是有效的 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 个字符,末尾为 =。
可能是什么原因造成的。两种代码的结果不同,这让我感到非常震惊运行。我已经确认 mac os x 和 ubuntu 16.04 lts 上的 openresty 1.15.8.2 都是这种情况。
encode_base64 有第二个参数,它打开无填充模式。所以我认为你的随机函数 returns 第二个真值。我建议尝试将其命名为 ngx.encode_base64((random.bytes(32))