Java MD5 String md5(String source, int bit)

Here you can find the source of md5(String source, int bit)

Description

md

License

Open Source License

Declaration

public static String md5(String source, int bit) throws NoSuchAlgorithmException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String md5(String source, int bit) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(source.getBytes());/*from   w ww  .j  a va  2s .  c  om*/
        byte b[] = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        if (bit == 16) {
            return buf.substring(8, 24);
        }
        if (bit == 32) {
            return buf.toString();
        }
        return null;
    }
}

Related

  1. md5(String source)
  2. md5(String source)
  3. md5(String source)
  4. md5(String source)
  5. md5(String source)
  6. MD5(String sourceStr)
  7. MD5(String src)
  8. md5(String src)
  9. md5(String src)