ptth/bare_minimum_crypto/cpp/sender.cpp

51 lines
980 B
C++
Raw Normal View History

2021-01-18 02:52:36 +00:00
#include "sender.h"
2021-01-18 22:57:33 +00:00
#include <stdexcept>
#include "json.hpp"
#include "expiring_signature.h"
2021-01-18 02:52:36 +00:00
namespace BareMinimumCrypto {
2021-01-18 22:57:33 +00:00
using nlohmann::json;
optional <Sender> Sender::create (SigningKey k, ExpiringSignature c)
{
Sender s;
s.sender_key = k;
s.cert = c;
return s;
}
Bytes Sender::try_sign (const Bytes & data, Instant now) const {
2021-01-18 22:57:33 +00:00
const auto signed_data = std::move (*sender_key.sign_data (data, now));
const json j {
{"cert", {
{"sig", json::binary (cert.sig)},
{"cert", json::binary (cert.cert)},
}},
{"data", {
{"sig", json::binary (signed_data.sig)},
{"cert", json::binary (signed_data.cert)},
}},
};
return json::to_msgpack (j);
}
2021-01-18 02:52:36 +00:00
optional <Bytes> Sender::sign (const Bytes & data, Instant now) const {
2021-01-18 22:57:33 +00:00
try {
return try_sign (data, now);
}
catch (exception &) {
return nullopt;
}
}
2021-01-18 02:52:36 +00:00
optional <Bytes> Sender::sign (const Bytes & data) const {
2021-01-18 22:57:33 +00:00
return sign (data, Instant::now ());
}
2021-01-18 02:52:36 +00:00
}