All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.MD5Hex Maven / Gradle / Ivy

The newest version!
package com.databasesandlife.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.bind.DatatypeConverter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

/**
 * For strings, UTF-8 bytes are used.
 * 
 * @author This source is copyright Adrian Smith and licensed under the LGPL 3.
 * @see Project on GitHub
 */
public class MD5Hex {

    protected static String bytesToHex(byte[] bytes) {
        var hexString = new StringBuilder();
        for (byte aByte : bytes) {
            var x = "0" + Integer.toHexString(0xFF & aByte);
            hexString.append(x.substring(x.length() - 2));
        }
        return hexString.toString();
    }

    public static String md5(byte[] stuff) {
        try {
            var algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(stuff);
            byte[] messageDigest = algorithm.digest();
            return bytesToHex(messageDigest);
        }
        catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); }
    }

    public static String md5(String stuff) {
        return md5(stuff.getBytes(StandardCharsets.UTF_8));
    }

    public static String md5(CharSequence stuff) {
        return md5(stuff.toString());
    }

    /** @param stuff client must close this input stream */
    public static String md5(InputStream stuff) throws IOException {
        try {
            var digest = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[8192];
            var read = 0;
            while( (read = stuff.read(buffer)) > 0) digest.update(buffer, 0, read);
            return bytesToHex(digest.digest());        
        }
        catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); }
    }

    public static String md5(File stuff) {
       try {
           try (var str = new BufferedInputStream(new FileInputStream(stuff))) {
               return MD5Hex.md5(str);
           }
        }
        catch (IOException e) { throw new RuntimeException(e); }
    }

    public static String md5(Document xslt) {
        try {
            var str = new StringWriter();        
            var transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty("omit-xml-declaration","yes");
            transformer.transform(new DOMSource(xslt), new StreamResult(str));
            return md5(str.toString());
        }
        catch (TransformerException e) { throw new RuntimeException(e); }
    }

    public static String sha256hex(String stuff) {
        try {
            var md = MessageDigest.getInstance("SHA-256");
            md.update(stuff.getBytes(StandardCharsets.UTF_8));
            return DatatypeConverter.printHexBinary(md.digest()).toLowerCase();
        }
        catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy