local md5 = require "md5.core"
function md5.sumhexa(k)
k = md5.sum(k)
return (string.gsub(k, ".", function (c)
return string.format("%02x", string.byte(c))
end))
end
local SHORT_BASE = {
"a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" ,
"m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ,
"y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" ,
"A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" ,
"M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" ,
"Y" , "Z"
}
local SHORT_PRE = "short_pre"
function short_url(str)
local m = md5.sumhexa(SHORT_PRE..str)
local ret = {}
for k=0,3 do
local sub = string.sub(m, k*8+1, k*8+8)
local int = 0x3fffffff & tonumber(sub, 16)
local out = ''
for j=1,6 do
local v = ( 0x3d & int) + 1
out = out .. SHORT_BASE[v]
int = int >> 5
end
table.insert(ret, out)
end
return ret
end
local ret = short_url("hello world")
for k,v in pairs(ret) do
print("short_url", v)
end
注意了,lua比较特殊,数组下标从1开始。结果如下:
另外,再提供可让用户输入的短地址版本。就是只保留大写字母和数字,同时去掉了容易混淆的O0I1 。算法类似,可做激活码。
local md5 = require "md5.core"
function md5.sumhexa(k)
k = md5.sum(k)
return (string.gsub(k, ".", function (c)
return string.format("%02x", string.byte(c))
end))
end
local SHORT_BASE = {
'A','B','C','D','E','F','G','H',
'J','K','L','M','N','P','Q','R',
'S','T','U','V','W','X','Y','Z',
'2','3','4','5','6','7','8','9',
}
local SHORT_PRE = "short_pre"
function short_url(str)
local m = md5.sumhexa(SHORT_PRE..str)
local ret = {}
for k=0,3 do
local sub = string.sub(m, k*8+1, k*8+8)
local int = 0x3fffffff & tonumber(sub, 16)
local out = ''
for j=1,6 do
local v = ( 0x1f & int) + 1
out = out .. SHORT_BASE[v]
int = int >> 5
end
table.insert(ret, out)
end
return ret
end
local ret = short_url("hello world")
for k,v in pairs(ret) do
print("short_url", v)
end
结果如下:
参考:
http://blog.csdn.net/mycwq/article/details/52326266