key.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef I2PD_TOOLS_COMMON_KEY_HPP
  2. #define I2PD_TOOLS_COMMON_KEY_HPP
  3. #include "Identity.h"
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <sstream>
  7. #include <string>
  8. /** @brief returns string representation of a signing key type */
  9. std::string SigTypeToName(uint16_t keytype)
  10. {
  11. switch(keytype) {
  12. case i2p::data::SIGNING_KEY_TYPE_DSA_SHA1:
  13. return "DSA-SHA1";
  14. case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256:
  15. return "ECDSA-P256";
  16. case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA384_P384:
  17. return "ECDSA-P384";
  18. case i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA512_P521:
  19. return "ECDSA-P521";
  20. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048:
  21. return "RSA-2048-SHA256";
  22. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072:
  23. return "RSA-3072-SHA384";
  24. case i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096:
  25. return "RSA-4096-SHA512";
  26. case i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519:
  27. return "ED25519-SHA512";
  28. case i2p::data::SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256:
  29. return "GOSTR3410-A-GOSTR3411-256";
  30. case i2p::data::SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512:
  31. return "GOSTR3410-TC26-A-GOSTR3411-512";
  32. case i2p::data::SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519:
  33. return "RED25519-SHA512";
  34. default:
  35. std::stringstream ss;
  36. ss << "unknown: " << keytype;
  37. return ss.str();
  38. }
  39. }
  40. /** @brief make string uppercase */
  41. static void ToUpper(std::string & str)
  42. {
  43. std::transform(str.begin(), str.end(), str.begin(), [] (uint8_t ch) {
  44. return std::toupper(ch);
  45. });
  46. }
  47. /** @brief returns the signing key number given its name or -1 if there is no key of that type */
  48. uint16_t NameToSigType(const std::string & keyname)
  49. {
  50. if(keyname.size() <= 2) return atoi(keyname.c_str());
  51. std::string name = keyname;
  52. ToUpper(name);
  53. auto npos = std::string::npos;
  54. if(name.find("DSA") == 0) return i2p::data::SIGNING_KEY_TYPE_DSA_SHA1;
  55. if(name.find("P256") != npos) return i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256;
  56. if(name.find("P384") != npos) return i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA384_P384;
  57. if(name.find("RSA-SHA265") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA256_2048;
  58. if(name.find("RSA-SHA384") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA384_3072;
  59. if(name.find("RSA-SHA512") != npos) return i2p::data::SIGNING_KEY_TYPE_RSA_SHA512_4096;
  60. if(name.find("ED25519") != npos) return i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519;
  61. if(name.find("GOSTR3410-A-GOSTR3411-256") != npos) return i2p::data::SIGNING_KEY_TYPE_GOSTR3410_CRYPTO_PRO_A_GOSTR3411_256;
  62. if(name.find("GOSTR3410-TC26-A-GOSTR3411-512") != npos) return i2p::data::SIGNING_KEY_TYPE_GOSTR3410_TC26_A_512_GOSTR3411_512;
  63. if(name.find("RED25519") != npos) return i2p::data::SIGNING_KEY_TYPE_REDDSA_SHA512_ED25519;
  64. return -1;
  65. }
  66. #endif