refactor(time): 重构 DateTime 类并添加新功能

- 从 Java 文件中删除了旧的 DateTime 类
- 新增 Kotlin 版本的 DateTime 类,具有以下改进:
  - 添加了时间格式化枚举类 Formatter
  - 新增时间位移类 DateTimeOffset - 重构了 parse 和 format 方法,支持新的 Formatter 枚举  - 优化了文件时间转换方法,使用纳秒精度
- 删除了旧的 DateTimeJsonConverter 类
This commit is contained in:
2025-09-15 11:20:08 +08:00
parent dc129c016f
commit 7526b2e787
50 changed files with 1732 additions and 2705 deletions
@@ -0,0 +1,107 @@
/*
* Copyright 2025 mingliqiye
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ProjectName mingli-utils
* ModuleName mingli-utils.main
* CurrentFile HashUtils.kt
* LastUpdate 2025-09-15 09:38:04
* UpdateUser MingLiPro
*/
@file:JvmName("HashUtils")
package com.mingliqiye.utils.hash
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.mindrot.jbcrypt.BCrypt
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.security.Security
private val _addProvider = run {
Security.addProvider(BouncyCastleProvider())
}
/**
* 计算指定文件的哈希值。
*
* @param file 要计算哈希值的文件对象
* @param algorithm 使用的哈希算法名称(如 SHA-256、MD5 等)
* @return 文件的十六进制格式哈希值字符串
* @throws IOException 当文件不存在或读取过程中发生 I/O 错误时抛出
* @throws NoSuchAlgorithmException 当指定的哈希算法不可用时抛出
*/
@Throws(IOException::class, NoSuchAlgorithmException::class)
fun calculateFileHash(file: File, algorithm: String): String {
// 检查文件是否存在
if (!file.exists()) {
throw IOException("File not found: " + file.absolutePath)
}
val digest = MessageDigest.getInstance(algorithm)
FileInputStream(file).use { fis ->
val buffer = ByteArray(8192)
var bytesRead: Int
// 分块读取文件内容并更新摘要
while (fis.read(buffer).also { bytesRead = it } != -1) {
digest.update(buffer, 0, bytesRead)
}
}
return bytesToHex(digest.digest())
}
/**
* 将字节数组转换为十六进制字符串表示。
*
* @param bytes 输入的字节数组
* @return 对应的十六进制字符串
*/
private fun bytesToHex(bytes: ByteArray): String {
val hexString = StringBuilder(2 * bytes.size)
for (b in bytes) {
val hex = Integer.toHexString(0xff and b.toInt())
if (hex.length == 1) {
hexString.append('0')
}
hexString.append(hex)
}
return hexString.toString()
}
/**
* 使用 BCrypt 算法对字符串进行加密。
*
* @param string 需要加密的明文字符串
* @return 加密后的 BCrypt 哈希字符串
*/
fun bcrypt(string: String): String {
return BCrypt.hashpw(string, BCrypt.gensalt())
}
/**
* 验证给定字符串与 BCrypt 哈希是否匹配。
*
* @param string 明文字符串
* @param bcrypted 已经使用 BCrypt 加密的哈希字符串
* @return 如果匹配返回 true,否则返回 false
*/
fun checkBcrypt(string: String, bcrypted: String): Boolean {
return BCrypt.checkpw(string, bcrypted)
}