|
18 | 18 |
|
19 | 19 | import android.util.Base64;
|
20 | 20 |
|
| 21 | +import java.io.IOException; |
21 | 22 | import java.io.InputStream;
|
| 23 | +import java.security.DigestInputStream; |
22 | 24 | import java.security.MessageDigest;
|
23 | 25 | import java.security.NoSuchAlgorithmException;
|
24 | 26 |
|
25 | 27 | /**
|
26 | 28 | * Create by h4de5ing 2016/5/7 007
|
27 | 29 | */
|
28 | 30 | public class CipherUtils {
|
29 |
| - |
30 |
| - public static String md5Encode(InputStream in) { |
| 31 | + /** |
| 32 | + * checked |
| 33 | + */ |
| 34 | + public static String encode(String input) { |
31 | 35 | 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]; |
47 | 46 | }
|
48 |
| - return sb.toString(); |
49 |
| - } catch (Exception e) { |
50 |
| - e.printStackTrace(); |
| 47 | + return new String(resultCharArray); |
| 48 | + } catch (NoSuchAlgorithmException e) { |
| 49 | + return null; |
51 | 50 | }
|
52 |
| - return null; |
53 | 51 | }
|
54 | 52 |
|
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; |
57 | 59 | 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]; |
68 | 72 | }
|
69 |
| - return sb.toString(); |
| 73 | + return new String(resultCharArray); |
70 | 74 | } catch (NoSuchAlgorithmException e) {
|
| 75 | + return null; |
| 76 | + } catch (IOException e) { |
71 | 77 | e.printStackTrace();
|
| 78 | + } finally { |
| 79 | + try { |
| 80 | + if (digestInputStream != null) |
| 81 | + digestInputStream.close(); |
| 82 | + } catch (Exception e) { |
| 83 | + e.printStackTrace(); |
| 84 | + } |
72 | 85 | }
|
73 | 86 | return null;
|
74 | 87 | }
|
|
0 commit comments