Files
flagnote-service/src/main/java/com/flagnote/note/utils/BizKeyUtils.java
Jesse-Ma 5cb7ee80b8 inittime
2022-12-09 14:16:15 +08:00

134 lines
2.9 KiB
Java

package com.flagnote.note.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,String initTime) {
return md5(key +"#"+ MIX_STRING + "#" + initTime);
}
public static Boolean validateCipher(String key,String initTime,String cipher) {
return md5(key +"#"+ MIX_STRING + "#" + initTime).equals(cipher);
}
public static String getSecretKey(String key, String password) {
return md5(key + md5(MIX_STRING + password));
}
public static void main(String[] args) {
int c = 0;
while(true) {
c++;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 16; i++) {
sb.append(RANGE_STRING.charAt(RandomUtils.nextInt(30)));
}
Boolean result = validateKey(sb.toString());
if(c%1000==0) {
System.out.println(c);
System.out.println(result);
System.out.println(sb.toString());
}
if(result) {
System.out.println(c);
System.out.println(result);
System.out.println(sb.toString());
break;
}
}
}
}