JAVA MD5 Tool Class (Super Multifunction) __java

Source: Internet
Author: User

The jar you must have:


		<!--Https://mvnrepository.com/artifact/com.google.guava/guava-->
		<dependency>
			<groupid >com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>19.0 </version>
		</dependency>

		<!--https://mvnrepository.com/artifact/com.google.code.gson/ Gson-->
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactid >gson</artifactId>
			<version>2.8.0</version>
		</dependency>

Tool Class Code:


Import Java.io.File;
Import java.io.IOException;
Import Java.lang.reflect.Field;
Import Java.nio.charset.Charset;
Import java.util.List;

Import Java.util.Map;
Import Com.google.common.collect.Maps;
Import Com.google.common.hash.Funnel;
Import Com.google.common.hash.HashCode;
Import com.google.common.hash.HashFunction;
Import Com.google.common.hash.Hasher;
Import com.google.common.hash.Hashing;
Import Com.google.common.hash.PrimitiveSink;
Import Com.google.common.io.Files;

Import Com.google.gson.Gson;
    public class Md5util {private static hashfunction HF = HASHING.MD5 ();

    private static Charset Defaultcharset = Charset.forname ("UTF-8");
    Private Md5util () {throw new Assertionerror ("Do not instantiate tool class Oh"); public static string MD5 (string data) {hashcode hash = Hf.newhasher (). putstring (data, defaultcharset). Hash
        ();
    return hash.tostring (); public static string MD5 (string data, Charset Charset, Boolean isuppercase) {hashcode hash =Hf.newhasher (). putstring (data, CharSet = null? defaultcharset:charset). hash (); Return isuppercase?
    Hash.tostring (). toUpperCase (): hash.tostring (); public static String md5 (byte[] bytes, Boolean isuppercase) {hashcode hash = Hf.newhasher (). Putbytes (bytes
        ). hash (); Return isuppercase?
    Hash.tostring (). toUpperCase (): hash.tostring (); public static String MD5 (File sourcefile, Boolean isuppercase) {hashcode hash = Hf.newhasher (). Putobject (S

            Ourcefile, New funnel<file> () {private static final long serialversionuid = 2757585325527511209L;
                    @Override public void Funnel (File from, primitivesink into) {try {
                Into.putbytes (Files.tobytearray (from));
                catch (IOException e) {throw new RuntimeException (e);
        }}). hash (); Return isuppercase? Hash.tostring (). toUpperCase (): hash.toString (); /** * Converts it to JSON, then MD5 * * @param the Object data source can be any type of object * @param isuppercase results are capitalized * @par Am CharSet the code of operations involving strings, by default Utf-8 * @return/public static string MD5 (Object object, Boolean isuppercase, C
        Harset charset) {Hasher hasher = Hf.newhasher ();
        Gson Gson = new Gson ();

        String JSON = Gson.tojson (object);
        Hashcode hash = hasher.putstring (JSON, CharSet = null? defaultcharset:charset). hash (); Return isuppercase?
    Hash.tostring (). toUpperCase (): hash.tostring (); /** * @param object can only be an entity class that encapsulates data, not map,list, and so on * @param fieldnames the name of the field property that needs to participate in the MD5 calculation, if the property is also an enclosure to the data entity class. Back up. The specific property name can be. For example: Role.level.id * @param isuppercase result is capitalized * @param charset the operation code when the string is involved, the default is Utf-8 * @return */PU Blic static String MD5 (Object object, List<string> FieldNames, Boolean isuppercase, Charset Charset) {HASHC Ode hash = Hf.newhasher (). Putobject (object, New FunneL<object> () {private static final long serialversionuid = -5236251432355557848l; @Override public void Funnel (Object from, primitivesink into) {map<string, field> ALLF

                Ield = Getallfield (object); for (String fieldname:fieldnames) {try {if (Fieldname.contains ("."))
                        {Handledeepfield (object, CharSet, into, Allfield, fieldName);
                        else {Handlefield (object, CharSet, into, Allfield, fieldName);
                    The catch (Exception e) {throw new RuntimeException (e);
        }}}). hash (); Return isuppercase?
    Hash.tostring (). toUpperCase (): hash.tostring (); private static void Handledeepfield (Object tempvalue, Charset Charset, primitivesink into, map<string, field> Tempallfield, String fieldName) throws Nosuchfieldexception, illegalaccessexception {fiel
        d field = null;

        string[] names = Fieldname.split ("\;");
            for (String name:names) {field = Tempallfield.get (name);
            if (field = = null) {throw new nosuchfieldexception (FieldName);
            } field.setaccessible (True);
            Tempvalue = Field.get (Tempvalue);
            Field.setaccessible (FALSE);
        Tempallfield = Getallfield (Tempvalue);
    } stufffieldvalue (Tempvalue, CharSet, into); private static void Handlefield (Object object, Charset Charset, primitivesink into, map<string, field> allfie LD, String fieldName) throws Nosuchfieldexception, illegalaccessexception {Field field = Allfield.get
        (FieldName);
        if (field = = null) {throw new nosuchfieldexception (FieldName); } field.setaccessible (True);
        Object Tempvalue = Field.get (object);
        Stufffieldvalue (Tempvalue, CharSet, into);
    Field.setaccessible (FALSE); private static void Stufffieldvalue (Object value, Charset Charset, Primitivesink into) throws Illegalaccessexceptio
        n {if (value instanceof Integer) {into.putint ((int) value);
        else if (value instanceof long) {Into.putlong (long) value);
        else if (value instanceof float) {into.putfloat ((float) value);
        else if (value instanceof Double) {into.putdouble (double) value);
        else if (value instanceof short) {Into.putshort (short) value);
        else if (value instanceof byte) {into.putbyte ((byte) value);
        else if (value instanceof Boolean) {Into.putboolean ((Boolean) value);
        else if (value instanceof byte) {into.putbyte ((byte) value); else if (value instanceof Character) {Into.putchar ((char) value); else if (value instanceof String) {into.putstring (string) value, CharSet = = null? defaultcharset:charse
        T);
        else {throw new IllegalArgumentException (Value.getclass () + "is not basic data type"); }} private static map<string, Field> Getallfield (Object object) {map<string, field> FIELDMA

        p = Maps.newhashmap ();
        if (Object.getclass (). GetName (). Equals (Object.class.getName ())) {return fieldmap;
        } class<?> Tempclass = Object.getclass ();
        field[] Declaredfields = null;
            while (true) {declaredfields = Tempclass.getdeclaredfields ();
            for (field Field:declaredfields) {Fieldmap.put (Field.getname (), field);

            } Tempclass = Tempclass.getsuperclass ();
     if (Tempclass.getname (). Equals (Object.class.getName ())) {break;       } return Fieldmap;
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.