Skip to content

Commit 874fc65

Browse files
TaoTao
authored andcommitted
1.增加MD5加密
2.增加dp和px之间的互转 3.增加toast的扩展
1 parent 6e6cc7d commit 874fc65

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.catchpig.utils.ext
2+
3+
import android.content.Context
4+
import android.view.View
5+
6+
/**
7+
*
8+
* @author TLi2
9+
**/
10+
fun Context.dp2px(dp: Int): Int {
11+
val scale = resources.displayMetrics.density
12+
return (dp * scale + 0.5f).toInt()
13+
}
14+
15+
fun Context.px2dp(px: Int): Int {
16+
val scale = resources.displayMetrics.density
17+
return (px / scale + 0.5f).toInt()
18+
}
19+
20+
fun View.dp2px(dp: Int): Int {
21+
val scale = resources.displayMetrics.density
22+
return (dp * scale + 0.5f).toInt()
23+
}
24+
25+
fun View.px2dp(px: Int): Int {
26+
val scale = resources.displayMetrics.density
27+
return (px / scale + 0.5f).toInt()
28+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.catchpig.utils.ext
2+
3+
import java.security.MessageDigest
4+
5+
/**
6+
*
7+
* @author TLi2
8+
**/
9+
10+
/**
11+
* md5加密
12+
*/
13+
fun String.md5():String{
14+
val instance: MessageDigest = MessageDigest.getInstance("MD5")
15+
//对字符串加密,返回字节数组
16+
val digest:ByteArray = instance.digest(this.toByteArray())
17+
var sb = StringBuffer()
18+
for (b in digest) {
19+
//获取低八位有效值
20+
var i :Int = b.toInt() and 0xff
21+
//将整数转化为16进制
22+
var hexString = Integer.toHexString(i)
23+
if (hexString.length < 2) {
24+
//如果是一位的话,补0
25+
hexString = "0" + hexString
26+
}
27+
sb.append(hexString)
28+
}
29+
return sb.toString()
30+
}
31+
32+
/**
33+
* md5加密
34+
*/
35+
fun ByteArray.md5():String{
36+
val instance: MessageDigest = MessageDigest.getInstance("MD5")
37+
//对字符串加密,返回字节数组
38+
val digest:ByteArray = instance.digest(this)
39+
var sb = StringBuffer()
40+
for (b in digest) {
41+
//获取低八位有效值
42+
var i :Int = b.toInt() and 0xff
43+
//将整数转化为16进制
44+
var hexString = Integer.toHexString(i)
45+
if (hexString.length < 2) {
46+
//如果是一位的话,补0
47+
hexString = "0" + hexString
48+
}
49+
sb.append(hexString)
50+
}
51+
return sb.toString()
52+
}

utils/src/main/java/com/catchpig/utils/ext/ToastExt.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.catchpig.utils.ext
33
import android.content.Context
44
import android.widget.Toast
55
import androidx.annotation.StringRes
6+
import androidx.fragment.app.Fragment
67

78
/**
89
*
@@ -27,10 +28,42 @@ fun Context.longToast(@StringRes id: Int) {
2728
toast(id, Toast.LENGTH_LONG)
2829
}
2930

31+
fun Fragment.toast(content: String){
32+
this.activity?.toast(content)
33+
}
34+
35+
fun Fragment.toast(@StringRes id:Int){
36+
this.activity?.toast(id)
37+
}
38+
39+
fun Fragment.longToast(content: String){
40+
this.activity?.longToast(content)
41+
}
42+
43+
fun Fragment.longToast(@StringRes id:Int){
44+
this.activity?.longToast(id)
45+
}
46+
3047
fun String.toast(context: Context){
3148
context.toast(this)
3249
}
3350

3451
fun String.longToast(context: Context){
3552
context.longToast(this)
53+
}
54+
55+
fun Any.longToast(context: Context,content: String){
56+
content.longToast(context)
57+
}
58+
59+
fun Any.longToast(context: Context,@StringRes id:Int){
60+
context.longToast(id)
61+
}
62+
63+
fun Any.toast(context: Context,content: String){
64+
content.toast(context)
65+
}
66+
67+
fun Any.toast(context: Context,@StringRes id:Int){
68+
context.toast(id)
3669
}

utils/src/test/java/com/catchpig/utils/ExampleUnitTest.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.catchpig.utils
22

3+
import com.catchpig.utils.ext.md5
34
import org.junit.Test
45

56
import org.junit.Assert.*
@@ -14,4 +15,8 @@ class ExampleUnitTest {
1415
fun addition_isCorrect() {
1516
assertEquals(4, 2 + 2)
1617
}
18+
@Test
19+
fun encryption(){
20+
println("1".md5())
21+
}
1722
}

0 commit comments

Comments
 (0)