From 67957afb6ba01bcd85f1abd1a50ad2c1aa813c7c Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman <0xdec05eba@gmail.com> Date: Wed, 14 Feb 2018 22:18:48 +0100 Subject: Sign messages/verify message signatures --- src/Signature.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/Signature.cpp') diff --git a/src/Signature.cpp b/src/Signature.cpp index 946d754..8d3654d 100644 --- a/src/Signature.cpp +++ b/src/Signature.cpp @@ -35,6 +35,19 @@ namespace odhtdb return *this; } + string PublicKey::unsign(const DataView &signedMessage) const + { + if(signedMessage.size < SIGNED_HASH_SIZE) + throw UnsignInvalidSizeException("Signed message is too small (corrupt or malicious signed message)"); + + string result; + result.resize(signedMessage.size - SIGNED_HASH_SIZE); + if(crypto_sign_ed25519_open((unsigned char*)&result[0], nullptr, (const unsigned char*)signedMessage.data, signedMessage.size, (unsigned char*)data) != 0) + throw UnsignWrongKeyException("Message was not signed with matching private key"); + + return result; + } + string PublicKey::toString() const { string result; @@ -67,13 +80,13 @@ namespace odhtdb return *this; } - string PrivateKey::sign(const string &dataToSign) const + string PrivateKey::sign(const DataView &dataToSign) const { string result; - result.resize(crypto_sign_ed25519_BYTES + dataToSign.size()); + result.resize(crypto_sign_ed25519_BYTES + dataToSign.size); unsigned long long resultSize; - if(crypto_sign_ed25519((unsigned char*)&result[0], &resultSize, (unsigned char*)dataToSign.data(), dataToSign.size(), (unsigned char*)data) != 0) + if(crypto_sign_ed25519((unsigned char*)&result[0], &resultSize, (unsigned char*)dataToSign.data, dataToSign.size, (unsigned char*)data) != 0) throw DataSignException("Failed to sign data. Is private key invalid?"); if(resultSize != result.size()) -- cgit v1.2.3