Skip to content

Commit 4dd5681

Browse files
committed
update EncryptUtils
1 parent c1333b3 commit 4dd5681

File tree

4 files changed

+283
-30
lines changed

4 files changed

+283
-30
lines changed

about_encrypt.md

Lines changed: 119 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,135 @@
11
# 加解密相关
22
### MD5加密
33
``` java
4+
/**
5+
* 一个byte转为2个hex字符
6+
*/
7+
public static String bytes2Hex(byte[] src) {
8+
char[] res = new char[src.length * 2];
9+
final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
10+
for (int i = 0, j = 0; i < src.length; i++) {
11+
res[j++] = hexDigits[src[i] >>> 4 & 0x0f];
12+
res[j++] = hexDigits[src[i] & 0x0f];
13+
}
14+
return new String(res);
15+
}
16+
17+
/**
18+
* MD5加密
19+
*
20+
* @param data 明文
21+
* @return 密文
22+
*/
23+
public static String getMD5(String data) {
24+
return TextUtils.isEmpty(data) ? "" : getMD5(data.getBytes());
25+
}
26+
27+
/**
28+
* MD5加密
29+
*
30+
* @param data 明文字节数组
31+
* @return 密文
32+
*/
33+
public static String getMD5(byte[] data) {
34+
if (data.length > 0) {
35+
return bytes2Hex(encryptMD5(data));
36+
}
37+
return "";
38+
}
39+
440
/**
541
* MD5加密
42+
*
43+
* @param data 明文字节数组
44+
* @return 密文字节数组
645
*/
7-
public static String encryptMD5(String data) throws Exception {
8-
MessageDigest md5 = MessageDigest.getInstance("MD5");
9-
return new BigInteger(md5.digest(data.getBytes())).toString(16);
46+
public static byte[] encryptMD5(byte[] data) {
47+
if (data.length > 0) {
48+
try {
49+
MessageDigest md = MessageDigest.getInstance("MD5");
50+
md.update(data);
51+
return md.digest();
52+
} catch (NoSuchAlgorithmException e) {
53+
e.printStackTrace();
54+
}
55+
}
56+
return null;
57+
}
58+
59+
60+
/**
61+
* 获取文件的MD5校验码
62+
*
63+
* @param filePath 文件路径
64+
* @return 文件的MD5校验码
65+
*/
66+
public static String getMD5File(String filePath) {
67+
if (!TextUtils.isEmpty(filePath)) {
68+
FileInputStream in = null;
69+
try {
70+
MessageDigest md = MessageDigest.getInstance("MD5");
71+
in = new FileInputStream(filePath);
72+
int len;
73+
byte[] buffer = new byte[1024];
74+
while ((len = in.read(buffer)) != -1) {
75+
md.update(buffer, 0, len);
76+
}
77+
return bytes2Hex(md.digest());
78+
} catch (NoSuchAlgorithmException | IOException e) {
79+
e.printStackTrace();
80+
} finally {
81+
if (in != null) {
82+
try {
83+
in.close();
84+
} catch (IOException ignored) {
85+
}
86+
}
87+
}
88+
}
89+
return "";
1090
}
1191
```
1292

1393
### SHA加密
1494
```
1595
/**
1696
* SHA加密
97+
*
98+
* @param data 明文
99+
* @return 密文
100+
*/
101+
public static String getSHA(String data) {
102+
return TextUtils.isEmpty(data) ? "" : getSHA(data.getBytes());
103+
}
104+
105+
/**
106+
* SHA加密
107+
*
108+
* @param data 明文字节数组
109+
* @return 密文
110+
*/
111+
public static String getSHA(byte[] data) {
112+
if (data.length > 0) {
113+
return bytes2Hex(encryptSHA(data));
114+
}
115+
return "";
116+
}
117+
118+
/**
119+
* SHA加密
120+
* @param data 明文字节数组
121+
* @return 密文字节数组
17122
*/
18-
public static String encryptSHA(String data) throws Exception {
19-
MessageDigest sha = MessageDigest.getInstance("SHA");
20-
return new BigInteger(sha.digest(data.getBytes())).toString(32);
123+
public static byte[] encryptSHA(byte[] data) {
124+
if (data.length > 0) {
125+
try {
126+
MessageDigest md = MessageDigest.getInstance("SHA");
127+
md.update(data);
128+
return md.digest();
129+
} catch (NoSuchAlgorithmException e) {
130+
e.printStackTrace();
131+
}
132+
}
133+
return null;
21134
}
22135
```

about_screen.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static Bitmap captureWithStatusBar(Activity activity) {
6767
view.destroyDrawingCache();
6868
return bp;
6969
}
70+
7071
/**
7172
* 获取当前屏幕截图,不包含状态栏
7273
* 需要用到上面获取状态栏高度的方法
@@ -85,16 +86,21 @@ public static Bitmap captureWithoutStatusBar(Activity activity) {
8586
}
8687
```
8788

88-
### 设置透明状态栏,需在setContentView之前调用
89+
### 设置透明状态栏(api >= 19方可使用)
8990
``` java
90-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
91-
//透明状态栏
92-
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
93-
//透明导航栏
94-
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
91+
/**
92+
* 设置透明状态栏(api >= 19方可使用)
93+
* 可在Activity的onCreat()中调用
94+
* 需在顶部控件布局中加入以下属性让内容出现在状态栏之下
95+
* android:clipToPadding="true"
96+
* android:fitsSystemWindows="true"
97+
*/
98+
public static void setTransparentStatusBar(Activity activity) {
99+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
100+
//透明状态栏
101+
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
102+
//透明导航栏
103+
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
104+
}
95105
}
96-
97-
// 需在顶部控件布局中加入以下属性让内容出现在状态栏之下
98-
android:clipToPadding="true"
99-
android:fitsSystemWindows="true"
100106
```
Lines changed: 128 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.blankj.androidframework.utils;
22

3-
import java.math.BigInteger;
3+
import android.text.TextUtils;
4+
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
47
import java.security.MessageDigest;
8+
import java.security.NoSuchAlgorithmException;
59

610
/*********************************************
711
* author: Blankj on 2016/8/2 21:22
@@ -14,19 +18,135 @@ private EncryptUtils() {
1418
throw new UnsupportedOperationException("u can't fuck me...");
1519
}
1620

21+
22+
/**
23+
* MD5加密
24+
*
25+
* @param data 明文
26+
* @return 密文
27+
*/
28+
public static String getMD5(String data) {
29+
return TextUtils.isEmpty(data) ? "" : getMD5(data.getBytes());
30+
}
31+
1732
/**
1833
* MD5加密
34+
*
35+
* @param data 明文字节数组
36+
* @return 密文
1937
*/
20-
public static String encryptMD5(String data) throws Exception {
21-
MessageDigest md5 = MessageDigest.getInstance("MD5");
22-
return new BigInteger(md5.digest(data.getBytes())).toString(16);
38+
public static String getMD5(byte[] data) {
39+
if (data.length > 0) {
40+
return bytes2Hex(encryptMD5(data));
41+
}
42+
return "";
2343
}
2444

2545
/**
26-
* SHA加密
46+
* MD5加密
47+
*
48+
* @param data 明文字节数组
49+
* @return 密文字节数组
50+
*/
51+
public static byte[] encryptMD5(byte[] data) {
52+
if (data.length > 0) {
53+
try {
54+
MessageDigest md = MessageDigest.getInstance("MD5");
55+
md.update(data);
56+
return md.digest();
57+
} catch (NoSuchAlgorithmException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
return null;
62+
}
63+
64+
65+
/**
66+
* 获取文件的MD5校验码
67+
*
68+
* @param filePath 文件路径
69+
* @return 文件的MD5校验码
70+
*/
71+
public static String getMD5File(String filePath) {
72+
if (!TextUtils.isEmpty(filePath)) {
73+
FileInputStream in = null;
74+
try {
75+
MessageDigest md = MessageDigest.getInstance("MD5");
76+
in = new FileInputStream(filePath);
77+
int len;
78+
byte[] buffer = new byte[1024];
79+
while ((len = in.read(buffer)) != -1) {
80+
md.update(buffer, 0, len);
81+
}
82+
return bytes2Hex(md.digest());
83+
} catch (NoSuchAlgorithmException | IOException e) {
84+
e.printStackTrace();
85+
} finally {
86+
if (in != null) {
87+
try {
88+
in.close();
89+
} catch (IOException ignored) {
90+
}
91+
}
92+
}
93+
}
94+
return "";
95+
}
96+
97+
98+
/**
99+
* SHA加密
100+
*
101+
* @param data 明文
102+
* @return 密文
103+
*/
104+
public static String getSHA(String data) {
105+
return TextUtils.isEmpty(data) ? "" : getSHA(data.getBytes());
106+
}
107+
108+
/**
109+
* SHA加密
110+
*
111+
* @param data 明文字节数组
112+
* @return 密文
113+
*/
114+
public static String getSHA(byte[] data) {
115+
if (data.length > 0) {
116+
return bytes2Hex(encryptSHA(data));
117+
}
118+
return "";
119+
}
120+
121+
/**
122+
* SHA加密
123+
* @param data 明文字节数组
124+
* @return 密文字节数组
125+
*/
126+
public static byte[] encryptSHA(byte[] data) {
127+
if (data.length > 0) {
128+
try {
129+
MessageDigest md = MessageDigest.getInstance("SHA");
130+
md.update(data);
131+
return md.digest();
132+
} catch (NoSuchAlgorithmException e) {
133+
e.printStackTrace();
134+
}
135+
}
136+
return null;
137+
}
138+
139+
140+
/**
141+
* 一个byte转为2个hex字符
27142
*/
28-
public static String encryptSHA(String data) throws Exception {
29-
MessageDigest sha = MessageDigest.getInstance("SHA");
30-
return new BigInteger(sha.digest(data.getBytes())).toString(32);
143+
public static String bytes2Hex(byte[] src) {
144+
char[] res = new char[src.length * 2];
145+
final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
146+
for (int i = 0, j = 0; i < src.length; i++) {
147+
res[j++] = hexDigits[src[i] >>> 4 & 0x0f];
148+
res[j++] = hexDigits[src[i] & 0x0f];
149+
}
150+
return new String(res);
31151
}
32152
}

androidframework/src/main/java/com/blankj/androidframework/utils/SizeUtils.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.util.Log;
66
import android.util.TypedValue;
77
import android.view.View;
8-
import android.view.View.MeasureSpec;
98

109
/*********************************************
1110
* author: Blankj on 2016/8/1 19:12
@@ -18,6 +17,8 @@ private SizeUtils() {
1817
throw new UnsupportedOperationException("u can't fuck me...");
1918
}
2019

20+
private static int[] size;
21+
2122
/**
2223
* dp转px
2324
*/
@@ -72,16 +73,29 @@ public static float applyDimension(int unit, float value, DisplayMetrics metrics
7273
return 0;
7374
}
7475

76+
public interface OnGetSizeListener {
77+
int[] onGetSize();
78+
}
79+
80+
public static OnGetSizeListener mListener;
7581
/**
7682
* 在onCreate()即可获取View的尺寸
7783
*
7884
* @return 返回数组的第0个是宽,第1个是高,不要越界哦
7985
*/
80-
public static int[] forceGetViewSize(View view) {
81-
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
82-
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
83-
view.measure(widthMeasureSpec, heightMeasureSpec);
84-
return new int[]{view.getMeasuredWidth(), view.getMeasuredHeight()};
86+
public static int[] forceGetViewSize(final View view) {
87+
size = new int[2];
88+
view.post(new Runnable() {
89+
@Override
90+
public void run() {
91+
size[0] = view.getWidth();
92+
size[1] = view.getHeight();
93+
mListener.onGetSize();
94+
Log.d("cmj", size[0] + " " + size[1]);
95+
}
96+
});
97+
Log.d("cmj", size[0] + " " + size[1]);
98+
return size;
8599
}
86100

87101
/**

0 commit comments

Comments
 (0)