|
@@ -1,6 +1,10 @@
|
|
package com.platform.common.util;
|
|
package com.platform.common.util;
|
|
|
|
|
|
|
|
+import com.platform.common.enums.ResultCode;
|
|
|
|
+import com.platform.common.exception.BusinessException;
|
|
|
|
+
|
|
import java.security.MessageDigest;
|
|
import java.security.MessageDigest;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @Description Sha加密
|
|
* @Description Sha加密
|
|
@@ -36,8 +40,50 @@ public class ShaUtil {
|
|
}
|
|
}
|
|
return new String(buf);
|
|
return new String(buf);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- // TODO: handle exception
|
|
|
|
- return null;
|
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ throw new BusinessException(ResultCode.WECHAT_TOKEN_CHECK_ERROR.getDescription());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 用SHA1算法验证Token
|
|
|
|
+ *
|
|
|
|
+ * @param token 票据
|
|
|
|
+ * @param timestamp 时间戳
|
|
|
|
+ * @param nonce 随机字符串
|
|
|
|
+ * @return 安全签名
|
|
|
|
+ * @throws BusinessException
|
|
|
|
+ */
|
|
|
|
+ public static String getSHA1(String token, String timestamp, String nonce) throws BusinessException {
|
|
|
|
+ try {
|
|
|
|
+ String[] array = new String[] { token, timestamp, nonce };
|
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
|
+ // 字符串排序
|
|
|
|
+ Arrays.sort(array);
|
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
|
+ sb.append(array[i]);
|
|
|
|
+ }
|
|
|
|
+ String str = sb.toString();
|
|
|
|
+ // SHA1签名生成
|
|
|
|
+ MessageDigest md = MessageDigest.getInstance("SHA-1");
|
|
|
|
+ md.update(str.getBytes());
|
|
|
|
+ byte[] digest = md.digest();
|
|
|
|
+
|
|
|
|
+ StringBuffer hexstr = new StringBuffer();
|
|
|
|
+ String shaHex = "";
|
|
|
|
+ for (int i = 0; i < digest.length; i++) {
|
|
|
|
+ shaHex = Integer.toHexString(digest[i] & 0xFF);
|
|
|
|
+ if (shaHex.length() < 2) {
|
|
|
|
+ hexstr.append(0);
|
|
|
|
+ }
|
|
|
|
+ hexstr.append(shaHex);
|
|
|
|
+ }
|
|
|
|
+ return hexstr.toString();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ throw new BusinessException(ResultCode.WECHAT_TOKEN_CHECK_ERROR.getDescription());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|