DiceKeys Seeded Cryptography Library
symmetric-key.hpp
1 #pragma once
2 
3 #include <string>
4 #include "sodium-buffer.hpp"
5 #include "packaged-sealed-message.hpp"
6 
39 class SymmetricKey {
40 
41  public:
50  const std::string recipe;
51 
56  const SodiumBuffer& keyBytes,
57  std::string recipe
58  );
59 
64  const SymmetricKey &other
65  );
66 
67  // /**
68  // * @brief Construct (reconstitute) a SymmetricKey from its JSON
69  // * representation
70  // *
71  // * @param symmetricKeyAsJson A SymmetricKey serialized in JSON format
72  // * via a previous call to toJson
73  // */
74  // SymmetricKey(
75  // const std::string& symmetricKeyAsJson
76  // );
77 
89  const std::string& seedString,
90  const std::string& recipe
91  );
92 
104  const std::string& seedString,
105  const std::string& recipe
106  );
107 
121  const std::vector<unsigned char> sealToCiphertextOnly(
122  const unsigned char* message,
123  const size_t messageLength,
124  const std::string& unsealingInstructions = {}
125  ) const;
126 
140  const std::vector<unsigned char> sealToCiphertextOnly(
141  const SodiumBuffer& message,
142  const std::string& unsealingInstructions = {}
143  ) const;
144 
158  const SodiumBuffer& message,
159  const std::string& unsealingInstructions = {}
160  ) const;
161 
175  const std::string& message,
176  const std::string& unsealingInstructions = {}
177  ) const;
178 
192  const std::vector<unsigned char>& message,
193  const std::string& unsealingInstructions = {}
194  ) const;
195 
196 
211  const unsigned char* message,
212  const size_t messageLength,
213  const std::string& unsealingInstructions = {}
214  ) const;
215 
231  const SodiumBuffer unseal(
232  const unsigned char* ciphertext,
233  const size_t ciphertextLength,
234  const std::string& unsealingInstructions = {}
235  ) const;
236 
249  const SodiumBuffer unseal(
250  const std::vector<unsigned char> &ciphertext,
251  const std::string& unsealingInstructions = {}
252  ) const;
253 
260  const SodiumBuffer unseal(
261  const PackagedSealedMessage& packagedSealedMessage
262  ) const;
263 
272  static const SodiumBuffer unseal(
273  const PackagedSealedMessage &packagedSealedMessage,
274  const std::string& seedString
275  );
276 
277 
287  const std::string toJson(
288  int indent = -1,
289  const char indent_char = ' '
290  ) const;
291 
292 
300  const SodiumBuffer toSerializedBinaryForm() const;
301 
309  static SymmetricKey fromSerializedBinaryForm(const SodiumBuffer &serializedBinaryForm);
310 
314  static SymmetricKey fromJson(
315  const std::string& symmetricKeyAsJson
316  );
317 
318 protected:
319 
324  const unsigned char* ciphertext,
325  const size_t ciphertextLength,
326  const std::string& unsealingInstructions = {}
327  ) const;
328 
329 };
SymmetricKey::unseal
const SodiumBuffer unseal(const unsigned char *ciphertext, const size_t ciphertextLength, const std::string &unsealingInstructions={}) const
Unseal a message.
Definition: symmetric-key.cpp:177
SodiumBuffer
A byte array containing a length and a pointer to memory (the data field), which ensures data is eras...
Definition: sodium-buffer.hpp:27
SymmetricKey::keyBytes
const SodiumBuffer keyBytes
The binary representation of the symmetric key.
Definition: symmetric-key.hpp:46
SymmetricKey::recipe
const std::string recipe
A JSON Format for Recipes string used to specify how this key is derived.
Definition: symmetric-key.hpp:50
SymmetricKey::toJson
const std::string toJson(int indent=-1, const char indent_char=' ') const
Serialize this object to a JSON-formatted string.
Definition: symmetric-key.cpp:226
SymmetricKey
A SymmetricKey can be used to seal and unseal messages. This SymmetricKey class can be (re) derived f...
Definition: symmetric-key.hpp:39
PackagedSealedMessage
When a message is sealed, the ciphertext is packaged with the recipe in JSON Format for Recipes,...
Definition: packaged-sealed-message.hpp:14
SymmetricKey::toSerializedBinaryForm
const SodiumBuffer toSerializedBinaryForm() const
Serialize to byte array as a list of: (keyBytes, recipe)
Definition: symmetric-key.cpp:239
SymmetricKey::seal
const PackagedSealedMessage seal(const SodiumBuffer &message, const std::string &unsealingInstructions={}) const
Seal a plaintext message.
Definition: symmetric-key.cpp:97
SymmetricKey::sealToCiphertextOnly
const std::vector< unsigned char > sealToCiphertextOnly(const unsigned char *message, const size_t messageLength, const std::string &unsealingInstructions={}) const
Seal a plaintext message.
Definition: symmetric-key.cpp:59
SymmetricKey::unsealMessageContents
const SodiumBuffer unsealMessageContents(const unsigned char *ciphertext, const size_t ciphertextLength, const std::string &unsealingInstructions={}) const
Internal implementation of unseal.
Definition: symmetric-key.cpp:140
SymmetricKey::fromSerializedBinaryForm
static SymmetricKey fromSerializedBinaryForm(const SodiumBuffer &serializedBinaryForm)
Deserialize from a byte array stored as a list of: (keyBytes, recipe)
Definition: symmetric-key.cpp:247
SymmetricKey::deriveFromSeed
static SymmetricKey deriveFromSeed(const std::string &seedString, const std::string &recipe)
Construct a new SymmetricKey by (re)deriving it from a seed string and a set of recipe in JSON Format...
Definition: symmetric-key.cpp:44
SymmetricKey::SymmetricKey
SymmetricKey(const SodiumBuffer &keyBytes, std::string recipe)
Construct a SymmetricKey from its members.
Definition: symmetric-key.cpp:26
SymmetricKey::fromJson
static SymmetricKey fromJson(const std::string &symmetricKeyAsJson)
Internal implementation of JSON parser for the JSON contructor.
Definition: symmetric-key.cpp:212