From 063ca24bf51bb13c2bcbf31492e1d28ced3c2ee4 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 1 Oct 2022 17:24:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E6=94=B9=E4=BA=86=20utils=20=E5=8C=85?= =?UTF-8?q?=E5=BD=93=E4=B8=AD=E7=9A=84=20common=20=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E6=A0=BC=E5=BC=8F=20(WARN:breking-changes)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 2 +- .../cc/sukazyo/cono/morny/ServerMain.java | 4 +- .../cono/morny/bot/command/MornyCommands.java | 4 +- .../cono/morny/bot/event/OnCallMe.java | 6 +- .../sukazyo/cono/morny/bot/query/RawText.java | 6 +- .../cono/morny/daemon/MedicationTimer.java | 4 +- .../cc/sukazyo/cono/morny/data/MornyJrrp.java | 5 +- .../cono/morny/util/CommonConvert.java | 36 +++++++++++ .../cono/morny/util/CommonEncrypt.java | 38 ++++++++++++ ...mmonFormatUtils.java => CommonFormat.java} | 6 +- .../sukazyo/cono/morny/util/EncryptUtils.java | 62 ------------------- 13 files changed, 98 insertions(+), 81 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java rename src/main/java/cc/sukazyo/cono/morny/util/{CommonFormatUtils.java => CommonFormat.java} (79%) delete mode 100644 src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java diff --git a/gradle.properties b/gradle.properties index eb58aeb..3fa9e53 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.2.1 +VERSION = 0.8.0.0 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 9dcb287..1d1528b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -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; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index d2cb07e..4e716b8 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -59,7 +59,7 @@ public class MornyCoeur { * {@link cc.sukazyo.cono.morny.bot.event.OnUpdateTimestampOffsetLock} * 会根据这里定义的时间戳取消掉比此时间更早的事件链 */ - public long latestEventTimestamp; + public final long latestEventTimestamp; /** * morny 主程序启动时间
* 用于统计数据 diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index 9f58547..17a831f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -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; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index 2918a80..968cd2d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -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 { diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index 1684d69..777fe40 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -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("on %s [UTC+8]\n- %s before", 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)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java index 91333ed..f9f9d4f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java @@ -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 { public static final String ID_PREFIX = "[morny/r/text]"; @@ -19,7 +21,7 @@ public class RawText implements ITelegramQuery { public InlineQueryUnit 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()) )); diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java index 5932bb8..813640b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java @@ -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 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())); diff --git a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java index 2fd858e..f3b2db1 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java @@ -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; } } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java new file mode 100644 index 0000000..eddf5fa --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java @@ -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]; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java new file mode 100644 index 0000000..7bcf21d --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java @@ -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; + +/** + * 用于数据加密或编码的工具类
+ * 显然大部分代码是抄来的
+ * + */ +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)); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonFormatUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonFormat.java similarity index 79% rename from src/main/java/cc/sukazyo/cono/morny/util/CommonFormatUtils.java rename to src/main/java/cc/sukazyo/cono/morny/util/CommonFormat.java index c629548..0598ba6 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/CommonFormatUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonFormat.java @@ -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)) )); diff --git a/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java deleted file mode 100644 index 243389a..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -package cc.sukazyo.cono.morny.util; - -import javax.annotation.Nonnull; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * 用于数据加密或编码的工具类
- * 显然大部分代码是抄来的
- * - */ -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]; - } - -}