Merge pull request #41 from Celeter/master

MD5Untils类:java转kotln
pull/42/head
kunfei 5 years ago committed by GitHub
commit 4398b324c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      app/src/main/java/io/legado/app/utils/MD5Utils.java
  2. 41
      app/src/main/java/io/legado/app/utils/MD5Utils.kt

@ -1,40 +0,0 @@
package io.legado.app.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 将字符串转化为MD5
*/
public class MD5Utils {
public static String strToMd5By32(String str) {
if (str == null) return null;
String reStr = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(str.getBytes());
StringBuilder stringBuffer = new StringBuilder();
for (byte b : bytes) {
int bt = b & 0xff;
if (bt < 16) {
stringBuffer.append(0);
}
stringBuffer.append(Integer.toHexString(bt));
}
reStr = stringBuffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return reStr;
}
public static String strToMd5By16(String str) {
String reStr = strToMd5By32(str);
if (reStr != null) {
reStr = reStr.substring(8, 24);
}
return reStr;
}
}

@ -0,0 +1,41 @@
package io.legado.app.utils
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
/**
* 将字符串转化为MD5
*/
object MD5Utils {
fun strToMd5By32(str: String?): String? {
if (str == null) return null
var reStr: String? = null
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.printStackTrace()
}
return reStr
}
fun strToMd5By16(str: String): String? {
var reStr = strToMd5By32(str)
if (reStr != null) {
reStr = reStr.substring(8, 24)
}
return reStr
}
}
Loading…
Cancel
Save