增加摘要函数、UID生成函数

This commit is contained in:
wcx 2024-07-07 23:43:18 +08:00
parent d5946b8a33
commit 027774c545
4 changed files with 44 additions and 3 deletions

View File

@ -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) }
}

View File

@ -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()
}

View File

@ -1,7 +1,7 @@
server:
port: 18888
port: 18888 #服务端口
spring:
application:
name: surl
name: surl # 应用名称
main:
banner-mode: off
banner-mode: off # 关闭banner

View File

@ -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))
}
}