From bb9dda6ba9eaf26037af132d2ca5e94bb52e818a Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 10 Oct 2016 07:59:54 -0400 Subject: [PATCH] pretty print sig key types --- common/key.hpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ keygen.cpp | 8 ++++-- keyinfo.cpp | 4 ++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 common/key.hpp diff --git a/common/key.hpp b/common/key.hpp new file mode 100644 index 0000000..859493a --- /dev/null +++ b/common/key.hpp @@ -0,0 +1,72 @@ +#ifndef I2PD_TOOLS_COMMON_KEY_HPP +#define I2PD_TOOLS_COMMON_KEY_HPP +#include "Identity.h" +#include +#include +#include +#include + + +/** @brief returns string representation of a signing key type */ +std::string SigTypeToName(uint16_t keytype) +{ + switch(keytype) { + case i2p::data::SIGNING_KEY_TYPE_DSA_SHA1: + return "DSA-SHA1"; + case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256: + return "ECDSA-P256"; + case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA384_P384: + return "ECDSA-P384"; + case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA512_P521: + return "ECDSA-P521"; + case i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048: + return "RSA-2048-SHA256"; + case i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072: + return "RSA-3072-SHA384"; + case i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096: + return "RSA-4096-SHA512"; + case i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519: + return "ED25519-SHA512"; + default: + std::stringstream ss; + ss << "unknown: " << keytype; + return ss.str(); + } +} + +/** @brief make string uppercase */ +static void ToUpper(std::string & str) +{ + std::transform(str.begin(), str.end(), str.begin(), [] (uint8_t ch) { + return std::toupper(ch); + }); +} +/** @brief returns the signing key number given its name or -1 if there is no key of that type */ +uint16_t NameToSigType(const std::string & keyname) +{ + if(keyname.size() == 1) return atoi(keyname.c_str()); + + std::string name = keyname; + ToUpper(name); + auto npos = std::string::npos; + if(name.find("DSA") == 0) return i2p::data::SIGNING_KEY_TYPE_DSA_SHA1; + + if(name.find("P256") != npos) return i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256; + + if(name.find("P384") != npos) return i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA384_P384; + + if(name.find("RSA2048") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048; + if(name.find("RSA-2048") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048; + + if(name.find("RSA3072") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072; + if(name.find("RSA-3072") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072; + + if(name.find("RSA4096") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096; + if(name.find("RSA-4096") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096; + + if(name.find("ED25519") != npos) return i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519; + + return -1; +} + +#endif diff --git a/keygen.cpp b/keygen.cpp index b61f61d..d99c2c3 100644 --- a/keygen.cpp +++ b/keygen.cpp @@ -3,17 +3,21 @@ #include #include "Crypto.h" #include "Identity.h" +#include "common/key.hpp" int main (int argc, char * argv[]) { if (argc < 2) { - std::cout << "Usage: keygen filename " << std::endl; + std::cout << "Usage: keygen filename " << std::endl; return -1; } i2p::crypto::InitCrypto (false); i2p::data::SigningKeyType type = i2p::data::SIGNING_KEY_TYPE_DSA_SHA1; - if (argc >= 3) type = atoi (argv[2]); + if (argc > 2) { + std::string str(argv[2]); + type = NameToSigType(str); + } auto keys = i2p::data::PrivateKeys::CreateRandomKeys (type); std::ofstream f (argv[1], std::ofstream::binary | std::ofstream::out); if (f) diff --git a/keyinfo.cpp b/keyinfo.cpp index 6b6689c..25d4d23 100644 --- a/keyinfo.cpp +++ b/keyinfo.cpp @@ -4,6 +4,8 @@ #include #include #include +#include "common/key.hpp" + int main(int argc, char * argv[]) { @@ -58,7 +60,7 @@ int main(int argc, char * argv[]) std::cout << "Destination: " << dest->ToBase64() << std::endl; std::cout << "Destination Hash: " << ident.ToBase64() << std::endl; std::cout << "B32 Address: " << ident.ToBase32() << ".b32.i2p" << std::endl; - std::cout << "Signature Type: " << (int) dest->GetSigningKeyType() << std::endl; + std::cout << "Signature Type: " << SigTypeToName(dest->GetSigningKeyType()) << std::endl; std::cout << "Encryption Type: " << (int) dest->GetCryptoKeyType() << std::endl; } else { if(print_full) {