All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soento.core.util.Md5Util Maven / Gradle / Ivy

package com.soento.core.util;

import java.security.MessageDigest;

/**
 * @author yantao.zeng
 */
public class Md5Util {
    private final static char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String encode(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes("UTF-8"));
            byte[] di = md.digest();
            char[] str = new char[16 * 2];
            int k = 0;
            final int num16 = 16;
            for (int i = 0; i < num16; i++) {
                byte byte0 = di[i];
                str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
                str[k++] = HEX_DIGITS[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String encode(String input, String salt) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update((input + salt).getBytes("UTF-8"));
            byte[] di = md.digest();
            char[] str = new char[16 * 2];
            int k = 0;
            final int num16 = 16;
            for (int i = 0; i < num16; i++) {
                byte byte0 = di[i];
                str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
                str[k++] = HEX_DIGITS[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy