初始化
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.ifish.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Random;
|
||||
|
||||
public class IfishUtil {
|
||||
|
||||
private static final String formatDate1 ="yyyy-MM-dd";
|
||||
private static final String formatDate2 ="yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
/**
|
||||
* 格式化日期yyyy-MM-dd HH:mm:ss
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String format2(Date date){
|
||||
SimpleDateFormat format = new SimpleDateFormat(formatDate2);
|
||||
return format.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期yyyy-MM-dd
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String format1(Date date){
|
||||
SimpleDateFormat format = new SimpleDateFormat(formatDate1);
|
||||
return format.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成字母与数字组合
|
||||
* @param length
|
||||
* @return
|
||||
*/
|
||||
public static String getCharAndNumr(int length) {
|
||||
String val = "";
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < length; i++) {
|
||||
// 输出字母还是数字
|
||||
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
|
||||
// 字符串
|
||||
if ("char".equalsIgnoreCase(charOrNum)) {
|
||||
// 取得大写字母还是小写字母
|
||||
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
|
||||
val += (char) (choice + random.nextInt(26));
|
||||
// 数字
|
||||
} else if ("num".equalsIgnoreCase(charOrNum)) {
|
||||
val += String.valueOf(random.nextInt(10));
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.ifish.netease;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
public class CheckSumBuilder {
|
||||
|
||||
// 计算并获取CheckSum
|
||||
public static String getCheckSum(String appSecret, String nonce, String curTime) {
|
||||
return encode("sha1", appSecret + nonce + curTime);
|
||||
}
|
||||
|
||||
// 计算并获取md5值
|
||||
public static String getMD5(String requestBody) {
|
||||
return encode("md5", requestBody);
|
||||
}
|
||||
|
||||
private static String encode(String algorithm, String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
MessageDigest messageDigest
|
||||
= MessageDigest.getInstance(algorithm);
|
||||
messageDigest.update(value.getBytes());
|
||||
return getFormattedText(messageDigest.digest());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getFormattedText(byte[] bytes) {
|
||||
int len = bytes.length;
|
||||
StringBuilder buf = new StringBuilder(len * 2);
|
||||
for (int j = 0; j < len; j++) {
|
||||
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
|
||||
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user