
com.databasesandlife.util.MD5Hex Maven / Gradle / Ivy
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.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
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) {
StringBuffer hexString = new StringBuffer();
for (int i=0;i 0) digest.update(buffer, 0, read);
return bytesToHex(digest.digest());
}
catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); }
}
public static String md5(File stuff) {
try {
FileInputStream fileStr = new FileInputStream(stuff);
try {
BufferedInputStream str = new BufferedInputStream(fileStr);
try {
return MD5Hex.md5(str);
}
finally { str.close(); }
}
finally { fileStr.close(); }
}
catch (IOException e) { throw new RuntimeException(e); }
}
public static String md5(Document xslt) {
try {
StringWriter str = new StringWriter();
Transformer 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); }
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy