JAVA的。
package ly.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; public class Security { public static String GetG_TK(String str){ int hash = 5381; for(int i = 0, len = str.length(); i < len; ++i){ hash += (hash << 5) + (int)(char)str.charAt(i); } return (hash & 0x7fffffff)+""; } }
js的。
//QQ空间,发送请求时,需要带的g_tk function getGTK(str){ var hash = 5381; for(var i = 0, len = str.length; i < len; ++i) { hash += (hash << 5) + str.charAt(i).charCodeAt(); } return hash & 0x7fffffff; } alert(getGTK("@3VYHMMEZI"))
php的。
//G_tk计算 function getGTK($skey){ $hash = 5381; for($i=0;$i<strlen($skey);++$i){ $hash += ($hash << 5) + utf8_unicode($skey[$i]); } return $hash & 0x7fffffff; } function utf8_unicode($c) { switch(strlen($c)) { case 1: return ord($c); case 2: $n = (ord($c[0]) & 0x3f) << 6; $n += ord($c[1]) & 0x3f; return $n; case 3: $n = (ord($c[0]) & 0x1f) << 12; $n += (ord($c[1]) & 0x3f) << 6; $n += ord($c[2]) & 0x3f; return $n; case 4: $n = (ord($c[0]) & 0x0f) << 18; $n += (ord($c[1]) & 0x3f) << 12; $n += (ord($c[2]) & 0x3f) << 6; $n += ord($c[3]) & 0x3f; return $n; } }
发表评论