java,url長鏈接生成短鏈接,短鏈接生成器,自定義字符串,對字符串md5混合KEY加密,根據短鏈接獲得key值,不重復的隨機數,不重復的隨機字符串
1 package com.zdz.test; 2 3 import java.math.BigInteger; 4 import java.security.MessageDigest; 5 6 import javax.security.sasl.SaslException; 7 8 /** 9 * 短鏈接生成器 10 * 11 * @author zdz8207 12 * @version v1.0,2017-01-11 13 * @since v1.0 14 */ 15 public class ShortUrlGenerator { 16 17 /** 18 * 生成短鏈接 19 * @param url 長鏈接 20 * @return 四個短地址數組,取任意一個即可 21 */ 22 public static String[] getShortUrl(String url) { 23 24 // 可以自定義生成 MD5 加密字符傳前的混合 KEY 25 String key = "zdz8207"; 26 27 // 要使用生成 URL 的字符 28 String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", 29 "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", 30 "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", 31 "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", 32 "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", 33 "U", "V", "W", "X", "Y", "Z" 34 }; 35 36 // 對傳入網址進行 MD5 加密 37 String sMD5EncryptResult = getMD5(key + url); 38 39 String hex = sMD5EncryptResult; 40 String[] resUrl = new String[4]; 41 42 //生成4個短地址,8位一組 43 for (int i = 0; i < 4; i++) { 44 45 // 把加密字符按照 8 位一組 16 進制與 0x3FFFFFFF 進行位與運算 46 String sTempSubString = hex.substring(i * 8, i * 8 + 8); 47 48 // 這里需要使用 long 型來轉換,因為 Inteper.parseInt() 只能處理 31 位 , 首位為符號位 , 如果不用 49 // long ,則會越界 50 long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16); 51 52 String outChars = ""; 53 54 //生成6次-6位短地址 55 for (int j = 0; j < 6; j++) { 56 57 // 把得到的值與 0x0000003D 進行位與運算,取得字符數組 chars 索引 58 long index = 0x0000003D & lHexLong; 59 60 // 把取得的字符相加 61 outChars += chars[(int) index]; 62 63 // 每次循環按位右移 5 位 64 lHexLong = lHexLong >> 5; 65 66 } 67 68 // 把字符串存入對應索引的輸出數組 69 resUrl[i] = outChars; 70 71 } 72 73 return resUrl; 74 75 } 76 77 /** 78 * 根據短鏈接獲得key值 79 * @param shortUrl 短鏈接 80 * @return key值 String 81 */ 82 public static String getShortKey(String shortUrl) { 83 String key = shortUrl.substring(shortUrl.lastIndexOf("/") + 1,shortUrl.length()); 84 return key; 85 } 86 87 /** 88 * 對字符串md5加密 89 * @param str 90 * @return 91 * @throws SaslException 92 */ 93 public static String getMD5(String str){ 94 try { 95 // 生成一個MD5加密計算摘要 96 MessageDigest md = MessageDigest.getInstance("MD5"); 97 // 計算md5函數 98 md.update(str.getBytes()); 99 // digest()最后確定返回md5 hash值,返回值為8為字符串。因為md5 hash值是16位的hex值,實際上就是8位的字符 100 // BigInteger函數則將8位的字符串轉換成16位hex值,用字符串來表示;得到字符串形式的hash值 101 return new BigInteger(1, md.digest()).toString(16); 102 } catch (Exception e) { 103 System.out.println("MD5加密出現錯誤"); 104 return ""; 105 } 106 } 107 108 //測試函數 109 public static void main(String[] args) { 110 // 長連接:http://www.cnblogs.com/zdz8207/ 111 // 解析后的四個短地址數組,取任意一個即可 112 113 String sLongUrl = "http://www.cnblogs.com/zdz8207/"; 114 String[] aResult = getShortUrl(sLongUrl); 115 116 // 打印出結果,四個短地址數組,取任意一個即可 117 for (int i = 0; i < aResult.length; i++) { 118 System.out.println(aResult[i]); 119 } 120 // FjIjqm 121 // bEjyay 122 // jaUJFf 123 // ZNBrEz 124 125 //根據短鏈接獲得key值 126 String key = getShortKey("http://www.cnblogs.com/FjIjqm"); 127 System.out.println("key="+key);//key=FjIjqm 128 129 } 130 }
------------------------------
本人微信公眾帳號: 心禪道(xinchandao)
本人微信公眾帳號:雙色球預測合買(ssqyuce)