tools: Allow specifying passphrase for ndnsec import/export in command line
Change-Id: Ic389249e017df27b0246cdd1fcd3a1431607246b
Refs: #4633
diff --git a/tools/ndnsec/import.cpp b/tools/ndnsec/import.cpp
index a818a3a..b0735e3 100644
--- a/tools/ndnsec/import.cpp
+++ b/tools/ndnsec/import.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -33,10 +33,13 @@
std::string input("-");
std::string importPassword;
- po::options_description description("General Usage\n ndnsec import [-h] input \nGeneral options");
+ po::options_description description("General Usage\n"
+ " ndnsec import [-h] [-P passphrase] input \n"
+ "General options");
description.add_options()
("help,h", "produce help message")
("input,i", po::value<std::string>(&input), "input source, stdin if -")
+ ("password,P", po::value<std::string>(&importPassword), "Passphrase (will prompt if empty or not specified)")
;
po::positional_options_description p;
@@ -67,15 +70,18 @@
else
safeBag = io::load<security::SafeBag>(input);
- int count = 3;
- while (!getPassword(importPassword, "Passphrase for the private key: ", false)) {
- count--;
- if (count <= 0) {
- std::cerr << "ERROR: Fail to get password" << std::endl;
- memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
- return 1;
+ if (importPassword.empty()) {
+ int count = 3;
+ while (!getPassword(importPassword, "Passphrase for the private key: ", false)) {
+ count--;
+ if (count <= 0) {
+ std::cerr << "ERROR: Fail to get password" << std::endl;
+ memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
+ return 1;
+ }
}
}
+
keyChain.importSafeBag(*safeBag, importPassword.c_str(), importPassword.size());
memset(const_cast<char*>(importPassword.c_str()), 0, importPassword.size());
return 0;