Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 11 | namespace ndn { |
| 12 | |
| 13 | /** |
| 14 | * A Signature is storage for the signature-related information (info and value) in a Data packet. |
| 15 | */ |
| 16 | class Signature { |
| 17 | public: |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 18 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 19 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 20 | enum { |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 21 | Sha256 = 0, |
| 22 | Sha256WithRsa = 1 |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | Signature() |
| 26 | : type_(-1) |
| 27 | { |
| 28 | } |
| 29 | |
Alexander Afanasyev | 49b0703 | 2013-12-28 23:13:21 -0800 | [diff] [blame] | 30 | Signature(const Block &info, const Block &value = Block()) |
Alexander Afanasyev | 31b2adf | 2014-01-03 15:31:16 -0800 | [diff] [blame] | 31 | : value_(value) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 31b2adf | 2014-01-03 15:31:16 -0800 | [diff] [blame] | 33 | setInfo(info); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 34 | } |
| 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 Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 50 | info_.encode(); // will do nothing if wire already exists |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 51 | 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 Afanasyev | 31b2adf | 2014-01-03 15:31:16 -0800 | [diff] [blame] | 62 | type_ = readNonNegativeInteger(signatureType); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 63 | } |
| 64 | else |
| 65 | { |
| 66 | type_ = -1; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | const Block& |
| 71 | getValue() const |
| 72 | { |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 73 | value_.encode(); // will do nothing if wire already exists |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 74 | 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 Afanasyev | 31b2adf | 2014-01-03 15:31:16 -0800 | [diff] [blame] | 87 | info_.reset(); |
| 88 | value_.reset(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 91 | protected: |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 92 | int32_t type_; |
| 93 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 94 | mutable Block info_; |
| 95 | mutable Block value_; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace ndn |
| 99 | |
| 100 | #endif // NDN_SIGNATURE_HPP |