Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
2 | /** | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 3 | * Copyright (C) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
5 | */ | ||||
6 | |||||
7 | #ifndef NDN_SIGNATURE_HPP | ||||
8 | #define NDN_SIGNATURE_HPP | ||||
9 | |||||
10 | namespace ndn { | ||||
11 | |||||
12 | /** | ||||
13 | * A Signature is storage for the signature-related information (info and value) in a Data packet. | ||||
14 | */ | ||||
15 | class Signature { | ||||
16 | public: | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 17 | class Error : public std::runtime_error |
18 | { | ||||
19 | public: | ||||
20 | explicit | ||||
21 | Error(const std::string& what) | ||||
22 | : std::runtime_error(what) | ||||
23 | { | ||||
24 | } | ||||
25 | }; | ||||
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 27 | enum { |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 28 | Sha256 = 0, |
29 | Sha256WithRsa = 1 | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 30 | }; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 31 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 32 | Signature() |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 33 | : m_type(-1) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 34 | { |
35 | } | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 36 | |
37 | Signature(const Block& info, const Block& value = Block()) | ||||
38 | : m_value(value) | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 39 | { |
Alexander Afanasyev | 31b2adf | 2014-01-03 15:31:16 -0800 | [diff] [blame] | 40 | setInfo(info); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 41 | } |
42 | |||||
43 | operator bool() const | ||||
44 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 45 | return m_type != -1; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 46 | } |
47 | |||||
48 | uint32_t | ||||
49 | getType() const | ||||
50 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 51 | return m_type; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 52 | } |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 53 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 54 | const Block& |
55 | getInfo() const | ||||
56 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 57 | m_info.encode(); // will do nothing if wire already exists |
58 | return m_info; | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 59 | } |
60 | |||||
61 | void | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 62 | setInfo(const Block& info) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 63 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 64 | m_info = info; |
65 | if (m_info.hasWire() || m_info.hasValue()) | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 66 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 67 | m_info.parse(); |
68 | const Block& signatureType = m_info.get(Tlv::SignatureType); | ||||
69 | m_type = readNonNegativeInteger(signatureType); | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 70 | } |
71 | else | ||||
72 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 73 | m_type = -1; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 74 | } |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 75 | } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 76 | |
77 | const Block& | ||||
78 | getValue() const | ||||
79 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 80 | m_value.encode(); // will do nothing if wire already exists |
81 | return m_value; | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 82 | } |
83 | |||||
84 | void | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 85 | setValue(const Block& value) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 86 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 87 | m_value = value; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 88 | } |
89 | |||||
90 | void | ||||
91 | reset() | ||||
92 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 93 | m_type = -1; |
94 | m_info.reset(); | ||||
95 | m_value.reset(); | ||||
96 | } | ||||
97 | |||||
98 | public: // EqualityComparable concept | ||||
99 | bool | ||||
100 | operator==(const Signature& other) const | ||||
101 | { | ||||
102 | return getInfo() == other.getInfo() && | ||||
103 | getValue() == other.getValue(); | ||||
104 | } | ||||
105 | |||||
106 | bool | ||||
107 | operator!=(const Signature& other) const | ||||
108 | { | ||||
109 | return !(*this == other); | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 110 | } |
111 | |||||
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 112 | protected: |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame^] | 113 | int32_t m_type; |
114 | |||||
115 | mutable Block m_info; | ||||
116 | mutable Block m_value; | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 117 | }; |
118 | |||||
119 | } // namespace ndn | ||||
120 | |||||
121 | #endif // NDN_SIGNATURE_HPP |