Skip to content

Commit 40a7a5f

Browse files
committed
[commit] modify CipherUtils
1 parent 82d08c4 commit 40a7a5f

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

library/src/main/java/com/code19/library/CipherUtils.java

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,57 +18,70 @@
1818

1919
import android.util.Base64;
2020

21+
import java.io.IOException;
2122
import java.io.InputStream;
23+
import java.security.DigestInputStream;
2224
import java.security.MessageDigest;
2325
import java.security.NoSuchAlgorithmException;
2426

2527
/**
2628
* Create by h4de5ing 2016/5/7 007
2729
*/
2830
public class CipherUtils {
29-
30-
public static String md5Encode(InputStream in) {
31+
/**
32+
* checked
33+
*/
34+
public static String encode(String input) {
3135
try {
32-
MessageDigest digester = MessageDigest.getInstance("MD5");
33-
byte[] bytes = new byte[8192];
34-
int byteCount;
35-
while ((byteCount = in.read(bytes)) > 0) {
36-
digester.update(bytes, 0, byteCount);
37-
}
38-
byte[] digest = digester.digest();
39-
StringBuilder sb = new StringBuilder();
40-
for (byte b : digest) {
41-
int r = b & 0xff;
42-
String hex = Integer.toHexString(r);
43-
if (hex.length() == 1) {
44-
hex = 0 + hex;
45-
}
46-
sb.append(hex);
36+
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
37+
byte[] inputByteArray = input.getBytes();
38+
messageDigest.update(inputByteArray);
39+
byte[] resultByteArray = messageDigest.digest();
40+
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
41+
char[] resultCharArray = new char[resultByteArray.length * 2];
42+
int index = 0;
43+
for (byte b : resultByteArray) {
44+
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
45+
resultCharArray[index++] = hexDigits[b & 0xf];
4746
}
48-
return sb.toString();
49-
} catch (Exception e) {
50-
e.printStackTrace();
47+
return new String(resultCharArray);
48+
} catch (NoSuchAlgorithmException e) {
49+
return null;
5150
}
52-
return null;
5351
}
5452

55-
56-
public static String md5Encode(String pwd) {
53+
/**
54+
* checked
55+
*/
56+
public static String md5Encode(InputStream in) {
57+
int bufferSize = 256 * 1024;
58+
DigestInputStream digestInputStream = null;
5759
try {
58-
MessageDigest instance = MessageDigest.getInstance("md5");
59-
byte[] digest = instance.digest(pwd.getBytes());
60-
StringBuilder sb = new StringBuilder();
61-
for (byte b : digest) {
62-
int r = b & 0xff;
63-
String hex = Integer.toHexString(r);
64-
if (hex.length() == 1) {
65-
hex = 0 + hex;
66-
}
67-
sb.append(r);
60+
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
61+
digestInputStream = new DigestInputStream(in, messageDigest);
62+
byte[] buffer = new byte[bufferSize];
63+
while (digestInputStream.read(buffer) > 0) ;
64+
messageDigest = digestInputStream.getMessageDigest();
65+
byte[] resultByteArray = messageDigest.digest();
66+
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
67+
char[] resultCharArray = new char[resultByteArray.length * 2];
68+
int index = 0;
69+
for (byte b : resultByteArray) {
70+
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
71+
resultCharArray[index++] = hexDigits[b & 0xf];
6872
}
69-
return sb.toString();
73+
return new String(resultCharArray);
7074
} catch (NoSuchAlgorithmException e) {
75+
return null;
76+
} catch (IOException e) {
7177
e.printStackTrace();
78+
} finally {
79+
try {
80+
if (digestInputStream != null)
81+
digestInputStream.close();
82+
} catch (Exception e) {
83+
e.printStackTrace();
84+
}
7285
}
7386
return null;
7487
}

0 commit comments

Comments
 (0)