Java MD5 String md5(final String s)

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

Description

Fast and dirty MD5 hasher.

License

Apache License

Parameter

Parameter Description
s The String to be hashed

Return

Hashed value of the String

Declaration


final static String md5(final String s) 

Method Source Code


//package com.java2s;
/*//from  w  w w.  ja v  a  2 s  .co  m
 * 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.
 * 
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 */

import java.math.BigInteger;
import java.security.MessageDigest;

public class Main {
    /**
     * Fast and dirty MD5 hasher. Please note that this logic does not always work properly
     * 
     * @param s The String to be hashed
     * @return Hashed value of the String
     */

    final static String md5(final String s) {

        if (s == null) {
            throw new NullPointerException();
        }

        if (s.length() == 0) {
            return "";
        }

        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(s.getBytes());
            BigInteger hash = new BigInteger(1, md5.digest());
            return hash.toString(16);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}

Related

  1. md5(final String inData)
  2. md5(final String input)
  3. md5(final String input)
  4. md5(final String message)
  5. md5(final String s)
  6. md5(final String str)
  7. md5(final String string)
  8. md5(final String string)
  9. md5(final String text)