lua中实现的base64加密
2015-08-18 17:52
573 查看
[转载]原始地址 是http://www.buildapp.net/post/187.htm
function str2base64(str)
local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local s64 = ''
while #str > 0 do -- iterate through string
local bytes_num = 0 -- number of shifted bytes
local buf = 0 -- input buffer
for byte_cnt=1,3 do
buf = (buf * 256)
if #str > 0 then -- if string not empty, shift 1st byte to buf
buf = buf + string.byte(str, 1, 1)
str = string.sub(str, 2)
bytes_num = bytes_num + 1
end
end
for group_cnt=1,(bytes_num+1) do
b64char = math.fmod(math.floor(buf/262144), 64) + 1
s64 = s64 .. string.sub(b64chars, b64char, b64char)
buf = buf * 64
end
for fill_cnt=1,(3-bytes_num) do
s64 = s64 .. '='
end
end
return s64
end
function str2base64(str)
local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local s64 = ''
while #str > 0 do -- iterate through string
local bytes_num = 0 -- number of shifted bytes
local buf = 0 -- input buffer
for byte_cnt=1,3 do
buf = (buf * 256)
if #str > 0 then -- if string not empty, shift 1st byte to buf
buf = buf + string.byte(str, 1, 1)
str = string.sub(str, 2)
bytes_num = bytes_num + 1
end
end
for group_cnt=1,(bytes_num+1) do
b64char = math.fmod(math.floor(buf/262144), 64) + 1
s64 = s64 .. string.sub(b64chars, b64char, b64char)
buf = buf * 64
end
for fill_cnt=1,(3-bytes_num) do
s64 = s64 .. '='
end
end
return s64
end
相关文章推荐
- 【Lua 第一篇】 Manager + Data 的定义与应用
- Lua语言介绍
- ulua
- ****************本文是Lua编写的游戏接入多盟SDK*******************
- leetcode 150 —— Evaluate Reverse Polish Notation
- lua——元表、元方法、继承
- A*寻路算法lua实现
- Lua语言基础
- ngx_lua 模块API说明
- window下编译lua源码,编译lua的库文件,编译lua解释器,编译lua编译器
- Lua中的面向对象
- uLua拆分MonoBehavior逻辑
- lua_next函数分析
- Mac下 Lua 添加库 luafilesystem
- 用Lua扩展谷歌拼音输入法
- 【Lua】tolua
- Lua.LearningLua.7-userdata
- 【Lua】__newindex
- Lua学习笔记--环境配置&Hello World
- Lua.LearningLua.5-document-for-luaL_findtable-function