package com.parc.ccn.security.crypto;

import javax.xml.stream.XMLStreamException;

import com.parc.ccn.Library;
import com.parc.ccn.data.util.XMLEncodable;
import com.parc.security.crypto.DigestHelper;

public class CCNDigestHelper extends DigestHelper {

	public static String DEFAULT_DIGEST_ALGORITHM = "SHA-256";
	// public static String DEFAULT_DIGEST_ALGORITHM = "SHA-1";
	public static int DEFAULT_DIGEST_LENGTH = 64;
	
	/**
	 * Same digest preparation algorithm as ContentObject.
	 * @throws XMLStreamException 
	 */
	public static byte [] digestLeaf(
			String digestAlgorithm,
			XMLEncodable [] toBeSigneds,
			byte [][] additionalToBeSigneds) throws XMLStreamException {
		
		if (null == toBeSigneds) {
			Library.logger().info("Value to be signed must not be null.");
			throw new XMLStreamException("Unexpected null content in digestLeaf!");
		}
		byte [][] encodedData = new byte [toBeSigneds.length + ((null != additionalToBeSigneds) ? additionalToBeSigneds.length : 0)][];
		for (int i=0; i < toBeSigneds.length; ++i) {
			encodedData[i] = toBeSigneds[i].encode();
		}
		if (null != additionalToBeSigneds) {
			for (int i=0,j=toBeSigneds.length; j < encodedData.length; ++i,++j) {
				encodedData[j] = additionalToBeSigneds[i];
			}
		}
		return DigestHelper.digest(digestAlgorithm, 
									encodedData);
	}
	
}

@@ -30,19 +30,17 @@
 .crypto;
-%0D%0A%0D
+%0A
 %0Aimport 
@@ -75,19 +75,17 @@
 ception;
-%0D%0A%0D
+%0A
 %0Aimport 
@@ -105,17 +105,16 @@
 Library;
-%0D
 %0Aimport 
@@ -149,17 +149,16 @@
 codable;
-%0D
 %0Aimport 
@@ -195,19 +195,17 @@
 tHelper;
-%0D%0A%0D
+%0A
 %0Apublic 
@@ -252,11 +252,9 @@
 er %7B
-%0D%0A%0D
+%0A
 %0A%09pu
@@ -309,17 +309,16 @@
 HA-256%22;
-%0D
 %0A%09// pub
@@ -370,17 +370,16 @@
 %22SHA-1%22;
-%0D
 %0A%09public
@@ -421,18 +421,15 @@
  64;
-%0D
 %0A%09
-%0D
 %0A%09/**
-%0D
 %0A%09 *
@@ -480,17 +480,16 @@
 tObject.
-%0D
 %0A%09 * @th
@@ -516,15 +516,13 @@
 ion 
-%0D
 %0A%09 */
-%0D
 %0A%09pu
@@ -552,17 +552,16 @@
 estLeaf(
-%0D
 %0A%09%09%09Stri
@@ -579,17 +579,16 @@
 gorithm,
-%0D
 %0A%09%09%09XMLE
@@ -611,17 +611,16 @@
 Signeds,
-%0D
 %0A%09%09%09byte
@@ -679,13 +679,11 @@
 on %7B
-%0D
 %0A%09%09
-%0D
 %0A%09%09i
@@ -703,25 +703,24 @@
 BeSigneds) %7B
-%0D
 %0A%09%09%09Library.
@@ -773,17 +773,16 @@
 null.%22);
-%0D
 %0A%09%09%09thro
@@ -848,22 +848,20 @@
 Leaf!%22);
-%0D
 %0A%09%09%7D
-%0D
 %0A%09%09byte 
@@ -982,17 +982,16 @@
 : 0)%5D%5B%5D;
-%0D
 %0A%09%09for (
@@ -1029,17 +1029,16 @@
 ; ++i) %7B
-%0D
 %0A%09%09%09enco
@@ -1074,22 +1074,20 @@
 ncode();
-%0D
 %0A%09%09%7D
-%0D
 %0A%09%09if (n
@@ -1117,17 +1117,16 @@
 gneds) %7B
-%0D
 %0A%09%09%09for 
@@ -1190,17 +1190,16 @@
 i,++j) %7B
-%0D
 %0A%09%09%09%09enc
@@ -1241,20 +1241,17 @@
 %5Bi%5D;
-%0D
 %0A%09%09%09%7D
-%0D
 %0A%09%09%7D
-%0D
 %0A%09%09r
@@ -1276,16 +1276,128 @@
 .digest(
+((null == digestAlgorithm) %7C%7C (digestAlgorithm.length() == 0)) ?%0A%09%09%09%09CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM : 
 digestAl
@@ -1405,17 +1405,16 @@
 orithm, 
-%0D
 %0A%09%09%09%09%09%09%09
@@ -1432,16 +1432,12 @@
 ta);
-%0D
 %0A%09%7D
-%0D
 %0A%09
-%0D
 %0A%7D
-%0D
 %0A

Use CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM explicitly if none provided rather than relying on DigestHelper.DEFAULT_DIGEST_ALGORITHM to be the same