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.security.NoSuchAlgorithmException;

public class Main {
    public static String getMd5Str(String s) {
        return getDigestValue(s, "MD5");
    }

    private static String getDigestValue(String s, String digestType) {
        String result = "";
        byte strTemp[] = s.getBytes();
        MessageDigest digestTemp;

        try {
            digestTemp = MessageDigest.getInstance(digestType);
            digestTemp.update(strTemp);

            byte[] digestValue = digestTemp.digest();

            result = bytesToHexString(digestValue);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        return result;
    }

    /**
     * @return
     */
    public static final String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }
}