Meki Cherkaoui | b0365a7 | 2012-02-18 00:59:57 -0800 | [diff] [blame] | 1 | var CCNProtocolDTags = require('./CCNProtocolDTags').CCNProtocolDTags; |
| 2 | |
| 3 | |
| 4 | var Signature = function Signature(_Witness,_Signature,_DigestAlgorithm) { |
| 5 | |
| 6 | this.Witness = _Witness;//byte [] _witness; |
| 7 | this.Signature = _Signature;//byte [] _signature; |
| 8 | this.DigestAlgorithm = _DigestAlgorithm//String _digestAlgorithm; |
| 9 | }; |
| 10 | |
| 11 | exports.Signature = Signature; |
| 12 | |
| 13 | |
| 14 | Signature.prototype.decode =function( decoder) { |
| 15 | decoder.readStartElement(this.getElementLabel()); |
| 16 | if (decoder.peekStartElement(CCNProtocolDTags.DigestAlgorithm)) { |
| 17 | this.DigestAlgorithm = decoder.readUTF8Element(CCNProtocolDTags.DigestAlgorithm); |
| 18 | } |
| 19 | if (decoder.peekStartElement(CCNProtocolDTags.Witness)) { |
| 20 | this.Witness = decoder.readBinaryElement(CCNProtocolDTags.Witness); |
| 21 | } |
| 22 | this.Signature = decoder.readBinaryElement(CCNProtocolDTags.SignatureBits); |
| 23 | decoder.readEndElement(); |
| 24 | |
| 25 | }; |
| 26 | |
| 27 | |
| 28 | exports.Signature = Signature; |
| 29 | |
| 30 | Signature.prototype.encode= function( encoder){ |
| 31 | |
| 32 | if (!this.validate()) { |
| 33 | throw new Exception("Cannot encode: field values missing."); |
| 34 | } |
| 35 | |
| 36 | encoder.writeStartElement(this.getElementLabel()); |
| 37 | |
| 38 | if ((null != this.DigestAlgorithm) && (!this.DigestAlgorithm.equals(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM))) { |
| 39 | encoder.writeElement(CCNProtocolDTags.DigestAlgorithm, OIDLookup.getDigestOID(this.DigestAlgorithm)); |
| 40 | } |
| 41 | |
| 42 | if (null != this.Witness) { |
| 43 | // needs to handle null witness |
| 44 | encoder.writeElement(CCNProtocolDTags.Witness, this.Witness); |
| 45 | } |
| 46 | |
| 47 | encoder.writeElement(CCNProtocolDTags.SignatureBits, this.Signature); |
| 48 | |
| 49 | encoder.writeEndElement(); |
| 50 | }; |
| 51 | |
| 52 | Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; }; |
| 53 | |
| 54 | |
| 55 | Signature.prototype.validate = function() { |
| 56 | return null != this.Signature; |
| 57 | }; |
| 58 | |