Last active
December 27, 2015 13:18
-
-
Save linux-china/7331933 to your computer and use it in GitHub Desktop.
shorturl with Java. 7位key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String[] shortUrl(String url) { | |
//私钥 | |
String privateKey = "private_key"; | |
//要使用生成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" | |
}; | |
String hex = DigestUtils.sha1Hex(privateKey + url); | |
String[] ShortStr = new String[4]; | |
for (int i = 0; i < 4; i++) { | |
String outChars = ""; | |
int j = i + 1; | |
String subHex = hex.substring(i * 10, j * 10); | |
long idx = Long.valueOf("3FFFFFFFFF", 16) & Long.valueOf(subHex, 16); | |
for (int k = 0; k < 7; k++) { | |
int index = (int) (Long.valueOf("0000003D", 16) & idx); | |
outChars += chars[index]; | |
idx = idx >> 5; | |
} | |
ShortStr[i] = outChars; | |
} | |
return ShortStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment