package org.icij.extract.document;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.metadata.TikaCoreProperties;

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class TikaDigestIdentifier implements Identifier {

	private final String key;
	private final String algorithm;

	TikaDigestIdentifier(final String algorithm) {
		key = TikaCoreProperties.TIKA_META_PREFIX + "digest" + Metadata.NAMESPACE_PREFIX_DELIMITER + algorithm
				.replace("-", "");
		this.algorithm = algorithm;
	}

	@Override
	public String generate(final Document document) {
		final String hash = document.getMetadata().get(key);

		if (null == hash) {
			throw new RuntimeException("Unexpected null hash. Check that the correct algorithm is specified.");
		}

		if (!(document instanceof EmbeddedDocument)) {
			return hash;
		}

		final MessageDigest digest;

		try {
			digest = MessageDigest.getInstance(algorithm);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}

		digest.update(((EmbeddedDocument) document).getParent().getId().getBytes(StandardCharsets.UTF_8));
		digest.update(hash.getBytes(StandardCharsets.UTF_8));

		return DatatypeConverter.printHexBinary(digest.digest()).toLowerCase(Locale.ROOT);
	}
}

@@ -122,16 +122,66 @@
 perties;
+%0Aimport org.apache.tika.metadata.TikaMetadataKeys;
 %0A%0Aimport
@@ -218,24 +218,31 @@
 ter;%0Aimport 
+static 
 java.nio.cha
@@ -262,16 +262,22 @@
 Charsets
+.UTF_8
 ;%0Aimport
@@ -356,24 +356,31 @@
 ion;%0Aimport 
+static 
 java.util.Lo
@@ -383,16 +383,24 @@
 l.Locale
+.ENGLISH
 ;%0A%0Apubli
@@ -981,10 +981,8 @@
 %09if 
-(!
 (doc
@@ -1015,17 +1015,16 @@
 ocument)
-)
  %7B%0A%09%09%09re
@@ -1031,20 +1031,170 @@
 turn
+ generateEmbedded((EmbeddedDocument) document,
  hash
+)
 ;%0A%09%09%7D%0A%0A
+%09%09return hash;%0A%09%7D%0A%0A%09private String generateEmbedded(final EmbeddedDocument embed, final String hash) %7B%0A
 %09%09fi
@@ -1364,32 +1364,19 @@
 %0A%0A%09%09
-digest.update(((
+// 
 Embedded
 Docu
@@ -1375,138 +1375,491 @@
 dded
-D
+ d
 ocument
-) document).getParent().getId().getBytes(StandardCharsets.UTF_8));%0A%09%09digest.update(hash.getBytes(StandardCharsets.
+s in different files or the same file could have the same hash. Add the parent path to the%0A%09%09// value to be digested, the embedded relationship ID, and the actual hash.%0A%09%09final String embeddedRelationshipId = embed.getMetadata().get(TikaMetadataKeys.EMBEDDED_RELATIONSHIP_ID);%0A%0A%09%09digest.update(hash.getBytes(UTF_8));%0A%09%09digest.update(embed.getParent().getId().getBytes(UTF_8));%0A%09%09if (null != embeddedRelationshipId) %7B%0A%09%09%09digest.update(embeddedRelationshipId.getBytes(
 UTF_8));
 %0A%0A%09%09
@@ -1854,16 +1854,20 @@
 UTF_8));
+%0A%09%09%7D
 %0A%0A%09%09retu
@@ -1935,19 +1935,15 @@
 ase(
-Locale.ROOT
+ENGLISH
 );%0A%09

Include embedded relationship ID in hash