blob: 5f83ea8ee09f539b49534876f32ac1ddd098e47c [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson146d7de2012-11-17 16:15:28 -08002 * @author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07004 * This class represents PublisherPublicKeyDigest Objects
5 */
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07006
7/**
8 * @constructor
9 */
10var PublisherPublicKeyDigest = function PublisherPublicKeyDigest(pkd){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070011
Meki Cherkaoui8f173612012-06-06 01:05:40 -070012 //this.PUBLISHER_ID_LEN = 256/8;
13 this.PUBLISHER_ID_LEN = 512/8;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070014
Meki Cherkaoui8f173612012-06-06 01:05:40 -070015
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070016 this.publisherPublicKeyDigest = pkd;
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070017 //if( typeof _pkd == "object") this.publisherPublicKeyDigest = _pkd; // Byte Array
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070018 //else if( typeof _pkd == "PublicKey") ;//TODO...
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070019
20};
21
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070022PublisherPublicKeyDigest.prototype.from_ccnb = function( decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070023
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070024 this.publisherPublicKeyDigest = decoder.readBinaryElement(this.getElementLabel());
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070025
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070026 if(LOG>4)console.log('Publisher public key digest is ' + this.publisherPublicKeyDigest);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070027
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070028 if (null == this.publisherPublicKeyDigest) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070029 throw new Error("Cannot parse publisher key digest.");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070030 }
31
32 //TODO check if the length of the PublisherPublicKeyDigest is correct ( Security reason)
33
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070034 if (this.publisherPublicKeyDigest.length != this.PUBLISHER_ID_LEN) {
Jeff Thompsoncf187ff2012-10-21 16:18:55 -070035 if (LOG > 0)
36 console.log('LENGTH OF PUBLISHER ID IS WRONG! Expected ' + this.PUBLISHER_ID_LEN + ", got " + this.publisherPublicKeyDigest.length);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070037
Jeff Thompsoncf187ff2012-10-21 16:18:55 -070038 //this.publisherPublicKeyDigest = new PublisherPublicKeyDigest(this.PublisherPublicKeyDigest).PublisherKeyDigest;
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070039 }
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070040 };
41
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070042PublisherPublicKeyDigest.prototype.to_ccnb= function( encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070043 //TODO Check that the ByteArray for the key is present
Meki Cherkaoui8f173612012-06-06 01:05:40 -070044 if (!this.validate()) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070045 throw new Error("Cannot encode : field values missing.");
Meki Cherkaoui8f173612012-06-06 01:05:40 -070046 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070047 if(LOG>3) console.log('PUBLISHER KEY DIGEST IS'+this.publisherPublicKeyDigest);
48 encoder.writeElement(this.getElementLabel(), this.publisherPublicKeyDigest);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070049};
50
51PublisherPublicKeyDigest.prototype.getElementLabel = function() { return CCNProtocolDTags.PublisherPublicKeyDigest; };
52
53PublisherPublicKeyDigest.prototype.validate =function() {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070054 return (null != this.publisherPublicKeyDigest);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070055};