java MD5Utils.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java MD5Utils.java相关的知识,希望对你有一定的参考价值。

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

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

    public MD5Utils() {
    }

    private static String md5(String data) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] b = data.getBytes("UTF8");
        md5.update(b, 0, b.length);
        return byteArrayToHexString(md5.digest());
    }

    private static String byteArrayToHexString(byte[] b) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < b.length; ++i) {
            sb.append(byteToHexString(b[i]));
        }

        return sb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (b < 0) {
            n = 256 + b;
        }

        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static String genMd5(String msg) {
        String messageDigest = null;

        try {
            messageDigest = md5(msg);
            return messageDigest;
        } catch (Exception var3) {
            var3.printStackTrace();
            throw new RuntimeException("Md5 Error. Cause: " + var3);
        }
    }

    public static String genB2bMd5(Map<String, String> map) {
        List<String> md5List = new ArrayList();
        String md5Str = "";
        String separation = ":";
        Iterator i$ = map.entrySet().iterator();

        while (true) {
            String name;
            String value;
            do {
                do {
                    do {
                        if (!i$.hasNext()) {
                            Object[] md5Array = md5List.toArray();
                            Arrays.sort(md5Array);

                            for (int i = 0; i < md5Array.length; ++i) {
                                md5Str = md5Str + md5Array[i];
                                if (i != md5Array.length - 1) {
                                    md5Str = md5Str + separation;
                                }
                            }

                            return genMd5(md5Str);
                        }

                        Map.Entry<String, String> entry = (Map.Entry) i$.next();
                        name = (String) entry.getKey();
                        value = (String) entry.getValue();
                    } while ("transactionType".equalsIgnoreCase(name));
                } while ("checksum".equalsIgnoreCase(name));
            } while ("multiplierFlag".equalsIgnoreCase(name) && !"1".equalsIgnoreCase(value));

            md5List.add(name + "=" + value);
        }
    }
}

以上是关于java MD5Utils.java的主要内容,如果未能解决你的问题,请参考以下文章

Java Switch

Java Math

Java 布尔运算

java [Java] Java常用代码#java

Java - 35 Java 实例

Java While 循环