001: package de.schlund.pfixxml.util;
002:
003: import java.io.UnsupportedEncodingException;
004: import java.security.MessageDigest;
005: import java.security.NoSuchAlgorithmException;
006:
007: import org.apache.log4j.Logger;
008: import org.w3c.dom.Node;
009: import org.w3c.dom.NodeList;
010:
011: /*
012: * Created on 05.07.2004
013: */
014:
015: /**
016: * @author Niels Schelbach
017: * 05.07.2004
018: */
019: public class MD5Utils {
020:
021: private final static Logger logger = Logger
022: .getLogger(MD5Utils.class);
023:
024: public static final String CHARSET_UTF8 = "UTF-8";
025: public static final String CHARSET_LATIN1 = "ISO-8859-1";
026:
027: /*
028: * Convert an array of little-endian words to a hex string.
029: */
030: public static String byteToHex(byte[] raw) {
031: String hex_tab = "0123456789abcdef";
032: StringBuffer sb = new StringBuffer();
033: for (int i = 0; i < raw.length; i++) {
034: byte b = raw[i];
035: sb.append(hex_tab.charAt((b & 0xF0) >> 4));
036: sb.append(hex_tab.charAt(b & 0xF));
037: }
038: return sb.toString();
039: }
040:
041: public static String hex_md5(String message) {
042: return hex_md5(message, CHARSET_LATIN1);
043: }
044:
045: public static String hex_md5(String message, String charset) {
046: String result = "";
047: try {
048: MessageDigest md = MessageDigest.getInstance("MD5");
049: byte[] raw = md.digest(new String(message)
050: .getBytes(charset));
051: result = byteToHex(raw);
052: } catch (NoSuchAlgorithmException ex) {
053: logger.error("this should not happen!", ex);
054: throw new RuntimeException("No Such Algorithm", ex);
055: } catch (UnsupportedEncodingException ex) {
056: logger.error("this should not happen!", ex);
057: throw new RuntimeException("Unsupported Charset", ex);
058: }
059: return result;
060: }
061:
062: public static String hex_md5(Node node) {
063: String val = serializeNode(node);
064: return hex_md5(val, "UTF-8");
065: }
066:
067: private static String serializeNode(Node node) {
068: StringBuffer buffer = new StringBuffer();
069: String nodeType = null;
070: Short nodeTypeNum = node.getNodeType();
071: switch (nodeTypeNum) {
072: case Node.ATTRIBUTE_NODE:
073: nodeType = "ATTRIBUTE";
074: break;
075: case Node.CDATA_SECTION_NODE:
076: nodeType = "CDATA";
077: break;
078: case Node.COMMENT_NODE:
079: nodeType = "COMMENT";
080: break;
081: case Node.DOCUMENT_FRAGMENT_NODE:
082: nodeType = "FRAGMENT";
083: break;
084: case Node.DOCUMENT_NODE:
085: nodeType = "DOCUMENT";
086: break;
087: case Node.DOCUMENT_TYPE_NODE:
088: nodeType = "DOCUMENT_TYPE";
089: break;
090: case Node.ELEMENT_NODE:
091: nodeType = "ELEMENT";
092: break;
093: case Node.ENTITY_NODE:
094: nodeType = "ENTITY";
095: break;
096: case Node.ENTITY_REFERENCE_NODE:
097: nodeType = "ENTITY_REFERENCE";
098: break;
099: case Node.NOTATION_NODE:
100: nodeType = "NOTATION";
101: break;
102: case Node.PROCESSING_INSTRUCTION_NODE:
103: nodeType = "PROCESSING_INSTRUCTION";
104: break;
105: case Node.TEXT_NODE:
106: nodeType = "TEXT";
107: break;
108: default:
109: throw new RuntimeException("Unexpected node type!");
110: }
111: buffer.append(nodeType);
112: buffer.append('[');
113: String nodeNamespace = node.getNamespaceURI();
114: if (nodeNamespace != null) {
115: buffer.append("NAMESPACE[\"");
116: buffer.append(nodeNamespace);
117: buffer.append("\"]");
118: }
119: String nodeName = node.getNodeName();
120: if (nodeName != null) {
121: buffer.append("NAME[\"");
122: buffer.append(nodeName);
123: buffer.append("\"]");
124: }
125: String nodeValue = node.getNodeValue();
126: if (nodeValue != null) {
127: buffer.append("VALUE[\"");
128: buffer.append(nodeValue);
129: buffer.append("\"]");
130: }
131: NodeList children = node.getChildNodes();
132: for (int i = 0; i < children.getLength(); i++) {
133: buffer.append(serializeNode(children.item(i)));
134: }
135: buffer.append(']');
136: return buffer.toString();
137: }
138: }
|