refactor: implements DigestUtils
This commit is contained in:
@@ -3,7 +3,6 @@ package io.legado.app.help
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.util.Base64
|
import android.util.Base64
|
||||||
import androidx.annotation.Keep
|
import androidx.annotation.Keep
|
||||||
import cn.hutool.crypto.digest.DigestUtil
|
|
||||||
import io.legado.app.constant.AppConst
|
import io.legado.app.constant.AppConst
|
||||||
import io.legado.app.constant.AppConst.dateFormat
|
import io.legado.app.constant.AppConst.dateFormat
|
||||||
import io.legado.app.constant.AppLog
|
import io.legado.app.constant.AppLog
|
||||||
@@ -960,8 +959,8 @@ interface JsExtensions {
|
|||||||
fun digestHex(
|
fun digestHex(
|
||||||
data: String,
|
data: String,
|
||||||
algorithm: String,
|
algorithm: String,
|
||||||
): String? {
|
): String {
|
||||||
return DigestUtil.digester(algorithm).digestHex(data)
|
return DigestUtils.getDigest(algorithm, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -974,8 +973,8 @@ interface JsExtensions {
|
|||||||
fun digestBase64Str(
|
fun digestBase64Str(
|
||||||
data: String,
|
data: String,
|
||||||
algorithm: String,
|
algorithm: String,
|
||||||
): String? {
|
): String {
|
||||||
return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP)
|
return Base64.encodeToString(DigestUtils.getDigest(algorithm, data.toByteArray()), Base64.NO_WRAP)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun md5Encode(str: String): String {
|
fun md5Encode(str: String): String {
|
||||||
|
|||||||
@@ -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
|
package io.legado.app.utils
|
||||||
|
|
||||||
|
|
||||||
import java.security.MessageDigest
|
|
||||||
import java.security.NoSuchAlgorithmException
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将字符串转化为MD5
|
* 将字符串转化为MD5
|
||||||
*/
|
*/
|
||||||
@@ -11,25 +7,7 @@ import java.security.NoSuchAlgorithmException
|
|||||||
object MD5Utils {
|
object MD5Utils {
|
||||||
|
|
||||||
fun md5Encode(str: String?): String {
|
fun md5Encode(str: String?): String {
|
||||||
if (str == null) return ""
|
return DigestUtils.getDigest("MD5", str)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun md5Encode16(str: String): String {
|
fun md5Encode16(str: String): String {
|
||||||
|
|||||||
Reference in New Issue
Block a user