lua 屏蔽密码功能实现

来源:互联网 发布:杭州网络教育学校 编辑:程序博客网 时间:2024/05/16 09:16

给定字符串,如果出现脱敏关键字,则屏蔽密码对应的数字

local source = "pwd\":\"12333123ad\"}\"apwd\":\"1233sd3123\"pwd:123321,adpwd1233fgfsdgs"

local desensitizeKeyArray = { "PWD", "USER_PWD", "pwd", "AUTH_INFO", "REQ_AUTH_INFO",
"REQ_EXT_ACC_PWD", "EXT_ACC_PWD" };


function desensitizeStr(source,desensitizeKeyArray)
for i=1,#desensitizeKeyArray do


local regex = "(%A*)"..desensitizeKeyArray[i].."(%D*)".."(%d+)"


local m = string.match(source, regex)
if m then
--source = string.gsub(source,regex,replaceStr(%5))
source = string.gsub(source,regex,"%1"..desensitizeKeyArray[i].."%2".."***")
--print("source:"..source)
return source
--break
end
end
end
local res = desensitizeStr(source,desensitizeKeyArray)
print("res:"..res)