C# Google PageRank .net库 (checksum)
今天看到Google PageRank ChecksumPHP代码才发现上次发布的库写的很烂,我根本没有去理解里面的意思,有很多函数是没有必要的,如<< >>位操作都已经是现成的,我怎么还照着vb代码改写,真是愚蠢。
为了弥补我的愚蠢,把php代码改编成的.net库,发布出来。
Google PageRank 链接库下载地址:
Google PageRank .net 库
演示地址:
查询PageRank
使用实例:
//得到Pagerank值
Console.WriteLine(PRCrack.PageRank.CheckPR("http://www.google.com"));
//得到pagerank checksum
Console.WriteLine(PRCrack.PageRank.OutputCheckSum("http://www.google.com",1));
包含函数:
//输出指定网址的pagerank checksum
//参数m_Version指定1,表示得到新版本checksum,Google Toolbar版本>>=2.0.114
//参数m_Version指定0,表示得到老版本checksum,Google Toolbar版本<2.0.114
public static string OutputCheckSum(string m_Url,int m_Version)
//输出制定网址的pagerank
public static string CheckPR(string m_Url)
Checksum 算法源代码请看详细内容
为了弥补我的愚蠢,把php代码改编成的.net库,发布出来。
Google PageRank 链接库下载地址:
Google PageRank .net 库
演示地址:
查询PageRank
使用实例:
//得到Pagerank值
Console.WriteLine(PRCrack.PageRank.CheckPR("http://www.google.com"));
//得到pagerank checksum
Console.WriteLine(PRCrack.PageRank.OutputCheckSum("http://www.google.com",1));
包含函数:
//输出指定网址的pagerank checksum
//参数m_Version指定1,表示得到新版本checksum,Google Toolbar版本>>=2.0.114
//参数m_Version指定0,表示得到老版本checksum,Google Toolbar版本<2.0.114
public static string OutputCheckSum(string m_Url,int m_Version)
//输出制定网址的pagerank
public static string CheckPR(string m_Url)
Checksum 算法源代码请看详细内容
1using System; 2 3namespace PRCrack 4{ 5 /// <summary> 6 /// Google PageRank的Checksum算法。 7 /// 作者:4111y80y 8 /// 日期:2005年1月22日 9 /// email:sillyboy@china.com 10 /// </summary> 11 class CheckSum 12 { 13 public CheckSum() 14 { 15 // 16 // TODO: 在此处添加构造函数逻辑 17 // 18 } 19 20 uint GOOGLE_MAGIC=0xE6359A60; 21 22 private uint zeroFill(uint a, int b) 23 { 24 checked 25 { 26 uint z = 0x80000000; 27 if (Convert.ToBoolean(z & a)) 28 { 29 a = (a>>1); 30 a &= (~z); 31 a |= 0x40000000; 32 a = (a>>(b-1)); 33 } 34 else 35 { 36 a = (a>>b); 37 } 38 } 39 return a; 40 } 41 42 private uint[] mix(uint a,uint b,uint c) 43 { 44 a -= b; a -= c; a ^= (uint)(zeroFill(c,13)); 45 b -= c; b -= a; b ^= (uint)(a<<8); 46 c -= a; c -= b; c ^= (uint)(zeroFill(b,13)); 47 a -= b; a -= c; a ^= (uint)(zeroFill(c,12)); 48 b -= c; b -= a; b ^= (uint)(a<<16); 49 c -= a; c -= b; c ^= (uint)(zeroFill(b,5)); 50 a -= b; a -= c; a ^= (uint)(zeroFill(c,3)); 51 b -= c; b -= a; b ^= (uint)(a<<10); 52 c -= a; c -= b; c ^= (uint)(zeroFill(b,15)); 53 54 return new uint[3]{a,b,c}; 55 } 56 57 private uint GoogleCH(uint[] url, uint length, uint init) 58 { 59 if(length==0) 60 { 61 length = (uint)url.Length; 62 } 63 uint a ,b; 64 a=b= 0x9E3779B9; 65 uint c = init; 66 int k = 0; 67 uint len = length; 68 uint[] m_mix=new uint[3]; 69 while(len >= 12) 70 { 71 a += (uint)(url[k+0] +(url[k+1]<<8) +(url[k+2]<<16) +(url[k+3]<<24)); 72 73 b += (uint)(url[k+4] +(url[k+5]<<8) +(url[k+6]<<16) +(url[k+7]<<24)); 74 c += (uint)(url[k+8] +(url[k+9]<<8) +(url[k+10]<<16)+(url[k+11]<<24)); 75 m_mix = mix(a,b,c); 76 a = m_mix[0]; b = m_mix[1]; c = m_mix[2]; 77 78 k += 12; 79 len -= 12; 80 } 81 82 c += length; 83 84 switch(len) /* all the case statements fall through */ 85 { 86 case 11: 87 { 88 c+=(uint)(url[k+10]<<24); 89 c+=(uint)(url[k+9]<<16); 90 c+=(uint)(url[k+8]<<8); 91 b+=(uint)(url[k+7]<<24); 92 b+=(uint)(url[k+6]<<16); 93 b+=(uint)(url[k+5]<<8); 94 b+=(uint)(url[k+4]); 95 a+=(uint)(url[k+3]<<24); 96 a+=(uint)(url[k+2]<<16); 97 a+=(uint)(url[k+1]<<8); 98 a+=(uint)(url[k+0]); 99 break; 100 } 101 case 10: 102 { 103 c+=(uint)(url[k+9]<<16); 104 c+=(uint)(url[k+8]<<8); 105 b+=(uint)(url[k+7]<<24); 106 b+=(uint)(url[k+6]<<16); 107 b+=(uint)(url[k+5]<<8); 108 b+=(uint)(url[k+4]); 109 a+=(uint)(url[k+3]<<24); 110 a+=(uint)(url[k+2]<<16); 111 a+=(uint)(url[k+1]<<8); 112 a+=(uint)(url[k+0]); 113 break; 114 } 115 case 9 : 116 { 117 c+=(uint)(url[k+8]<<8); 118 b+=(uint)(url[k+7]<<24); 119 b+=(uint)(url[k+6]<<16); 120 b+=(uint)(url[k+5]<<8); 121 b+=(uint)(url[k+4]); 122 a+=(uint)(url[k+3]<<24); 123 a+=(uint)(url[k+2]<<16); 124 a+=(uint)(url[k+1]<<8); 125 a+=(uint)(url[k+0]); 126 break; 127 } 128 /* the first byte of c is reserved for the length */ 129 case 8 : 130 { 131 b+=(uint)(url[k+7]<<24); 132 b+=(uint)(url[k+6]<<16); 133 b+=(uint)(url[k+5]<<8); 134 b+=(uint)(url[k+4]); 135 a+=(uint)(url[k+3]<<24); 136 a+=(uint)(url[k+2]<<16); 137 a+=(uint)(url[k+1]<<8); 138 a+=(uint)(url[k+0]); 139 break; 140 } 141 case 7 : 142 { 143 b+=(uint)(url[k+6]<<16); 144 b+=(uint)(url[k+5]<<8); 145 b+=(uint)(url[k+4]); 146 a+=(uint)(url[k+3]<<24); 147 a+=(uint)(url[k+2]<<16); 148 a+=(uint)(url[k+1]<<8); 149 a+=(uint)(url[k+0]); 150 break; 151 } 152 case 6 : 153 { 154 b+=(uint)(url[k+4]); 155 a+=(uint)(url[k+3]<<24); 156 a+=(uint)(url[k+2]<<16); 157 a+=(uint)(url[k+1]<<8); 158 a+=(uint)(url[k+0]); 159 break; 160 } 161 case 5 : 162 { 163 b+=(uint)(url[k+4]); 164 a+=(uint)(url[k+3]<<24); 165 a+=(uint)(url[k+2]<<16); 166 a+=(uint)(url[k+1]<<8); 167 a+=(uint)(url[k+0]); 168 break; 169 } 170 case 4 : 171 { 172 a+=(uint)(url[k+3]<<24); 173 a+=(uint)(url[k+2]<<16); 174 a+=(uint)(url[k+1]<<8); 175 a+=(uint)(url[k+0]); 176 break; 177 } 178 case 3 : 179 { 180 a+=(uint)(url[k+2]<<16); 181 a+=(uint)(url[k+1]<<8); 182 a+=(uint)(url[k+0]); 183 break; 184 } 185 case 2 : 186 { 187 a+=(uint)(url[k+1]<<8); 188 a+=(uint)(url[k+0]); 189 break; 190 } 191 case 1 : 192 { 193 a+=(uint)(url[k+0]); 194 break; 195 } 196 /* case 0: nothing left to add */ 197 } 198 m_mix = mix(a,b,c); 199 /*-------------------------------------------- report the result */ 200 return m_mix[2]; 201 } 202 203 private uint GoogleCH(string url, uint length) 204 { 205 uint[] m_urluint=new uint[url.Length]; 206 for(int i=0;i<url.Length;i++) 207 { 208 m_urluint[i]=url[i]; 209 } 210 return GoogleCH(m_urluint,length,GOOGLE_MAGIC); 211 } 212 213 private uint GoogleCH(string sURL) 214 { 215 return GoogleCH(sURL,0); 216 } 217 218 private uint GoogleCH(uint[] url, uint length) 219 { 220 return GoogleCH(url, length, GOOGLE_MAGIC); 221 } 222 223 private uint[] c32to8bit(uint[] arr32) 224 { 225 uint[] arr8=new uint[arr32.GetLength(0)*4+3]; 226 227 for(int i=0;i<arr32.GetLength(0);i++) 228 { 229 for (int bitOrder=i*4;bitOrder<=i*4+3;bitOrder++) 230 { 231 arr8[bitOrder]=arr32[i]&255; 232 arr32[i]=zeroFill(arr32[i], 8); 233 } 234 } 235 return arr8; 236 } 237 238 //新算法,ToolBar 版本>>=2.0.114 239 public string CalculateChecksum(string sURL) 240 { 241 uint ch=GoogleCH("info:" + sURL); 242 243 ch = (((ch/7) << 2) | (((uint)(ch%13))&7)); 244 245 uint[] prbuf = new uint[20]; 246 prbuf[0] = ch; 247 for(int i = 1; i < 20; i++) { 248 prbuf[i] = prbuf[i-1]-9; 249 } 250 ch = GoogleCH(c32to8bit(prbuf), 80); 251 252 return string.Format("6{0}",ch); 253 } 254 255 //老算法,ToolBar 版本<2.0.114 256 public string CalculateChecksumOld(string sURL) 257 { 258 uint ch=GoogleCH("info:" + sURL); 259 260 string CalculateChecksum = "6" + Convert.ToString((ch)); 261 return CalculateChecksum; 262 } 263 } 264}