blob: e45dbd6425e6355900ee9671708883469a5d8506 [file] [log] [blame]
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_SIGNATURE_HPP
9#define NDN_SIGNATURE_HPP
10
11namespace ndn {
12
13/**
14 * A Signature is storage for the signature-related information (info and value) in a Data packet.
15 */
16class Signature {
17public:
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080018 struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
19
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080020 enum {
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080021 Sha256 = 0,
22 Sha256WithRsa = 1
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080023 };
24
25 Signature()
26 : type_(-1)
27 {
28 }
29
30 Signature(const Block &info, const Block &value)
31 : info_(info)
32 , value_(value)
33 {
34 Buffer::const_iterator i = info_.value_begin();
35 Tlv::readVarNumber(i, info_.value_end());
36 size_t length = Tlv::readVarNumber(i, info_.value_end());
37 type_ = Tlv::readNonNegativeInteger(length, i, info_.value_end());
38 }
39
40 operator bool() const
41 {
42 return type_ != -1;
43 }
44
45 uint32_t
46 getType() const
47 {
48 return type_;
49 }
50
51 const Block&
52 getInfo() const
53 {
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080054 info_.encode(); // will do nothing if wire already exists
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080055 return info_;
56 }
57
58 void
59 setInfo(const Block &info)
60 {
61 info_ = info;
62 if (info_.hasWire() || info_.hasValue())
63 {
64 info_.parse();
65 const Block &signatureType = info_.get(Tlv::SignatureType);
66
67 Buffer::const_iterator i = signatureType.value_begin();
68 type_ = Tlv::readVarNumber(i, signatureType.value_end());
69 }
70 else
71 {
72 type_ = -1;
73 }
74 }
75
76 const Block&
77 getValue() const
78 {
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080079 value_.encode(); // will do nothing if wire already exists
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080080 return value_;
81 }
82
83 void
84 setValue(const Block &value)
85 {
86 value_ = value;
87 }
88
89 void
90 reset()
91 {
92 type_ = -1;
93 info_ = Block();
94 value_ = Block();
95 }
96
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080097protected:
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080098 int32_t type_;
99
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800100 mutable Block info_;
101 mutable Block value_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800102};
103
104} // namespace ndn
105
106#endif // NDN_SIGNATURE_HPP