If Name.getContentDigestValue() returns a content digest, then use Sha256 to digest the content and dump an error if not equal.
diff --git a/js/ndnProtocol/components/ndnProtocolService.js b/js/ndnProtocol/components/ndnProtocolService.js
index f3a8909..290f91a 100644
--- a/js/ndnProtocol/components/ndnProtocolService.js
+++ b/js/ndnProtocol/components/ndnProtocolService.js
@@ -111,6 +111,7 @@
this.firstReceivedSegmentNumber = null;
this.firstReceivedContentObject = null;
+ this.contentSha256 = null;
}
ContentClosure.prototype.upcall = function(kind, upcallInfo) {
@@ -151,8 +152,7 @@
var contentUriSpec;
if (!this.uriEndsWithSegmentNumber && endsWithSegmentNumber(contentObject.name)) {
var nameWithoutSegmentNumber = new Name
- (contentObject.name.components.slice
- (0, contentObject.name.components.length - 1));
+ (contentObject.name.components.slice(0, contentObject.name.components.length - 1));
contentUriSpec = "ndn:" + nameWithoutSegmentNumber.to_uri();
}
else
@@ -165,9 +165,15 @@
var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
this.contentListener.onStart(contentTypeEtc.contentType, contentTypeEtc.contentCharset,
ioService.newURI(contentUriSpec, this.uriOriginCharset, null));
+
+ if (contentObject.name.getContentDigestValue() != null)
+ // We need to compute the content digest.
+ this.contentSha256 = new Sha256();
}
this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
+ if (this.contentSha256 != null)
+ this.contentSha256.update(contentObject.content);
// Check for the special case if the saved content is for the next segment that we need.
if (this.firstReceivedContentObject != null &&
@@ -179,6 +185,8 @@
this.firstReceivedContentObject = null;
this.contentListener.onReceivedContent(DataUtils.toString(contentObject.content));
+ if (this.contentSha256 != null)
+ this.contentSha256.update(contentObject.content);
}
var finalSegmentNumber = null;
@@ -199,9 +207,17 @@
components.push(nextSegmentNumber);
this.ndn.expressInterest(new Name(components), this);
}
- else
+ else {
// Finished.
- this.contentListener.onStop();
+ this.contentListener.onStop();
+ if (this.contentSha256 != null) {
+ var nameContentDigest = contentObject.name.getContentDigestValue();
+ if (nameContentDigest != null &&
+ !DataUtils.arraysEqual(nameContentDigest, this.contentSha256.finalize()))
+ // TODO: How to show the user an error for invalid digest?
+ dump("Content does not match digest in name " + contentObject.name.to_uri());
+ }
+ }
return Closure.RESULT_OK;
};
@@ -278,4 +294,4 @@
return "";
else
return "?" + terms.join('&');
-}
\ No newline at end of file
+}