|
| 1 | +package encryptionUtils; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.nio.MappedByteBuffer; |
| 6 | +import java.nio.channels.FileChannel; |
| 7 | +import java.security.MessageDigest; |
| 8 | +import java.security.NoSuchAlgorithmException; |
| 9 | + |
| 10 | + |
| 11 | +public class MD5Util { |
| 12 | + |
| 13 | + protected static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', |
| 14 | + '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 15 | + |
| 16 | + protected static MessageDigest messagedigest = null; |
| 17 | + |
| 18 | + static { |
| 19 | + try { |
| 20 | + messagedigest = MessageDigest.getInstance("MD5"); |
| 21 | + } catch (NoSuchAlgorithmException e) { |
| 22 | + e.printStackTrace(); |
| 23 | + messagedigest = null; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public static String getFileMD5String(File file) { |
| 28 | + String ret = null; |
| 29 | + if (messagedigest != null) { |
| 30 | + FileInputStream in = null; |
| 31 | + FileChannel ch = null; |
| 32 | + try { |
| 33 | + in = new FileInputStream(file); |
| 34 | + ch = in.getChannel(); |
| 35 | + MappedByteBuffer byteBuffer = ch.map( |
| 36 | + FileChannel.MapMode.READ_ONLY, 0, file.length()); |
| 37 | + messagedigest.update(byteBuffer); |
| 38 | + } catch (Exception e) { |
| 39 | + throw new RuntimeException(e); |
| 40 | + } finally { |
| 41 | + if (in != null) { |
| 42 | + try { |
| 43 | + in.close(); |
| 44 | + } catch (Exception e) { |
| 45 | + |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + } |
| 49 | + if (ch != null) { |
| 50 | + try { |
| 51 | + ch.close(); |
| 52 | + } catch (Exception e) { |
| 53 | + |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + ret = bufferToHex(messagedigest.digest()); |
| 59 | + } |
| 60 | + |
| 61 | + return ret; |
| 62 | + } |
| 63 | + |
| 64 | + public static String getMD5String(String s) { |
| 65 | + return getMD5String(s.getBytes()); |
| 66 | + } |
| 67 | + |
| 68 | + public static String getMD5String(byte[] bytes) { |
| 69 | + String bufferToHex = null; |
| 70 | + if (messagedigest != null) { |
| 71 | + messagedigest.update(bytes); |
| 72 | + bufferToHex = bufferToHex(messagedigest.digest()); |
| 73 | + } |
| 74 | + |
| 75 | + return bufferToHex; |
| 76 | + } |
| 77 | + |
| 78 | + private static String bufferToHex(byte bytes[]) { |
| 79 | + return bufferToHex(bytes, 0, bytes.length); |
| 80 | + } |
| 81 | + |
| 82 | + private static String bufferToHex(byte bytes[], int m, int n) { |
| 83 | + StringBuffer stringbuffer = new StringBuffer(2 * n); |
| 84 | + int k = m + n; |
| 85 | + for (int l = m; l < k; l++) { |
| 86 | + appendHexPair(bytes[l], stringbuffer); |
| 87 | + } |
| 88 | + return stringbuffer.toString(); |
| 89 | + } |
| 90 | + |
| 91 | + private static void appendHexPair(byte bt, StringBuffer stringbuffer) { |
| 92 | + char c0 = hexDigits[(bt & 0xf0) >> 4]; |
| 93 | + char c1 = hexDigits[bt & 0xf]; |
| 94 | + stringbuffer.append(c0); |
| 95 | + stringbuffer.append(c1); |
| 96 | + } |
| 97 | +} |
0 commit comments