blob: d4c3fa43d72d5bd678a867f0103b7ae92a9c766d [file] [log] [blame]
Meki Cherkaouib0365a72012-02-18 00:59:57 -08001var CCNProtocolDTags = require('./CCNProtocolDTags').CCNProtocolDTags;
2
3
4var 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
11exports.Signature = Signature;
12
13
14Signature.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
28exports.Signature = Signature;
29
30Signature.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
52Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; };
53
54
55Signature.prototype.validate = function() {
56 return null != this.Signature;
57};
58