Skip to content

Commit 324f354

Browse files
committed
add encrypt
1 parent 48216d0 commit 324f354

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@
5151
- 切换键盘显示与否状态
5252
- [正则相关](https://github.com/Blankj/AndroidUtilCode/blob/master/about_regular.md)
5353
- 正则工具类
54+
- [加解密相关](https://github.com/Blankj/AndroidUtilCode/blob/master/about_encrypt.md)
55+
- MD5加密
56+
- SHA加密
5457
- [未归类](https://github.com/Blankj/AndroidUtilCode/blob/master/unclassified.md)
5558
- 获取服务是否开启
56-
- MD5加密
5759

5860
期待你的Star和完善...

about_encrypt.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 加解密相关
2+
### MD5加密
3+
``` java
4+
/**
5+
* MD5加密
6+
*/
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);
10+
}
11+
```
12+
13+
### SHA加密
14+
```
15+
/**
16+
* SHA加密
17+
*/
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);
21+
}
22+
```

unclassified.md

-38
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,3 @@ public static boolean isRunningService(String className, Context context) {
2323
return false;
2424
}
2525
```
26-
27-
### MD5加密
28-
``` java
29-
/**
30-
* MD5加密
31-
*/
32-
public static String passwordMD5(String password) {
33-
StringBuilder sb = new StringBuilder();
34-
try {
35-
//1.获取数据摘要器
36-
//arg0 : 加密的方式
37-
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
38-
//2.将一个byte数组进行加密,返回的是一个加密过的byte数组,二进制的哈希计算,md5加密的第一步
39-
byte[] digest = messageDigest.digest(password.getBytes());
40-
//3.遍历byte数组
41-
for (int i = 0; i < digest.length; i++) {
42-
//4.MD5加密
43-
//byte值 -128-127
44-
int result = digest[i] & 0xff;
45-
//将得到int类型转化成16进制字符串
46-
//String hexString = Integer.toHexString(result)+1;//不规则加密,加盐
47-
String hexString = Integer.toHexString(result);
48-
if (hexString.length() < 2) {
49-
// System.out.print("0");
50-
sb.append("0");
51-
}
52-
//System.out.println(hexString);
53-
//e10adc3949ba59abbe56e057f20f883e
54-
sb.append(hexString);
55-
}
56-
return sb.toString();
57-
} catch (NoSuchAlgorithmException e) {
58-
//找不到加密方式的异常
59-
e.printStackTrace();
60-
}
61-
return null;
62-
}
63-
```

0 commit comments

Comments
 (0)