Util: Add KeyStore and SHA256 to CLI

This commit is contained in:
zzz
2020-08-01 13:22:24 +00:00
parent 336563e7c0
commit fa08d2f946
3 changed files with 54 additions and 2 deletions

View File

@ -1280,7 +1280,6 @@ public final class KeyStoreUtil {
* KeyStoreUtil keygen file.ks alias keypw (create keypair in keystore)
* KeyStoreUtil keygen2 file.ks alias keypw (create keypair using I2PProvider)
*/
/****
public static void main(String[] args) {
try {
if (args.length > 0 && "import".equals(args[0])) {
@ -1299,6 +1298,10 @@ public final class KeyStoreUtil {
testKeygen2(args);
return;
}
if (args.length > 0 && "list".equals(args[0])) {
listKeys(args);
return;
}
File ksf = (args.length > 0) ? new File(args[0]) : null;
if (ksf != null && !ksf.exists()) {
createKeyStore(ksf, DEFAULT_KEYSTORE_PASSWORD);
@ -1339,6 +1342,10 @@ public final class KeyStoreUtil {
private static void testExport(String[] args) throws Exception {
if (args.length != 4) {
System.err.println("Usage: KeyStoreUtil export keystore.ks keyalias keypassword");
System.exit(1);
}
File ksf = new File(args[1]);
String alias = args[2];
String pw = args[3];
@ -1386,5 +1393,40 @@ public final class KeyStoreUtil {
net.i2p.data.Signature sig = SigUtil.fromJavaSig(bsig, type);
System.out.println("Signature test: " + sig);
}
****/
private static void listKeys(String[] args) {
if (args.length != 2) {
System.err.println("Usage: KeyStoreUtil list keystore.ks");
System.exit(1);
}
File ksf = new File(args[1]);
if (ksf.exists()) {
InputStream fis = null;
try {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
fis = new FileInputStream(ksf);
ks.load(fis, DEFAULT_KEYSTORE_PASSWORD.toCharArray());
System.out.println("Certificates:");
for(Enumeration<String> e = ks.aliases(); e.hasMoreElements();) {
String alias = e.nextElement();
if (ks.isCertificateEntry(alias))
System.out.println(alias);
}
System.out.println("\nPrivate keys:");
for(Enumeration<String> e = ks.aliases(); e.hasMoreElements();) {
String alias = e.nextElement();
if (ks.isKeyEntry(alias))
System.out.println(alias);
}
} catch (IOException ioe) {
error("Unable to get certificates in key store " + ksf, ioe);
} catch (GeneralSecurityException gse) {
error("Unable to get certificates in key store " + ksf, gse);
} finally {
try { if (fis != null) fis.close(); } catch (IOException foo) {}
}
} else {
System.err.println("Keystore file not found: " + ksf);
}
}
}

View File

@ -92,4 +92,12 @@ public final class SHA256Generator {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: SHA256Generator 'text to hash'");
System.exit(1);
}
System.out.println(net.i2p.data.Base64.encode(getInstance().calculateHash(net.i2p.data.DataHelper.getUTF8(args[0])).getData()));
}
}

View File

@ -26,7 +26,9 @@ public class CommandLine {
"net.i2p.crypto.CertUtil",
"net.i2p.crypto.CryptoCheck",
"net.i2p.crypto.KeyGenerator",
"net.i2p.crypto.KeyStoreUtil",
"net.i2p.crypto.SelfSignedGenerator",
"net.i2p.crypto.SHA256Generator",
"net.i2p.crypto.SU3File",
"net.i2p.crypto.TrustedUpdate",
"net.i2p.data.Base32",