Java MD5 String md5(String str)

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

Description

Determine the message digest

License

Apache License

Parameter

Parameter Description
str a parameter

Exception

Parameter Description
FormatException an exception

Declaration

public static byte[] md5(String str) 

Method Source Code

//package com.java2s;
/*//from  w w  w.java  2  s. co  m
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    private static final String UTF8 = "UTF-8";
    private static final String MD5_ALGORITHM = "MD5";

    /**
     * Determine the message digest
     *
     * @param str
     * @return
     * @throws FormatException
     */
    public static byte[] md5(String str) {
        try {
            MessageDigest md = getMessageDigest();
            return md.digest(str.getBytes(UTF8));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static MessageDigest getMessageDigest() throws NoSuchAlgorithmException {
        return MessageDigest.getInstance(MD5_ALGORITHM);
    }
}

Related

  1. md5(String str)
  2. MD5(String str)
  3. MD5(String str)
  4. md5(String str)
  5. md5(String str)
  6. MD5(String str)
  7. md5(String str)
  8. MD5(String str)
  9. md5(String str)