Added base64 sign/verify methods.

This commit is contained in:
Ravin Perera
2019-09-27 17:17:54 +05:30
parent 0569f31c6f
commit 1a8672b656
3 changed files with 54 additions and 11 deletions

View File

@@ -15,9 +15,16 @@ unsigned long long get_sig_len()
return crypto_sign_BYTES;
}
void sign(const unsigned char *msg, unsigned long long msg_len, unsigned char *sig, const unsigned char *seckey)
void sign(const unsigned char *msg, unsigned long long msg_len, unsigned char *sig)
{
crypto_sign_detached(sig, NULL, msg, msg_len, seckey);
crypto_sign_detached(sig, NULL, msg, msg_len, conf::cfg.seckey);
}
string sign_b64(string msg)
{
unsigned char sig[crypto_sign_BYTES];
crypto_sign_detached(sig, NULL, (unsigned char *)msg.c_str(), msg.size() + 1, conf::cfg.seckey);
return base64_encode(sig, crypto_sign_BYTES);
}
bool verify(const unsigned char *msg, unsigned long long msg_len, const unsigned char *sig, const unsigned char *pubkey)
@@ -26,6 +33,22 @@ bool verify(const unsigned char *msg, unsigned long long msg_len, const unsigned
return result == 0;
}
bool verify_b64(string msg, string sigb64, string pubkeyb64)
{
vector<unsigned char> sigVector = base64_decode(sigb64);
unsigned char sig[sigVector.size()];
for (int i = 0; i < sigVector.size(); i++)
sig[i] = sigVector[i];
vector<unsigned char> pubkeyVector = base64_decode(pubkeyb64);
unsigned char pubkey[pubkeyVector.size()];
for (int i = 0; i < pubkeyVector.size(); i++)
pubkey[i] = pubkeyVector[i];
int result = crypto_sign_verify_detached(sig, (unsigned char *)msg.c_str(), msg.size() + 1, pubkey);
return result == 0;
}
void cryptopair_to_b64()
{
conf::cfg.pubkeyb64 = base64_encode(conf::cfg.pubkey, crypto_sign_PUBLICKEYBYTES);
@@ -41,13 +64,10 @@ void b64pair_to_crypto()
unsigned char *privDecodedBytes = (unsigned char *)malloc(privDecoded.size());
for (size_t i = 0; i < pubDecoded.size(); ++i)
{
pubDecodedBytes[i] = pubDecoded[i];
}
for (size_t i = 0; i < privDecoded.size(); ++i)
{
privDecodedBytes[i] = privDecoded[i];
}
if (conf::cfg.pubkey != NULL)
free(conf::cfg.pubkey);

View File

@@ -7,10 +7,32 @@ namespace crypto
{
int init();
/**
* Returns the length of the singature generated using crypto library.
*/
unsigned long long get_sig_len();
void sign(const unsigned char *msg, unsigned long long msg_len, unsigned char *sig, const unsigned char *seckey);
/**
* Generates the signature for the given message using the contract's secret key.
*/
void sign(const unsigned char *msg, unsigned long long msg_len, unsigned char *sig);
/**
* Returns the base64 signature for the given message using the contract's secret key.
*/
string sign_b64(string msg);
/**
* Verifies the given signature with the message using the provided public key.
*/
bool verify(const unsigned char *msg, unsigned long long msg_len, const unsigned char *sig, const unsigned char *pubkey);
/**
* Verifies the given base64 signature with the message using the provided base64 public key.
*/
bool verify_b64(string msg, string sigb64, string pubkeyb64);
} // namespace crypto
#endif

View File

@@ -18,11 +18,12 @@ int main(int argc, char **argv)
}
//Example sign and verification.
unsigned char msg[10] = "hotpocket";
unsigned char *sig = new unsigned char[crypto::get_sig_len()];
crypto::sign(msg, 10, sig, conf::cfg.seckey);
string msg = "hotpocket";
string sigb64 = crypto::sign_b64(msg);
cout << "Message: " << msg << endl;
cout << "Signature: " << sigb64 << endl;
bool isValid = crypto::verify(msg, 10, sig, conf::cfg.pubkey);
bool isValid = crypto::verify_b64(msg, sigb64, conf::cfg.pubkeyb64);
if (isValid)
cout << "Signature verified.\n";
else