Java MD5 md5(final String text)

Here you can find the source of md5(final String text)

Description

calculates an MD5 hash.

License

Apache License

Parameter

Parameter Description
text the text to hash

Exception

Parameter Description
UnsupportedOperationException if MD5 is not available (which is very unlikely)

Declaration

public static String md5(final String text) 

Method Source Code

//package com.java2s;
/*/*from w ww .j a v  a  2  s  .  c  om*/
   Copyright 2014-now by Alain Stalder. Made in Switzerland.
    
   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.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.bind.DatatypeConverter;

public class Main {
    /**
     * UTF-8 character set
     * 
     * @since 1.0
     */
    public static final Charset CHARSET_UTF_8 = Charset.forName("UTF-8");

    /**
     * calculates an MD5 hash.
     * <p>
     * The given text is first UTF-8 encoded to bytes, then the MD5 hash
     * is calculated and finally returned as a hex string.
     * 
     * @param text the text to hash
     * @throws UnsupportedOperationException if MD5 is not available (which is very unlikely)
     * 
     * @since 1.0
     */
    public static String md5(final String text) {
        return hash(text, "MD5");
    }

    /**
     * calculates a cryptographic hash function (message digest).
     * <p>
     * The given text is first UTF-8 encoded to bytes, then the given hash
     * is calculated and finally returned as a hex string.
     * 
     * @param text the text to hash
     * @param algorithm the hash algorithm to use
     * @throws UnsupportedOperationException if the given hash algorithm is not available
     * 
     * @since 1.0
     */
    public static String hash(final String text, final String algorithm) {
        MessageDigest hash;
        try {
            hash = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new UnsupportedOperationException("No message digest " + algorithm + ".", e);
        }
        byte[] digestBytes = null;
        digestBytes = hash.digest(text.getBytes(CHARSET_UTF_8));
        String digest = DatatypeConverter.printHexBinary(digestBytes);
        return digest;
    }
}

Related

  1. getMd5Script(String dstFilePath)
  2. inputStreamMD5(InputStream entity)
  3. isEtagAlsoAnMD5Hash(String etag)
  4. looksLikeValidMd5(String possibleMd5)
  5. md5(File f)
  6. md5(final String... tokens)
  7. md5(String source)
  8. md5(String toDigest)
  9. md5_hh(int C, int B, int G, int F, int A, int E, int D)