Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 3 | * This class represents Signature Objects |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | var 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 Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 14 | var 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 Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 33 | |
| 34 | Signature.prototype.decode =function( decoder) { |
| 35 | decoder.readStartElement(this.getElementLabel()); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 36 | |
| 37 | if(LOG>4)console.log('STARTED DECODING SIGNATURE '); |
| 38 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 39 | if (decoder.peekStartElement(CCNProtocolDTags.DigestAlgorithm)) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 40 | |
| 41 | if(LOG>4)console.log('DIGIEST ALGORITHM FOUND'); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 42 | this.DigestAlgorithm = decoder.readUTF8Element(CCNProtocolDTags.DigestAlgorithm); |
| 43 | } |
| 44 | if (decoder.peekStartElement(CCNProtocolDTags.Witness)) { |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 45 | if(LOG>4)console.log('WITNESS FOUND FOUND'); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 46 | this.Witness = decoder.readBinaryElement(CCNProtocolDTags.Witness); |
| 47 | } |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 48 | |
| 49 | //FORCE TO READ A SIGNATURE |
| 50 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 51 | //if(LOG>4)console.log('SIGNATURE FOUND '); |
| 52 | this.Signature = decoder.readBinaryElement(CCNProtocolDTags.SignatureBits); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame] | 53 | if(LOG>4)console.log('READ SIGNATURE '); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 54 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 55 | decoder.readEndElement(); |
| 56 | |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | Signature.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 | |
| 82 | Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; }; |
| 83 | |
| 84 | |
| 85 | Signature.prototype.validate = function() { |
| 86 | return null != this.Signature; |
| 87 | }; |
| 88 | |