Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.math.BigInteger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**
     * Calculates an MD5 hash for a text.
     * 
     * @param dataToEncode
     *            The data to encode.
     * @return A text representation of a hexadecimal value of length 32.
     */
    public static String messageDigestFive(final String dataToEncode) {
        MessageDigest m;
        try {
            m = MessageDigest.getInstance("MD5");
            byte[] data = dataToEncode.getBytes();
            m.update(data, 0, data.length);
            BigInteger i = new BigInteger(1, m.digest());
            return String.format("%1$032X", i);
        } catch (NoSuchAlgorithmException e) {
            // MD5 Should be supported by the runtime!
            throw new IllegalStateException(e);
        }
    }
}