Shuffy

不断的学习,不断的思考,才能不断的进步.Let's do better together!
posts - 102, comments - 43, trackbacks - 0, articles - 19

微博短网址原理(ShortUrl)算法

Posted on 2011-11-04 14:06 Shuffy 阅读(1124) 评论(0)  编辑 收藏 引用 所属分类: Java

短网址一直都在微博上应用。例如腾讯微博的短网址url.cn,新浪的sinaurl.cn等。 他们是如何实现呢,本文将介绍一下该技术算法!

比如我们在腾讯微博上发布网址的时候,微博会自动判别网址,并将其转换,例如转换为:http://url.cn/3fVZf1

为什么要这样做的,原因有以下几点:

1、腾讯微博限制字数为140字一条,那么如果我们需要发一些连接上去,但是这个连接非常的长,以至于将近要占用我们内容的一半篇幅,这肯定是不能被允许的,所以短网址应运而生了。

2、短网址可以在我们项目里可以很好的对开放级URL进行管理。有一部分网址可以会涵盖seqing,暴力,广告等信息,这样我们可以通过用户的举报,完全管理这个连接将不出现在我们的应用中,应为同样的URL通过加密算法之后,得到的地址是一样的。

3、我们可以对一系列的网址进行流量,点击等统计,挖掘出大多数用户的关注点,这样有利于我们对项目的后续工作更好的作出决策。

其实以上三点纯属个人观点,因为在我接下来的部分项目中会应用到,所以就了解了一下,下面先来看看短网址映射算法的理论(网上找到的资料)

1)将长网址md5生成32位签名串,分为4段, 每段8个字节;
2)对这四段循环处理, 取8个字节, 将他看成16进制串与0x3fffffff(30位1)与操作, 即超过30位的忽略处理;
3)这30位分成6段, 每5位的数字作为字母表的索引取得特定字符, 依次进行获得6位字符串;
4)总的md5串可以获得4个6位串; 取里面的任意一个就可作为这个长url的短url地址;

很简单的理论,我们并不一定说得到的URL是唯一的,但是我们能够取出4组URL,这样几乎不会出现太大的重复。

下面来看看程序部分:


Java代码
public static string[] ShortUrl(string url) 

//可以自定义生成MD5加密字符传前的混合KEY 
string key = "Leejor"; 
//要使用生成URL的字符 
string[] chars = new string[]{
"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"
};
//对传入网址进行MD5加密
string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5"); 

string[] resUrl = new string[4];
for (int i = 0; i < 4; i++)

//把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16); 
string outChars = string.Empty;
for (int j = 0; j < 6; j++)
{
//把得到的值与0x0000003D进行位与运算,取得字符数组chars索引 
int index = 0x0000003D & hexint;
//把取得的字符相加 
outChars += chars[index]; 
//每次循环按位右移5位
hexint = hexint >> 5; 
}
//把字符串存入对应索引的输出数组 
resUrl[i] = outChars; 

 
return resUrl; 
}
public static string[] ShortUrl(string url)
{
//可以自定义生成MD5加密字符传前的混合KEY
string key = "Leejor";
//要使用生成URL的字符
string[] chars = new string[]{
"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"

};
//对传入网址进行MD5加密
string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5");

string[] resUrl = new string[4];

for (int i = 0; i < 4; i++)
{
//把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
string outChars = string.Empty;
for (int j = 0; j < 6; j++)
{
//把得到的值与0x0000003D进行位与运算,取得字符数组chars索引
int index = 0x0000003D & hexint;
//把取得的字符相加
outChars += chars[index];
//每次循环按位右移5位
hexint = hexint >> 5;
}
//把字符串存入对应索引的输出数组
resUrl[i] = outChars;
}

return resUrl;
}

现在可以直接使用该方法,可以等到下面四组值

ShortUrl(http://www.me3.cn")[0]; //得到值fAVfui

ShortUrl("http://www.me3.cn")[1]; //得到值3ayQry

ShortUrl("http://www.me3.cn")[2]; //得到值UZzyUr

ShortUrl("http://www.me3.cn")[3]; //得到值36rQZn

原文地址:http://haohaoker-163-com.iteye.com/blog/1094692


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理