blob: 9dcbbc5df004c0e0ecb052f9ad8564d0bf640b08 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents Signature Objects
4 */
5
6
7var Signature = function Signature(_Witness,_Signature,_DigestAlgorithm) {
8
9 this.Witness = _Witness;//byte [] _witness;
10 this.Signature = _Signature;//byte [] _signature;
11 this.DigestAlgorithm = _DigestAlgorithm//String _digestAlgorithm;
12};
13
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070014var generateSignature = function(contentName,content,signedinfo){
15
16 var enc = new BinaryXMLEncoder();
17 contentName.encode(enc);
18 var hex1 = toHex(enc.getReducedOstream());
19
20 var enc = new BinaryXMLEncoder();
21 content.encode(enc);
22 var hex2 = toHex(enc.getReducedOstream());
23
24 var enc = new BinaryXMLEncoder();
25 signedinfo.encode(enc);
26 var hex3 = toHex(enc.getReducedOstream());
27
28 var hex = hex1+hex2+hex3;
29
30 //globalKeyManager.sig
31
32};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070033
34Signature.prototype.decode =function( decoder) {
35 decoder.readStartElement(this.getElementLabel());
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070036
37 if(LOG>4)console.log('STARTED DECODING SIGNATURE ');
38
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070039 if (decoder.peekStartElement(CCNProtocolDTags.DigestAlgorithm)) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070040
41 if(LOG>4)console.log('DIGIEST ALGORITHM FOUND');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070042 this.DigestAlgorithm = decoder.readUTF8Element(CCNProtocolDTags.DigestAlgorithm);
43 }
44 if (decoder.peekStartElement(CCNProtocolDTags.Witness)) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070045 if(LOG>4)console.log('WITNESS FOUND FOUND');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070046 this.Witness = decoder.readBinaryElement(CCNProtocolDTags.Witness);
47 }
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070048
49 //FORCE TO READ A SIGNATURE
50
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070051 //if(LOG>4)console.log('SIGNATURE FOUND ');
52 this.Signature = decoder.readBinaryElement(CCNProtocolDTags.SignatureBits);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070053 if(LOG>4)console.log('READ SIGNATURE ');
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070054
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070055 decoder.readEndElement();
56
57};
58
59
60Signature.prototype.encode= function( encoder){
61
62 if (!this.validate()) {
63 throw new Exception("Cannot encode: field values missing.");
64 }
65
66 encoder.writeStartElement(this.getElementLabel());
67
68 if ((null != this.DigestAlgorithm) && (!this.DigestAlgorithm.equals(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM))) {
69 encoder.writeElement(CCNProtocolDTags.DigestAlgorithm, OIDLookup.getDigestOID(this.DigestAlgorithm));
70 }
71
72 if (null != this.Witness) {
73 // needs to handle null witness
74 encoder.writeElement(CCNProtocolDTags.Witness, this.Witness);
75 }
76
77 encoder.writeElement(CCNProtocolDTags.SignatureBits, this.Signature);
78
79 encoder.writeEndElement();
80};
81
82Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; };
83
84
85Signature.prototype.validate = function() {
86 return null != this.Signature;
87};
88