aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/crypto.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2021-01-06 17:04:46 +0100
committerEelco Dolstra <edolstra@gmail.com>2021-01-06 17:04:46 +0100
commit555152ffe8494190ca42dd481991c9b54759f686 (patch)
tree2f4a5615c5b704ee950c6e5f81c3b1116740d2bb /src/libstore/crypto.cc
parent146af4ee9bb03968a7322a1ac70dc60c8d5a35e2 (diff)
crypto.cc: API cleanup and add generate() / to_string() methods
Diffstat (limited to 'src/libstore/crypto.cc')
-rw-r--r--src/libstore/crypto.cc33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/libstore/crypto.cc b/src/libstore/crypto.cc
index 9ec8abd22..135ced277 100644
--- a/src/libstore/crypto.cc
+++ b/src/libstore/crypto.cc
@@ -8,15 +8,15 @@
namespace nix {
-static std::pair<std::string, std::string> split(const string & s)
+static std::pair<std::string_view, std::string_view> split(std::string_view s)
{
size_t colon = s.find(':');
if (colon == std::string::npos || colon == 0)
return {"", ""};
- return {std::string(s, 0, colon), std::string(s, colon + 1)};
+ return {s.substr(0, colon), s.substr(colon + 1)};
}
-Key::Key(const string & s)
+Key::Key(std::string_view s)
{
auto ss = split(s);
@@ -29,7 +29,12 @@ Key::Key(const string & s)
key = base64Decode(key);
}
-SecretKey::SecretKey(const string & s)
+std::string Key::to_string() const
+{
+ return name + ":" + base64Encode(key);
+}
+
+SecretKey::SecretKey(std::string_view s)
: Key(s)
{
#if HAVE_SODIUM
@@ -45,7 +50,7 @@ SecretKey::SecretKey(const string & s)
}
#endif
-std::string SecretKey::signDetached(const std::string & data) const
+std::string SecretKey::signDetached(std::string_view data) const
{
#if HAVE_SODIUM
unsigned char sig[crypto_sign_BYTES];
@@ -69,7 +74,21 @@ PublicKey SecretKey::toPublicKey() const
#endif
}
-PublicKey::PublicKey(const string & s)
+SecretKey SecretKey::generate(std::string_view name)
+{
+#if HAVE_SODIUM
+ unsigned char pk[crypto_sign_PUBLICKEYBYTES];
+ unsigned char sk[crypto_sign_SECRETKEYBYTES];
+ if (crypto_sign_keypair(pk, sk) != 0)
+ throw Error("key generation failed");
+
+ return SecretKey(name, std::string((char *) sk, crypto_sign_SECRETKEYBYTES));
+#else
+ noSodium();
+#endif
+}
+
+PublicKey::PublicKey(std::string_view s)
: Key(s)
{
#if HAVE_SODIUM
@@ -84,7 +103,7 @@ bool verifyDetached(const std::string & data, const std::string & sig,
#if HAVE_SODIUM
auto ss = split(sig);
- auto key = publicKeys.find(ss.first);
+ auto key = publicKeys.find(std::string(ss.first));
if (key == publicKeys.end()) return false;
auto sig2 = base64Decode(ss.second);