整改了 utils 包当中的 common 工具类格式 (WARN:breking-changes)

This commit is contained in:
A.C.Sukazyo Eyre 2022-10-01 17:24:59 +08:00
parent ab37144d45
commit 063ca24bf5
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
13 changed files with 98 additions and 81 deletions

View File

@ -1,6 +1,6 @@
## Core
VERSION = 0.7.2.1
VERSION = 0.8.0.0
CODENAME = fuzhou

View File

@ -4,7 +4,7 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically.
*/
public class GradleProjectConfigures {
public static final String VERSION = "0.7.2.1";
public static final String VERSION = "0.8.0.0";
public static final String CODENAME = "fuzhou";
public static final long COMPILE_TIMESTAMP = 1663602816303L;
public static final long COMPILE_TIMESTAMP = 1664615965858L;
}

View File

@ -59,7 +59,7 @@ public class MornyCoeur {
* {@link cc.sukazyo.cono.morny.bot.event.OnUpdateTimestampOffsetLock}
* 会根据这里定义的时间戳取消掉比此时间更早的事件链
*/
public long latestEventTimestamp;
public final long latestEventTimestamp;
/**
* morny 主程序启动时间<br>
* 用于统计数据

View File

@ -1,6 +1,6 @@
package cc.sukazyo.cono.morny;
import cc.sukazyo.cono.morny.util.CommonFormatUtils;
import cc.sukazyo.cono.morny.util.CommonFormat;
import javax.annotation.Nonnull;
@ -205,7 +205,7 @@ public class ServerMain {
MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(),
MornySystem.getJarMd5(),
GradleProjectConfigures.COMPILE_TIMESTAMP,
CommonFormatUtils.formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)
CommonFormat.formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)
));
return;

View File

@ -27,8 +27,8 @@ import java.util.List;
import java.util.Map;
import static cc.sukazyo.cono.morny.Log.logger;
import static cc.sukazyo.cono.morny.util.CommonFormatUtils.formatDate;
import static cc.sukazyo.cono.morny.util.CommonFormatUtils.formatDuration;
import static cc.sukazyo.cono.morny.util.CommonFormat.formatDate;
import static cc.sukazyo.cono.morny.util.CommonFormat.formatDuration;
import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml;
public class MornyCommands {

View File

@ -4,7 +4,7 @@ import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornyTrusted;
import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.cono.morny.data.TelegramStickers;
import cc.sukazyo.cono.morny.util.CommonFormatUtils;
import cc.sukazyo.cono.morny.util.CommonFormat;
import cc.sukazyo.untitled.telegram.api.formatting.TGToString;
import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape;
import com.pengrad.telegrambot.model.Chat;
@ -122,9 +122,9 @@ public class OnCallMe extends EventListener {
event.message().from().id(),
String.format("<i>on</i> <code>%s [UTC+8]</code>\n- <code>%s</code> <i>before</i>",
MsgEscape.escapeHtml(
CommonFormatUtils.formatDate((long)lastDinnerData.forwardDate()*1000, 8)
CommonFormat.formatDate((long)lastDinnerData.forwardDate()*1000, 8)
), MsgEscape.escapeHtml(
CommonFormatUtils.formatDuration(System.currentTimeMillis()-(long)lastDinnerData.forwardDate()*1000)
CommonFormat.formatDuration(System.currentTimeMillis()-(long)lastDinnerData.forwardDate()*1000)
)
)
).replyToMessageId(sendResp.message().messageId()).parseMode(ParseMode.HTML));

View File

@ -1,7 +1,6 @@
package cc.sukazyo.cono.morny.bot.query;
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
import cc.sukazyo.cono.morny.util.EncryptUtils;
import javax.annotation.Nullable;
@ -9,6 +8,9 @@ import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineQueryResultArticle;
import com.pengrad.telegrambot.model.request.InputTextMessageContent;
import static cc.sukazyo.cono.morny.util.CommonConvert.byteArrayToHex;
import static cc.sukazyo.cono.morny.util.CommonEncrypt.encryptByMD5;
public class RawText implements ITelegramQuery<InlineQueryResultArticle> {
public static final String ID_PREFIX = "[morny/r/text]";
@ -19,7 +21,7 @@ public class RawText implements ITelegramQuery<InlineQueryResultArticle> {
public InlineQueryUnit<InlineQueryResultArticle> query (Update event) {
if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null;
return new InlineQueryUnit<>(new InlineQueryResultArticle(
ID_PREFIX + EncryptUtils.encryptByMD5(event.inlineQuery().query()),
ID_PREFIX + byteArrayToHex(encryptByMD5(event.inlineQuery().query())),
TITLE,
new InputTextMessageContent(event.inlineQuery().query())
));

View File

@ -1,7 +1,7 @@
package cc.sukazyo.cono.morny.daemon;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.util.CommonFormatUtils;
import cc.sukazyo.cono.morny.util.CommonFormat;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.MessageEntity;
import com.pengrad.telegrambot.model.request.ParseMode;
@ -54,7 +54,7 @@ public class MedicationTimer extends Thread {
public void refreshNotificationWrite (Message edited) {
if (edited.messageId() != lastNotify) return;
final String editTime = CommonFormatUtils.formatDate(edited.editDate()*1000, 8);
final String editTime = CommonFormat.formatDate(edited.editDate()*1000, 8);
ArrayList<MessageEntity> entities = new ArrayList<>();
if (edited.entities() != null) entities.addAll(List.of(edited.entities()));
entities.add(new MessageEntity(MessageEntity.Type.italic, edited.text().length() + "\n-- ".length(), editTime.length()));

View File

@ -1,6 +1,7 @@
package cc.sukazyo.cono.morny.data;
import cc.sukazyo.cono.morny.util.EncryptUtils;
import cc.sukazyo.cono.morny.util.CommonConvert;
import cc.sukazyo.cono.morny.util.CommonEncrypt;
import com.pengrad.telegrambot.model.User;
/**
@ -39,7 +40,7 @@ public class MornyJrrp {
* @return 算法得到的 jrrp 取值为 {@code [0.00. 100.00]}
*/
public static double calcJrrpXmomi (long userId, long dayStamp) {
return (double)Long.parseLong(EncryptUtils.encryptByMD5(userId + "@" + dayStamp).substring(0, 4), 16) / (double)0xffff;
return (double)Long.parseLong(CommonConvert.byteArrayToHex(CommonEncrypt.encryptByMD5(userId + "@" + dayStamp)).substring(0, 4), 16) / (double)0xffff;
}
}

View File

@ -0,0 +1,36 @@
package cc.sukazyo.cono.morny.util;
import javax.annotation.Nonnull;
public class CommonConvert {
private final static String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
/**
* 将字节数组转换成十六进制并以字符串的形式返回
* 128位是指二进制位二进制太长所以一般都改写成16进制
* 每一位16进制数可以代替4位二进制数所以128位二进制数写成16进制就变成了128/4=32位
*/
@Nonnull
public static String byteArrayToHex(@Nonnull byte[] b){
StringBuilder sb = new StringBuilder();
for (byte value : b) {
sb.append(byteToHex(value));
}
return sb.toString();
}
/**
* 将一个字节转换成十六进制并以字符串的形式返回
*/
@Nonnull
public static String byteToHex(byte b) {
int n = b;
if (n < 0)
n = n + 256;
int d1 = n / 16;
int d2 = n % 16;
return hexArray[d1]+hexArray[d2];
}
}

View File

@ -0,0 +1,38 @@
package cc.sukazyo.cono.morny.util;
import javax.annotation.Nonnull;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 用于数据加密或编码的工具类<br>
* <s>显然大部分代码是抄来的</s><br>
* <ul>
* <li><a href="https://blog.csdn.net/yu540135101/article/details/86765457">{@link #encryptByMD5} 来源</a></li>
* </ul>
*/
public class CommonEncrypt {
public static final Charset ENCRYPT_STANDARD_CHARSET = StandardCharsets.UTF_8;
/***
* 对指定的字符串进行MD5加密
*/
@Nonnull
public static byte[] encryptByMD5(@Nonnull byte[] data) {
try {
return MessageDigest.getInstance("md5").digest(data);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException();
}
}
@Nonnull
public static byte[] encryptByMD5(String originString) {
return encryptByMD5(originString.getBytes(ENCRYPT_STANDARD_CHARSET));
}
}

View File

@ -6,10 +6,12 @@ import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
public class CommonFormatUtils {
public class CommonFormat {
public static final String DATE_TIME_PATTERN_FULL_MILLIS = "yyyy-MM-dd HH:mm:ss:SSS";
public static String formatDate (long timestamp, int utcOffset) {
return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS").format(LocalDateTime.ofInstant(
return DateTimeFormatter.ofPattern(DATE_TIME_PATTERN_FULL_MILLIS).format(LocalDateTime.ofInstant(
Instant.ofEpochMilli(timestamp),
ZoneId.ofOffset("UTC", ZoneOffset.ofHours(utcOffset))
));

View File

@ -1,62 +0,0 @@
package cc.sukazyo.cono.morny.util;
import javax.annotation.Nonnull;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 用于数据加密或编码的工具类<br>
* <s>显然大部分代码是抄来的</s><br>
* <ul>
* <li><a href="https://blog.csdn.net/yu540135101/article/details/86765457">{@link #encryptByMD5} & {@link #byteToHex}
* & {@link #byteArrayToHex} 来源</a></li>
* </ul>
*/
public class EncryptUtils {
private final static String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
/***
* 对指定的字符串进行MD5加密
*/
@Nonnull
public static String encryptByMD5(String originString) {
try {
//创建具有MD5算法的信息摘要
MessageDigest md = MessageDigest.getInstance("MD5");
//使用指定的字节数组对摘要进行最后更新然后完成摘要计算
byte[] bytes = md.digest(originString.getBytes());
//将得到的字节数组变成字符串返回
String s = byteArrayToHex(bytes);
return s.toUpperCase();
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException();
}
}
/**
* 将字节数组转换成十六进制并以字符串的形式返回
* 128位是指二进制位二进制太长所以一般都改写成16进制
* 每一位16进制数可以代替4位二进制数所以128位二进制数写成16进制就变成了128/4=32位
*/
private static String byteArrayToHex(byte[] b){
StringBuilder sb = new StringBuilder();
for (byte value : b) {
sb.append(byteToHex(value));
}
return sb.toString();
}
/**
* 将一个字节转换成十六进制并以字符串的形式返回
*/
public static String byteToHex(byte b) {
int n = b;
if (n < 0)
n = n + 256;
int d1 = n / 16;
int d2 = n % 16;
return hexArray[d1]+hexArray[d2];
}
}