Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [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: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
| 9 | #ifndef NDN_PUBLIC_KEY_HPP |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 10 | #define NDN_PUBLIC_KEY_HPP |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 11 | |
| 12 | #include "../../util/blob.hpp" |
Jeff Thompson | 31b5c2f | 2013-10-12 15:13:16 -0700 | [diff] [blame] | 13 | #include "../../encoding/oid.hpp" |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 14 | #include "../security-common.hpp" |
| 15 | |
| 16 | namespace ndn { |
| 17 | |
Jeff Thompson | 0799671 | 2013-10-17 17:31:40 -0700 | [diff] [blame] | 18 | namespace der { class DerNode; } |
| 19 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 20 | class PublicKey { |
| 21 | public: |
| 22 | /** |
| 23 | * The default constructor. |
| 24 | */ |
| 25 | PublicKey() {} |
| 26 | |
| 27 | /** |
Jeff Thompson | 10a8c38 | 2013-12-13 11:53:43 -0800 | [diff] [blame] | 28 | * Create a new PublicKey with the given values. |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 29 | * @param algorithm The algorithm of the public key. |
| 30 | * @param keyDer The blob of the PublicKeyInfo in terms of DER. |
| 31 | */ |
| 32 | PublicKey(const OID& algorithm, const Blob& keyDer) |
| 33 | : algorithm_(algorithm), keyDer_(keyDer) |
| 34 | { |
| 35 | } |
| 36 | |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Encode the public key into DER. |
| 39 | * @return the encoded DER syntax tree. |
| 40 | */ |
Jeff Thompson | 0799671 | 2013-10-17 17:31:40 -0700 | [diff] [blame] | 41 | ptr_lib::shared_ptr<der::DerNode> |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 42 | toDer(); |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Decode the public key from DER blob. |
| 46 | * @param keyDer The DER blob. |
| 47 | * @return The decoded public key. |
| 48 | */ |
| 49 | static ptr_lib::shared_ptr<PublicKey> |
| 50 | fromDer(const Blob& keyDer); |
| 51 | |
| 52 | /* |
Jeff Thompson | 10a8c38 | 2013-12-13 11:53:43 -0800 | [diff] [blame] | 53 | * Get the digest of the public key. |
| 54 | * @param digestAlgorithm The digest algorithm. If omitted, use DIGEST_ALGORITHM_SHA256 by default. |
Jeff Thompson | c057343 | 2013-09-19 17:41:36 -0700 | [diff] [blame] | 55 | */ |
| 56 | Blob |
| 57 | getDigest(DigestAlgorithm digestAlgorithm = DIGEST_ALGORITHM_SHA256) const; |
| 58 | |
| 59 | /* |
| 60 | * Get the raw bytes of the public key in DER format. |
| 61 | */ |
| 62 | const Blob& |
| 63 | getKeyDer() const { return keyDer_; } |
| 64 | |
| 65 | private: |
| 66 | OID algorithm_; /**< Algorithm */ |
| 67 | Blob keyDer_; /**< PublicKeyInfo in DER */ |
| 68 | }; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | #endif |