blob: 8a20baab54992bfc2345d7bd69d0bdfb96f929a0 [file] [log] [blame]
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -07003 * Copyright (C) 2013-2014 Regents of the University of California.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -08004 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NDN_SIGNATURE_HPP
8#define NDN_SIGNATURE_HPP
9
10namespace ndn {
11
12/**
13 * A Signature is storage for the signature-related information (info and value) in a Data packet.
14 */
15class Signature {
16public:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070017 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 Afanasyevfa13f8e2014-01-03 15:19:07 -080026
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080027 enum {
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080028 Sha256 = 0,
29 Sha256WithRsa = 1
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080030 };
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070031
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080032 Signature()
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070033 : m_type(-1)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080034 {
35 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070036
37 Signature(const Block& info, const Block& value = Block())
38 : m_value(value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080039 {
Alexander Afanasyev31b2adf2014-01-03 15:31:16 -080040 setInfo(info);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080041 }
42
43 operator bool() const
44 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070045 return m_type != -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080046 }
47
48 uint32_t
49 getType() const
50 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070051 return m_type;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080052 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070053
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080054 const Block&
55 getInfo() const
56 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070057 m_info.encode(); // will do nothing if wire already exists
58 return m_info;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080059 }
60
61 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070062 setInfo(const Block& info)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080063 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070064 m_info = info;
65 if (m_info.hasWire() || m_info.hasValue())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080066 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070067 m_info.parse();
68 const Block& signatureType = m_info.get(Tlv::SignatureType);
69 m_type = readNonNegativeInteger(signatureType);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080070 }
71 else
72 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070073 m_type = -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080074 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070075 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080076
77 const Block&
78 getValue() const
79 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070080 m_value.encode(); // will do nothing if wire already exists
81 return m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080082 }
83
84 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070085 setValue(const Block& value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080086 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070087 m_value = value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080088 }
89
90 void
91 reset()
92 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070093 m_type = -1;
94 m_info.reset();
95 m_value.reset();
96 }
97
98public: // 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 Afanasyev2ba8f662014-01-05 22:53:18 -0800110 }
111
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800112protected:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700113 int32_t m_type;
114
115 mutable Block m_info;
116 mutable Block m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800117};
118
119} // namespace ndn
120
121#endif // NDN_SIGNATURE_HPP