MD5.java

/*
 * Decompiled with CFR 0_132.
 */
package org.xutils.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.xutils.common.util.IOUtil;

public final class MD5 {
    private static final char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private MD5() {
    }

    public static String toHexString(byte[] bytes) {
        if (bytes == null) {
            return "";
        }
        StringBuilder hex = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            hex.append(hexDigits[b >> 4 & 15]);
            hex.append(hexDigits[b & 15]);
        }
        return hex.toString();
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public static String md5(File file) throws IOException {
        MessageDigest messagedigest = null;
        FileInputStream in = null;
        FileChannel ch = null;
        byte[] encodeBytes = null;
        try {
            messagedigest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            ch = in.getChannel();
            MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0L, file.length());
            messagedigest.update(byteBuffer);
            encodeBytes = messagedigest.digest();
        }
        catch (NoSuchAlgorithmException neverHappened) {
            try {
                throw new RuntimeException(neverHappened);
            }
            catch (Throwable throwable) {
                IOUtil.closeQuietly(in);
                IOUtil.closeQuietly(ch);
                throw throwable;
            }
        }
        IOUtil.closeQuietly(in);
        IOUtil.closeQuietly(ch);
        return MD5.toHexString(encodeBytes);
    }

    public static String md5(String string) {
        byte[] encodeBytes = null;
        try {
            encodeBytes = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        }
        catch (NoSuchAlgorithmException neverHappened) {
            throw new RuntimeException(neverHappened);
        }
        catch (UnsupportedEncodingException neverHappened) {
            throw new RuntimeException(neverHappened);
        }
        return MD5.toHexString(encodeBytes);
    }
}