Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.security.MessageDigest;
import java.util.Locale;

public class Main {
    public static String md5String(String val) {
        if (val == null)
            return null;
        byte[] resBytes;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(val.getBytes("UTF-8"));
            resBytes = md.digest();
            return hexString(resBytes);
        } catch (Exception e) {
            return null;
        }
    }

    public static String hexString(byte[] bytes) {
        StringBuilder val = new StringBuilder();
        for (byte b : bytes) {
            if (b <= 15)
                val.append("0");
            val.append(Integer.toHexString(b & 0xFF));
        }
        return val.toString().toLowerCase(Locale.US);
    }
}