blob: e38ad3e182a0678a89de921b1ea10b7e9589ffac [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
Alexander Afanasyev49b07032013-12-28 23:13:21 -080030 Signature(const Block &info, const Block &value = Block())
Alexander Afanasyev31b2adf2014-01-03 15:31:16 -080031 : value_(value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080032 {
Alexander Afanasyev31b2adf2014-01-03 15:31:16 -080033 setInfo(info);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080034 }
35
36 operator bool() const
37 {
38 return type_ != -1;
39 }
40
41 uint32_t
42 getType() const
43 {
44 return type_;
45 }
46
47 const Block&
48 getInfo() const
49 {
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080050 info_.encode(); // will do nothing if wire already exists
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080051 return info_;
52 }
53
54 void
55 setInfo(const Block &info)
56 {
57 info_ = info;
58 if (info_.hasWire() || info_.hasValue())
59 {
60 info_.parse();
61 const Block &signatureType = info_.get(Tlv::SignatureType);
Alexander Afanasyev31b2adf2014-01-03 15:31:16 -080062 type_ = readNonNegativeInteger(signatureType);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080063 }
64 else
65 {
66 type_ = -1;
67 }
68 }
69
70 const Block&
71 getValue() const
72 {
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080073 value_.encode(); // will do nothing if wire already exists
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080074 return value_;
75 }
76
77 void
78 setValue(const Block &value)
79 {
80 value_ = value;
81 }
82
83 void
84 reset()
85 {
86 type_ = -1;
Alexander Afanasyev31b2adf2014-01-03 15:31:16 -080087 info_.reset();
88 value_.reset();
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080089 }
90
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080091protected:
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080092 int32_t type_;
93
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080094 mutable Block info_;
95 mutable Block value_;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080096};
97
98} // namespace ndn
99
100#endif // NDN_SIGNATURE_HPP