This commit is contained in:
Jesse-Ma
2022-07-21 11:45:02 +08:00
parent bacaff41ce
commit 83f70c2f88
15 changed files with 678 additions and 19 deletions

View File

@@ -0,0 +1,100 @@
package com.flagnote.gateway.utils;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import org.springframework.util.DigestUtils;
public class BizKeyUtils {
public static final String MIX_STRING = "6v8muhqp8ta45ncsyi8y";
public static final String RANGE_STRING = "abcdefhikmnopqstuvwxyz23456789";
public static String getKey() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 13; i++) {
sb.append(RANGE_STRING.charAt(RandomUtils.nextInt(30)));
}
sb.append(getFilteredKey(md5(sb.toString() + MIX_STRING + getPrefixTime())));
return sb.toString();
}
public static String getFilteredKey(String key) {
String result = "";
for (int i = 0; i < key.length(); i++) {
String ts = String.valueOf(key.charAt(i));
if (RANGE_STRING.indexOf(ts) >= 0) {
result += ts;
}
if (result.length() == 3) {
return result;
}
}
for (int i = 1; i < 3; i++) {
result += "x";
}
return result;
}
public static Boolean validateKey(String key) {
String oKey = key.substring(0, 13);
String cKey = oKey + getFilteredKey(md5(oKey + MIX_STRING + getPrefixTime()));
if (cKey.equals(key)) {
return true;
}
cKey = oKey + getFilteredKey(md5(oKey + MIX_STRING + (getPrefixTime() - 1)));
if (cKey.equals(key)) {
return true;
}
cKey = oKey + getFilteredKey(md5(oKey + MIX_STRING + (getPrefixTime() + 1)));
if (cKey.equals(key)) {
return true;
}
return false;
}
public static String mixKey(String key) {
return md5(key + md5(MIX_STRING + key));
}
public static String md5(String text) {
try {
return DigestUtils.md5DigestAsHex(text.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("无法生成MD5");
}
}
public static Integer getPrefixTime() {
return Integer.parseInt(String.valueOf(new Date().getTime()).substring(0, 4));
}
public static String getCipher(String key) {
return md5(key + MIX_STRING + key);
}
public static Boolean validateCipher(String key,String cipher) {
return md5(key + MIX_STRING + key).equals(cipher);
}
public static String getSecretKey(String key, String password) {
return md5(key + md5(MIX_STRING + password));
}
}