comparison artifacts/src/main/java/org/dive4elements/river/artifacts/D4EArtifact.java @ 8659:af415396d9ca

(issue1803) Use MD5 instead of a homegrown hashing algorithm For creating a digest of the parametrization we should use an algorithm that does not create collisions if there are small changes in the parametrization so that wrong results are returned.
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 02 Apr 2015 17:40:18 +0200
parents 7d1a32a543cb
children 5e38e2924c07
comparison
equal deleted inserted replaced
8658:3531f0cee5e1 8659:af415396d9ca
16 import java.util.List; 16 import java.util.List;
17 import java.util.Map; 17 import java.util.Map;
18 import java.util.Set; 18 import java.util.Set;
19 import java.util.TreeMap; 19 import java.util.TreeMap;
20 20
21 import java.security.MessageDigest;
22 import java.security.NoSuchAlgorithmException;
23 import java.io.ByteArrayOutputStream;
24 import java.io.ObjectOutputStream;
25 import java.io.IOException;
26
21 import javax.xml.xpath.XPathConstants; 27 import javax.xml.xpath.XPathConstants;
28 import javax.xml.bind.DatatypeConverter;
22 29
23 import net.sf.ehcache.Cache; 30 import net.sf.ehcache.Cache;
24 31
25 import org.apache.log4j.Logger; 32 import org.apache.log4j.Logger;
26 import org.w3c.dom.Document; 33 import org.w3c.dom.Document;
1133 * 1140 *
1134 * @return a hash code. 1141 * @return a hash code.
1135 */ 1142 */
1136 @Override 1143 @Override
1137 public String hash() { 1144 public String hash() {
1138 1145 try {
1139 long hash = 0L; 1146 ByteArrayOutputStream ba = new ByteArrayOutputStream();
1140 int shift = 3; 1147 ObjectOutputStream oa = new ObjectOutputStream(ba);
1141 1148 for (Map.Entry<String, StateData> entry: data.entrySet()) {
1142 for (Map.Entry<String, StateData> entry: data.entrySet()) { 1149 oa.writeObject(entry.getKey());
1143 String key = entry.getKey(); 1150 oa.writeObject(entry.getValue().getValue());
1144 Object value = entry.getValue().getValue(); 1151 }
1145 1152 MessageDigest md = MessageDigest.getInstance("MD5");
1146 hash ^= ((long)key.hashCode() << shift) 1153 byte[] md5sum = md.digest(ba.toByteArray());
1147 | ((long)value.hashCode() << (shift + 3)); 1154 return DatatypeConverter.printBase64Binary(md5sum);
1148 shift += 2; 1155 } catch (NoSuchAlgorithmException e) {
1149 } 1156 throw new RuntimeException("MD5 unavailable. Can't happen.");
1150 1157 } catch (IOException e) {
1151 return getCurrentStateId() + hash; 1158 throw new RuntimeException("Cant write parameter. Can't happen.");
1159 }
1152 } 1160 }
1153 1161
1154 1162
1155 /** 1163 /**
1156 * Return List of outputs, where combinations of outputname and filtername 1164 * Return List of outputs, where combinations of outputname and filtername
1507 Object old = null; 1515 Object old = null;
1508 1516
1509 if (cache != null) { 1517 if (cache != null) {
1510 net.sf.ehcache.Element element = cache.get(key); 1518 net.sf.ehcache.Element element = cache.get(key);
1511 if (element != null) { 1519 if (element != null) {
1512 log.debug("Got computation result from cache."); 1520 log.debug("Got computation result from cache for key: " + key);
1513 old = element.getValue(); 1521 old = element.getValue();
1514 } 1522 }
1515 } 1523 }
1516 else { 1524 else {
1517 log.debug("cache not configured."); 1525 log.debug("cache not configured.");

http://dive4elements.wald.intevation.org