From 71d4221af2604850363ed890949fe5227af85cd3 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 5 Oct 2016 10:06:06 -0400 Subject: [PATCH] add keyinfo tool --- .gitignore | 10 ++++- Makefile | 8 +++- contrib/tools/Makefile | 7 ++++ contrib/tools/README.md | 6 +++ contrib/tools/keyinfo/Makefile | 8 ++++ contrib/tools/keyinfo/README.md | 5 +++ contrib/tools/keyinfo/keyinfo.cpp | 70 +++++++++++++++++++++++++++++++ 7 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 contrib/tools/Makefile create mode 100644 contrib/tools/README.md create mode 100644 contrib/tools/keyinfo/Makefile create mode 100644 contrib/tools/keyinfo/README.md create mode 100644 contrib/tools/keyinfo/keyinfo.cpp diff --git a/.gitignore b/.gitignore index b6cffd15..73297488 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,7 @@ netDb autom4te.cache .deps stamp-h1 -Makefile +#Makefile config.h config.h.in~ config.log @@ -238,3 +238,11 @@ pip-log.txt # Sphinx docs/_build /androidIdea/ + + +# emacs files +*~ +*\#* + +# gdb files +.gdb_history \ No newline at end of file diff --git a/Makefile b/Makefile index 147bedd4..c68b977e 100644 --- a/Makefile +++ b/Makefile @@ -80,7 +80,13 @@ $(ARLIB): $(patsubst %.cpp,obj/%.o,$(LIB_SRC)) $(ARLIB_CLIENT): $(patsubst %.cpp,obj/%.o,$(LIB_CLIENT_SRC)) ar -r $@ $^ -clean: +tools: $(ARLIB) + $(MAKE) -C contrib/tools/ + +clean-tools: + $(MAKE) -C contrib/tools/ clean + +clean: clean-tools rm -rf obj rm -rf docs/generated $(RM) $(I2PD) $(SHLIB) $(ARLIB) $(SHLIB_CLIENT) $(ARLIB_CLIENT) diff --git a/contrib/tools/Makefile b/contrib/tools/Makefile new file mode 100644 index 00000000..6c60bb59 --- /dev/null +++ b/contrib/tools/Makefile @@ -0,0 +1,7 @@ + + +all: + $(MAKE) -C keyinfo keyinfo + +clean: + $(MAKE) -C keyinfo clean diff --git a/contrib/tools/README.md b/contrib/tools/README.md new file mode 100644 index 00000000..e598adfa --- /dev/null +++ b/contrib/tools/README.md @@ -0,0 +1,6 @@ +Collection of i2pd related tools + +# Buildling + + +run `make tools` in the root of the repo diff --git a/contrib/tools/keyinfo/Makefile b/contrib/tools/keyinfo/Makefile new file mode 100644 index 00000000..44a13ff7 --- /dev/null +++ b/contrib/tools/keyinfo/Makefile @@ -0,0 +1,8 @@ +CXXFLAGS = -I ../../../ +all: keyinfo + +keyinfo: + $(CXX) -o keyinfo keyinfo.cpp $(CXXFLAGS) -std=c++11 ../../../libi2pd.a -lssl -lcrypto -lboost_system + +clean: + $(RM) keyinfo diff --git a/contrib/tools/keyinfo/README.md b/contrib/tools/keyinfo/README.md new file mode 100644 index 00000000..b3c39a4b --- /dev/null +++ b/contrib/tools/keyinfo/README.md @@ -0,0 +1,5 @@ +# keyinfo + +print information about a private key file + + diff --git a/contrib/tools/keyinfo/keyinfo.cpp b/contrib/tools/keyinfo/keyinfo.cpp new file mode 100644 index 00000000..6b6689c6 --- /dev/null +++ b/contrib/tools/keyinfo/keyinfo.cpp @@ -0,0 +1,70 @@ +#include "Identity.h" +#include +#include +#include +#include +#include + +int main(int argc, char * argv[]) +{ + if(argc == 1) { + std::cout << "usage: " << argv[0] << " [-v] [-d] privatekey.dat" << std::endl; + return -1; + } + int opt; + bool print_full = false; + bool verbose = false; + while((opt = getopt(argc, argv, "vd"))!=-1) { + switch(opt){ + case 'v': + verbose = true; + break; + case 'd': + print_full = true; + break; + default: + std::cout << "usage: " << argv[0] << " [-v] [-d] privatekey.dat" << std::endl; + return -1; + } + } + std::string fname(argv[optind]); + i2p::data::PrivateKeys keys; + { + std::vector buff; + std::ifstream inf; + inf.open(fname); + if (!inf.is_open()) { + std::cout << "cannot open private key file " << fname << std::endl; + return 2; + } + inf.seekg(0, std::ios::end); + const std::size_t len = inf.tellg(); + inf.seekg(0, std::ios::beg); + buff.resize(len); + inf.read((char*)buff.data(), buff.size()); + if (!keys.FromBuffer(buff.data(), buff.size())) { + std::cout << "bad key file format" << std::endl; + return 3; + } + } + auto dest = keys.GetPublic(); + if(!dest) { + std::cout << "failed to extract public key" << std::endl; + return 3; + } + + const auto & ident = dest->GetIdentHash(); + if (verbose) { + 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 << "Encryption Type: " << (int) dest->GetCryptoKeyType() << std::endl; + } else { + if(print_full) { + std::cout << dest->ToBase64() << std::endl; + } else { + std::cout << ident.ToBase32() << ".b32.i2p" << std::endl; + } + } +}