Java MD5 String md5(String... args)

Here you can find the source of md5(String... args)

Description

md

License

Apache License

Declaration

public static String md5(String... args) throws Exception 

Method Source Code

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

import java.security.MessageDigest;

public class Main {
    private final static String CHARSET = "UTF-8";
    private final static String SEPARATOR = "$";

    public static String md5(String... args) throws Exception {
        String tmp = joinStrings(args);
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();//  ww w  .j a v a 2 s.c  o m
        messageDigest.update(tmp.getBytes(CHARSET));
        byte[] byteArray = messageDigest.digest();
        StringBuffer md5StrBuff = new StringBuffer();
        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
                md5StrBuff.append("0").append(
                        Integer.toHexString(0xFF & byteArray[i]));
            } else {
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
            }
        }
        return md5StrBuff.toString();

    }

    private static String joinStrings(String... array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i < array.length - 1) {
                sb.append(SEPARATOR);
            }
        }
        return sb.toString();
    }
}

Related

  1. md5(String userPass)
  2. md5(String value)
  3. md5(String value)
  4. md5(String value)
  5. md5(String value)
  6. MD5(String... texts)
  7. md52(String string)
  8. MD5_HEX(String source)
  9. md5AsHexString(String text, String charset)