refactor: remove DigestUtils

pull/1914/head
Xwite 3 years ago
parent 856948cc79
commit 8993ba4c9a
  1. 10
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  2. 62
      app/src/main/java/io/legado/app/utils/DigestUtils.kt
  3. 3
      app/src/main/java/io/legado/app/utils/MD5Utils.kt

@ -3,6 +3,8 @@ package io.legado.app.help
import android.net.Uri
import android.util.Base64
import androidx.annotation.Keep
import cn.hutool.crypto.digest.DigestUtil
import cn.hutool.crypto.digest.HMac
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.constant.AppLog
@ -960,7 +962,7 @@ interface JsExtensions {
data: String,
algorithm: String,
): String {
return DigestUtils.getDigest(algorithm, data)
return DigestUtil.digester(algorithm).digestHex(data)
}
/**
@ -974,7 +976,7 @@ interface JsExtensions {
data: String,
algorithm: String,
): String {
return Base64.encodeToString(DigestUtils.getDigest(algorithm, data.toByteArray()), Base64.NO_WRAP)
return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP)
}
/**
@ -990,7 +992,7 @@ interface JsExtensions {
algorithm: String,
key: String
): String {
return DigestUtils.getHMac(algorithm, key, data)
return HMac(algorithm, key.toByteArray()).digestHex(data)
}
/**
@ -1006,7 +1008,7 @@ interface JsExtensions {
algorithm: String,
key: String
): String {
return Base64.encodeToString(DigestUtils.getHMac(algorithm, key.toByteArray(), data.toByteArray()), Base64.NO_WRAP)
return Base64.encodeToString(HMac(algorithm, key.toByteArray()).digest(data), Base64.NO_WRAP)
}
fun md5Encode(str: String): String {

@ -1,62 +0,0 @@
package io.legado.app.utils
import java.security.MessageDigest
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
object DigestUtils {
/**
* 消息摘要
* 支持MD5 SHA-1 SHA-224 SHA-256 SHA-384 SHA-512
* https://developer.android.google.cn/reference/java/security/MessageDigest?hl=en
*/
fun getDigest(
algorithm: String,
data: String?
): String {
data ?: return ""
val bytes = getDigest(algorithm, data.toByteArray())
return StringUtils.byteToHexString(bytes)
}
fun getDigest(
algorithm: String,
data: ByteArray
): ByteArray {
return kotlin.runCatching {
val messageDigest = MessageDigest.getInstance(algorithm)
messageDigest.digest(data)
}.getOrThrow()
}
/**
* 散列消息鉴别码
* 支持DESMAC DESMAC/CFB8 DESedeMAC DESedeMAC/CFB8 DESedeMAC64 DESwithISO9797 HmacMD5 HmacSHA* ISO9797ALG3MAC PBEwithSHA*
* https://developer.android.google.cn/reference/kotlin/javax/crypto/Mac?hl=en
*/
fun getHMac(
algorithm: String,
key: String,
data: String?
): String {
data ?: return ""
val bytes = getHMac(algorithm, key.toByteArray(), data.toByteArray())
return StringUtils.byteToHexString(bytes)
}
fun getHMac(
algorithm: String,
key: ByteArray,
data: ByteArray
): ByteArray {
return kotlin.runCatching {
val mac= Mac.getInstance(algorithm)
val keySpec = SecretKeySpec(key, algorithm)
mac.init(keySpec)
mac.update(data)
mac.doFinal()
}.getOrThrow()
}
}

@ -1,5 +1,6 @@
package io.legado.app.utils
import cn.hutool.crypto.digest.DigestUtil
/**
* 将字符串转化为MD5
*/
@ -7,7 +8,7 @@ package io.legado.app.utils
object MD5Utils {
fun md5Encode(str: String?): String {
return DigestUtils.getDigest("MD5", str)
return DigestUtil.digester("MD5").digestHex(str)
}
fun md5Encode16(str: String): String {

Loading…
Cancel
Save