Here you can find the source of md5(final String text)
Parameter | Description |
---|---|
text | the text to hash |
Parameter | Description |
---|---|
UnsupportedOperationException | if MD5 is not available (which is very unlikely) |
public static String md5(final String text)
//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; } }