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[] hexdigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c",
            "d", "e", "f" };

    public static String md5(String s) {

        try {//w  w  w  .ja  v  a2 s  . c  o m
            MessageDigest md = MessageDigest.getInstance("md5");
            md.reset();
            md.update(s.getBytes());
            byte[] encodedStr = md.digest();
            StringBuffer buf = new StringBuffer();

            for (byte anEncodedStr : encodedStr) {
                int n = ((int) anEncodedStr & 0xff);
                int d1 = n / 16;
                int d2 = n % 16;
                buf.append(hexdigits[d1]).append(hexdigits[d2]);
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}

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)