Java MD5 String md5(String s)

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

Description

md

License

Apache License

Declaration

public static String md5(String s) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final String MD5 = "MD5";

    public static String md5(String s) {
        return encrypt(s, MD5);
    }// w  w w  .j av  a 2s  .  c  om

    public static String encrypt(String s, String algorithm) {

        byte[] unencodedPassword = s.getBytes();

        MessageDigest md = null;

        // first create an instance, given the provider
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        md.reset();

        // call the update method one or more times
        // (useful when you don't know the size of your data, eg. stream)
        md.update(unencodedPassword);

        // now calculate the hash
        byte[] bytes = md.digest();

        StringBuffer result = new StringBuffer();

        for (int i = 0; i < bytes.length; i++) {
            if (((int) bytes[i] & 0xff) < 0x10) {
                result.append("0");
            }

            result.append(Long.toString((int) bytes[i] & 0xff, 16));
        }

        return result.toString();
    }
}

Related

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