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 | |
| 14 | |
| 15 | Signature.prototype.decode =function( decoder) { |
| 16 | decoder.readStartElement(this.getElementLabel()); |
| 17 | if (decoder.peekStartElement(CCNProtocolDTags.DigestAlgorithm)) { |
| 18 | this.DigestAlgorithm = decoder.readUTF8Element(CCNProtocolDTags.DigestAlgorithm); |
| 19 | } |
| 20 | if (decoder.peekStartElement(CCNProtocolDTags.Witness)) { |
| 21 | this.Witness = decoder.readBinaryElement(CCNProtocolDTags.Witness); |
| 22 | } |
| 23 | this.Signature = decoder.readBinaryElement(CCNProtocolDTags.SignatureBits); |
| 24 | decoder.readEndElement(); |
| 25 | |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | Signature.prototype.encode= function( encoder){ |
| 30 | |
| 31 | if (!this.validate()) { |
| 32 | throw new Exception("Cannot encode: field values missing."); |
| 33 | } |
| 34 | |
| 35 | encoder.writeStartElement(this.getElementLabel()); |
| 36 | |
| 37 | if ((null != this.DigestAlgorithm) && (!this.DigestAlgorithm.equals(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM))) { |
| 38 | encoder.writeElement(CCNProtocolDTags.DigestAlgorithm, OIDLookup.getDigestOID(this.DigestAlgorithm)); |
| 39 | } |
| 40 | |
| 41 | if (null != this.Witness) { |
| 42 | // needs to handle null witness |
| 43 | encoder.writeElement(CCNProtocolDTags.Witness, this.Witness); |
| 44 | } |
| 45 | |
| 46 | encoder.writeElement(CCNProtocolDTags.SignatureBits, this.Signature); |
| 47 | |
| 48 | encoder.writeEndElement(); |
| 49 | }; |
| 50 | |
| 51 | Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; }; |
| 52 | |
| 53 | |
| 54 | Signature.prototype.validate = function() { |
| 55 | return null != this.Signature; |
| 56 | }; |
| 57 | |