// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.changhong.epc.rely.api.tool; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Date; import java.util.Random; import java.util.UUID; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; /** * @author liush * */ public class StringUtils extends org.apache.commons.lang3.StringUtils { public StringUtils() { } public static String generateUuidString() { UUID uuid = UUID.randomUUID(); String str = uuid.toString(); str = str.replaceAll("-", ""); return str; } public static String getRandomStr(int size) { if(size <= 0) { return ""; } else { Random ran = new Random(); StringBuffer numStr = new StringBuffer(); for(int i = 0; i < size; ++i) { int p = ran.nextInt(10); numStr.append(p); } return numStr.toString(); } } public static boolean isGB2312(char c) { Character ch = new Character(c); String sCh = ch.toString(); try { byte[] ex = sCh.getBytes("gb2312"); return ex.length > 1; } catch (UnsupportedEncodingException var4) { return false; } } private static final String[] ENCODES = {"UTF-8", "ISO-8859-1", "GB2312", "GBK"}; public static String getEncoding(String str) { for(String encode : ENCODES){ try { if(str.equals(new String(str.getBytes(encode), encode))) { return encode; } } catch (Exception var6) { ; } } return ""; } public static String getRandomString(int length) { String base = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for(int i = 0; i < length; ++i) { int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } public static String encodeBase64URLSafeString(String data) throws UnsupportedEncodingException { String base64str = Base64.encodeBase64String(data.getBytes()); if(base64str.contains("+")) { base64str = base64str.replaceAll("\\+", "-"); } if(base64str.contains("/")) { base64str = base64str.replaceAll("/", "_"); } return base64str; } public static String encodeBase64URLSafeString(byte[] bytes) throws UnsupportedEncodingException { String base64str = Base64.encodeBase64String(bytes); if(base64str.contains("+")) { base64str = base64str.replaceAll("\\+", "-"); } if(base64str.contains("/")) { base64str = base64str.replaceAll("/", "_"); } return base64str; } public static String decodeBase64URLSafeString(String data) throws UnsupportedEncodingException { if(data.contains("-")) { data = data.replaceAll("-", "+"); } if(data.contains("_")) { data = data.replaceAll("_", "/"); } return new String(Base64.decodeBase64(data)); } public static String hmac_sha1_base64_url_safe(String data, String key) throws UnsupportedEncodingException { byte[] hmac_sha1 = hmac_sha1(data, key); return encodeBase64URLSafeString(hmac_sha1); } public static byte[] hmac_sha1(String data, String key) { byte[] byteHMAC = null; try { Mac ignore = Mac.getInstance("HmacSHA1"); SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1"); ignore.init(spec); byteHMAC = ignore.doFinal(data.getBytes()); } catch (InvalidKeyException var5) { // System.err.println(var5.getMessage()); } catch (NoSuchAlgorithmException var6) { ; } return byteHMAC; } public static String generatToken(String apiKey, String secretKey, JSONObject policy) throws UnsupportedEncodingException { StringBuffer token = new StringBuffer(encodeBase64URLSafeString(apiKey)); token.append(":"); token.append(hmac_sha1_base64_url_safe(encodeBase64URLSafeString(policy.toJSONString()), secretKey)); token.append(":"); token.append(encodeBase64URLSafeString(policy.toJSONString())); return token.toString(); } public static String join(Object[] o, String flag) { if(o == null) { return null; } else { StringBuffer str_buff = new StringBuffer(); int i = 0; for(int len = o.length; i < len; ++i) { str_buff.append(String.valueOf(o[i])); if(i < len - 1) { str_buff.append(flag); } } return str_buff.toString(); } } public static String join(ArrayList a, String flag) { if(a.size() == 0) { return null; } else { StringBuffer str_buff = new StringBuffer(); int i = 0; for(int len = a.size(); i < len; ++i) { str_buff.append(String.valueOf(a.get(i))); if(i < len - 1) { str_buff.append(flag); } } return str_buff.toString(); } } public static synchronized String getMsTime() { return String.valueOf((new Date()).getTime()); } public static void main(String[] args) throws UnsupportedEncodingException { JSONObject policy = new JSONObject(); JSONArray apis = new JSONArray(); Date date = new Date(); Date ex = new Date(date.getTime() + 8640000000L); // System.out.println(ex.toGMTString()); policy.put("expiredTime", ex); policy.put("scope", "USER"); apis.add("login"); apis.add("authCode"); apis.add("registerByEmail"); apis.add("registerByPhone"); apis.add("checkToken"); apis.add("refreshToken"); apis.add("registBatch"); apis.add("getUserRegisterInfo"); apis.add("removeBind"); apis.add("tobind"); apis.add("isExisted"); apis.add("findbyCompanyId"); apis.add("fuzzyQueryUserDetail"); apis.add("findAllCompany"); apis.add("verifyAuthCode"); apis.add("searchCompanyList"); apis.add("update"); apis.add("create"); apis.add("checkUser"); apis.add("avatar"); apis.add("registerByPhone"); apis.add("resetPwdByPhone"); policy.put("apis", apis); // System.out.println(generatToken("7b8879ad", "9717cc2fff624ef2a8e7a5407b161513", policy)); } }