mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
refactor: implements DigestUtils
This commit is contained in:
parent
c42cf0a59e
commit
9be7857763
@ -3,7 +3,6 @@ package io.legado.app.help
|
||||
import android.net.Uri
|
||||
import android.util.Base64
|
||||
import androidx.annotation.Keep
|
||||
import cn.hutool.crypto.digest.DigestUtil
|
||||
import io.legado.app.constant.AppConst
|
||||
import io.legado.app.constant.AppConst.dateFormat
|
||||
import io.legado.app.constant.AppLog
|
||||
@ -960,8 +959,8 @@ interface JsExtensions {
|
||||
fun digestHex(
|
||||
data: String,
|
||||
algorithm: String,
|
||||
): String? {
|
||||
return DigestUtil.digester(algorithm).digestHex(data)
|
||||
): String {
|
||||
return DigestUtils.getDigest(algorithm, data)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -974,8 +973,8 @@ interface JsExtensions {
|
||||
fun digestBase64Str(
|
||||
data: String,
|
||||
algorithm: String,
|
||||
): String? {
|
||||
return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP)
|
||||
): String {
|
||||
return Base64.encodeToString(DigestUtils.getDigest(algorithm, data.toByteArray()), Base64.NO_WRAP)
|
||||
}
|
||||
|
||||
fun md5Encode(str: String): String {
|
||||
|
69
app/src/main/java/io/legado/app/utils/DigestUtils.kt
Normal file
69
app/src/main/java/io/legado/app/utils/DigestUtils.kt
Normal file
@ -0,0 +1,69 @@
|
||||
package io.legado.app.utils
|
||||
|
||||
import java.security.MessageDigest
|
||||
import java.security.NoSuchAlgorithmException
|
||||
import javax.crypto.Mac
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
|
||||
object DigestUtils {
|
||||
|
||||
/**
|
||||
* 消息摘要
|
||||
* MD2 MD5 SHA-1 SHA-256 SHA-384 SHA-512
|
||||
*/
|
||||
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 {
|
||||
lateinit var bytes: ByteArray
|
||||
try {
|
||||
val messageDigest = MessageDigest.getInstance(algorithm)
|
||||
bytes = messageDigest.digest(data)
|
||||
} catch (e: NoSuchAlgorithmException) {
|
||||
e.printOnDebug()
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
/**
|
||||
* 散列消息鉴别码
|
||||
* HmacMD5 HmacSHA1 HmacSHA224 HmacSHA256 HmacSHA384 HmacSHA512
|
||||
*/
|
||||
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 {
|
||||
lateinit var bytes: ByteArray
|
||||
try {
|
||||
val mac= Mac.getInstance(algorithm)
|
||||
val keySpec = SecretKeySpec(key, algorithm)
|
||||
mac.init(keySpec)
|
||||
mac.update(data)
|
||||
bytes = mac.doFinal()
|
||||
} catch(e: NoSuchAlgorithmException) {
|
||||
e.printOnDebug()
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
}
|
@ -1,9 +1,5 @@
|
||||
package io.legado.app.utils
|
||||
|
||||
|
||||
import java.security.MessageDigest
|
||||
import java.security.NoSuchAlgorithmException
|
||||
|
||||
/**
|
||||
* 将字符串转化为MD5
|
||||
*/
|
||||
@ -11,25 +7,7 @@ import java.security.NoSuchAlgorithmException
|
||||
object MD5Utils {
|
||||
|
||||
fun md5Encode(str: String?): String {
|
||||
if (str == null) return ""
|
||||
var reStr = ""
|
||||
try {
|
||||
val md5: MessageDigest = MessageDigest.getInstance("MD5")
|
||||
val bytes: ByteArray = md5.digest(str.toByteArray())
|
||||
val stringBuffer: StringBuilder = StringBuilder()
|
||||
for (b in bytes) {
|
||||
val bt: Int = b.toInt() and 0xff
|
||||
if (bt < 16) {
|
||||
stringBuffer.append(0)
|
||||
}
|
||||
stringBuffer.append(Integer.toHexString(bt))
|
||||
}
|
||||
reStr = stringBuffer.toString()
|
||||
} catch (e: NoSuchAlgorithmException) {
|
||||
e.printOnDebug()
|
||||
}
|
||||
|
||||
return reStr
|
||||
return DigestUtils.getDigest("MD5", str)
|
||||
}
|
||||
|
||||
fun md5Encode16(str: String): String {
|
||||
|
Loading…
Reference in New Issue
Block a user