Java MD5 String md5(String string)

Here you can find the source of md5(String string)

Description

Hashes the given string using the MD5 algorithm.

License

Open Source License

Declaration

public static byte[] md5(String string) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

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

public class Main {
    /** Hashes the given {@code string} using the MD5 algorithm. */
    public static byte[] md5(String string) {
        return hash("MD5", string);
    }/*from   w  w  w  .  j  a  v  a2s.c o m*/

    /** Hashes the given {@code string} using the given {@code algorithm}. */
    public static byte[] hash(String algorithm, String string) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException ex) {
            throw new IllegalArgumentException(String.format("[%s] isn't a valid hash algorithm!", algorithm), ex);
        }

        byte[] bytes;
        try {
            bytes = string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new IllegalStateException(ex);
        }

        return digest.digest(bytes);
    }
}

Related

  1. MD5(String string)
  2. md5(String string)
  3. md5(String string)
  4. md5(String string)
  5. md5(String string)
  6. md5(String string)
  7. md5(String string)
  8. md5(String strs)
  9. md5(String target)