Skip to content

Commit fc18a7a

Browse files
committed
see 08/14 log
1 parent 4cf7493 commit fc18a7a

File tree

10 files changed

+315
-189
lines changed

10 files changed

+315
-189
lines changed

md/update_log.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
### 更新Log
2+
#### 16/08/14 新增DES加密及单元检测
23
#### 16/08/13 新增MD2,SHA224,SHA256,SHA384,SHA512加密及单元测试,正折腾DES加密
34
#### 16/08/12 新增Base64和Html编码解码及他们的单元测试,新增TimeUtils单元测试,更新md
45
#### 16/08/11 新增SDCardUtils,UnitUtils,单元测试慢慢完善中
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.blankj.utilcode.utils;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2016/8/11
8+
* desc : 单位相关工具类
9+
* </pre>
10+
*/
11+
public class ConstUtils {
12+
13+
private ConstUtils() {
14+
throw new UnsupportedOperationException("u can't fuck me...");
15+
}
16+
17+
/******************** 存储相关常量 ********************/
18+
/**
19+
* Byte与Byte的倍数
20+
*/
21+
public static final long BYTE = 1;
22+
/**
23+
* KB与Byte的倍数
24+
*/
25+
public static final long KB = 1024;
26+
/**
27+
* MB与Byte的倍数
28+
*/
29+
public static final long MB = 1048576;
30+
/**
31+
* GB与Byte的倍数
32+
*/
33+
public static final long GB = 1073741824;
34+
35+
/******************** 时间相关常量 ********************/
36+
/**
37+
* 毫秒与毫秒的倍数
38+
*/
39+
public static final int MSEC = 1;
40+
/**
41+
* 秒与毫秒的倍数
42+
*/
43+
public static final int SEC = 1000;
44+
/**
45+
* 分与毫秒的倍数
46+
*/
47+
public static final int MIN = 60000;
48+
/**
49+
* 时与毫秒的倍数
50+
*/
51+
public static final int HOUR = 3600000;
52+
/**
53+
* 天与毫秒的倍数
54+
*/
55+
public static final int DAY = 86400000;
56+
57+
/******************** DES加密相关常量 ********************/
58+
/**
59+
* DES加密为ECB、无填充
60+
*/
61+
public static final String DES_ECB_NO_PADDING = "DES/ECB/NoPadding";
62+
/**
63+
* DES加密为CBC、无填充
64+
*/
65+
public static final String DES_CBC_NO_PADDING = "DES/CBC/NoPadding";
66+
/**
67+
* DES加密为CFB、无填充
68+
*/
69+
public static final String DES_CFB_NO_PADDING = "DES/CFB/NoPadding";
70+
/**
71+
* DES加密为OFB、无填充
72+
*/
73+
public static final String DES_OFB_NO_PADDING = "DES/OFB/NoPadding";
74+
/**
75+
* DES加密为ECB、零填充
76+
*/
77+
public static final String DES_ECB_ZEROS_PADDING = "DES/ECB/ZerosPadding";
78+
/**
79+
* DES加密为CBC、零填充
80+
*/
81+
public static final String DES_CBC_ZEROS_PADDING = "DES/CBC/ZerosPadding";
82+
/**
83+
* DES加密为CFB、零填充
84+
*/
85+
public static final String DES_CFB_ZEROS_PADDING = "DES/CFB/ZerosPadding";
86+
/**
87+
* DES加密为OFB、零填充
88+
*/
89+
public static final String DES_OFB_ZEROS_PADDING = "DES/OFB/ZerosPadding";
90+
/**
91+
* DES加密为ECB、PKCS5Padding填充
92+
*/
93+
public static final String DES_ECB_PKCS5_PADDING = "DES/ECB/PKCS5Padding";
94+
/**
95+
* DES加密为CBC、PKCS5Padding填充
96+
*/
97+
public static final String DES_CBC_PKCS5_PADDING = "DES/CBC/PKCS5Padding";
98+
/**
99+
* DES加密为CFB、PKCS5Padding填充
100+
*/
101+
public static final String DES_CFB_PKCS5_PADDING = "DES/CFB/PKCS5Padding";
102+
/**
103+
* DES加密为OFB、PKCS5Padding填充
104+
*/
105+
public static final String DES_OFB_PKCS5_PADDING = "DES/OFB/PKCS5Padding";
106+
}

utilcode/src/main/java/com/blankj/utilcode/utils/ConvertUtils.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static byte[] hexString2Bytes(String hexString) {
4343
throw new IllegalArgumentException("长度不是偶数");
4444
}
4545
char[] hexBytes = hexString.toUpperCase().toCharArray();
46-
byte[] res = new byte[len / 2];
46+
byte[] res = new byte[len >>> 1];
4747
for (int i = 0; i < len; i += 2) {
4848
res[i >> 1] = (byte) (hex2Dec(hexBytes[i]) << 4 | hex2Dec(hexBytes[i + 1]));
4949
}
@@ -59,10 +59,34 @@ public static byte[] hexString2Bytes(String hexString) {
5959
private static int hex2Dec(char hexChar) {
6060
if (hexChar >= '0' && hexChar <= '9') {
6161
return hexChar - '0';
62-
} else if (hexChar >= 'A' && hexChar <= 'E') {
62+
} else if (hexChar >= 'A' && hexChar <= 'F') {
6363
return hexChar - 'A' + 10;
6464
} else {
6565
throw new IllegalArgumentException();
6666
}
6767
}
68+
69+
/**
70+
* char转byte
71+
*/
72+
public static byte[] getBytes(char[] chars) {
73+
int len = chars.length;
74+
byte[] bytes = new byte[len];
75+
for (int i = 0; i < len; i++) {
76+
bytes[i] = (byte) (chars[i]);
77+
}
78+
return bytes;
79+
}
80+
81+
/**
82+
* byte转char
83+
*/
84+
public static char[] getChars(byte[] bytes) {
85+
int len = bytes.length;
86+
char[] chars = new char[len];
87+
for (int i = 0; i < len; i++) {
88+
chars[i] = (char) (bytes[i] & 0xff);
89+
}
90+
return chars;
91+
}
6892
}

utilcode/src/main/java/com/blankj/utilcode/utils/EncodeUtils.java

+26-6
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static String urlDecode(String input, String charset) {
8282
* @param input 要编码的字符串
8383
* @return Base64编码后的字符串
8484
*/
85-
public static String base64Encode(String input) {
85+
public static byte[] base64Encode(String input) {
8686
return base64Encode(input.getBytes());
8787
}
8888

@@ -92,8 +92,28 @@ public static String base64Encode(String input) {
9292
* @param input 要编码的字节数组
9393
* @return Base64编码后的字符串
9494
*/
95-
public static String base64Encode(byte[] input) {
96-
return Base64.encodeToString(input, Base64.DEFAULT);
95+
public static byte[] base64Encode(byte[] input) {
96+
return Base64.encode(input, Base64.NO_WRAP);
97+
}
98+
99+
/**
100+
* Base64编码
101+
*
102+
* @param input 要编码的字节数组
103+
* @return Base64编码后的字符串
104+
*/
105+
public static String base64Encode2String(byte[] input) {
106+
return Base64.encodeToString(input, Base64.NO_WRAP);
107+
}
108+
109+
/**
110+
* Base64解码
111+
*
112+
* @param input 要解码的字符串
113+
* @return Base64解码后的字符串
114+
*/
115+
public static byte[] base64Decode(String input) {
116+
return Base64.decode(input, Base64.NO_WRAP);
97117
}
98118

99119
/**
@@ -102,8 +122,8 @@ public static String base64Encode(byte[] input) {
102122
* @param input 要解码的字符串
103123
* @return Base64解码后的字符串
104124
*/
105-
public static String base64Decode(String input) {
106-
return new String(Base64.decode(input, Base64.DEFAULT));
125+
public static byte[] base64Decode(byte[] input) {
126+
return Base64.decode(input, Base64.NO_WRAP);
107127
}
108128

109129
/**
@@ -114,7 +134,7 @@ public static String base64Decode(String input) {
114134
* @return Base64URL安全编码后的字符串
115135
*/
116136
public static String base64UrlSafeEncode(String input) {
117-
return Base64.encodeToString(input.getBytes(), Base64.URL_SAFE);
137+
return new String(Base64.encode(input.getBytes(), Base64.URL_SAFE));
118138
}
119139

120140
/**

0 commit comments

Comments
 (0)