From 027774c5450321e3a7eb2807752442823a6e757d Mon Sep 17 00:00:00 2001 From: wcx <2738076308@qq.com> Date: Sun, 7 Jul 2024 23:43:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=91=98=E8=A6=81=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E3=80=81UID=E7=94=9F=E6=88=90=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/dev/surl/surl/util/CheckSumUtil.kt | 8 ++++++++ src/main/java/dev/surl/surl/util/UIDUtil.kt | 15 +++++++++++++++ src/main/resources/application.yml | 6 +++--- src/test/java/dev/surl/surl/UtilTests.kt | 18 ++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/main/java/dev/surl/surl/util/CheckSumUtil.kt create mode 100644 src/main/java/dev/surl/surl/util/UIDUtil.kt create mode 100644 src/test/java/dev/surl/surl/UtilTests.kt diff --git a/src/main/java/dev/surl/surl/util/CheckSumUtil.kt b/src/main/java/dev/surl/surl/util/CheckSumUtil.kt new file mode 100644 index 0000000..96ff05e --- /dev/null +++ b/src/main/java/dev/surl/surl/util/CheckSumUtil.kt @@ -0,0 +1,8 @@ +package dev.surl.surl.util + +import org.springframework.util.DigestUtils + +fun md5(str: String): String { + val cksum = DigestUtils.md5Digest(str.toByteArray()) + return cksum.joinToString("") { "%02x".format(it) } +} \ No newline at end of file diff --git a/src/main/java/dev/surl/surl/util/UIDUtil.kt b/src/main/java/dev/surl/surl/util/UIDUtil.kt new file mode 100644 index 0000000..b5b8e4c --- /dev/null +++ b/src/main/java/dev/surl/surl/util/UIDUtil.kt @@ -0,0 +1,15 @@ +package dev.surl.surl.util + +import java.util.concurrent.ThreadLocalRandom + +private val CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray() +private const val LENGTH = 16 + +fun genSurlUID(): String { + return genSurlUID(LENGTH) +} + +fun genSurlUID(length: Int): String { + val random = ThreadLocalRandom.current() + return CharArray(length) { CHARS[random.nextInt(CHARS.size)] }.concatToString() +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 54f3721..639b0c8 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,7 +1,7 @@ server: - port: 18888 + port: 18888 #服务端口 spring: application: - name: surl + name: surl # 应用名称 main: - banner-mode: off + banner-mode: off # 关闭banner diff --git a/src/test/java/dev/surl/surl/UtilTests.kt b/src/test/java/dev/surl/surl/UtilTests.kt new file mode 100644 index 0000000..a40a09a --- /dev/null +++ b/src/test/java/dev/surl/surl/UtilTests.kt @@ -0,0 +1,18 @@ +package dev.surl.surl + +import dev.surl.surl.util.genSurlUID +import dev.surl.surl.util.md5 +import org.junit.jupiter.api.Test + +class UtilTests { + @Test + fun md5Test() { + assert(md5("sd") == "6226f7cbe59e99a90b5cef6f94f966fd") + } + + @Test + fun randomUIDTest() { + println(genSurlUID()) + println(genSurlUID(8)) + } +} \ No newline at end of file