From 074c630ac6af5642a986ebb64b5679ea89f94e4c Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sat, 24 Dec 2016 13:46:51 +0000
Subject: [PATCH] Utils: Add RKG command line tool

---
 .../i2p/data/router/RouterKeyGenerator.java   | 38 +++++++++++--------
 .../java/src/net/i2p/router/CommandLine.java  |  1 +
 2 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/router/java/src/net/i2p/data/router/RouterKeyGenerator.java b/router/java/src/net/i2p/data/router/RouterKeyGenerator.java
index a099d062ab..dd5d4312d7 100644
--- a/router/java/src/net/i2p/data/router/RouterKeyGenerator.java
+++ b/router/java/src/net/i2p/data/router/RouterKeyGenerator.java
@@ -22,6 +22,7 @@ import net.i2p.crypto.SHA256Generator;
 import net.i2p.data.DataHelper;
 import net.i2p.data.Hash;
 import net.i2p.data.RoutingKeyGenerator;
+import net.i2p.util.ConvertToHash;
 import net.i2p.util.HexDump;
 import net.i2p.util.Log;
 
@@ -216,23 +217,30 @@ public class RouterKeyGenerator extends RoutingKeyGenerator {
         return SHA256Generator.getInstance().calculateHash(modVal);
     }
 
-/****
+    /**
+     * @since 0.9.29
+     */
     public static void main(String args[]) {
-        Hash k1 = new Hash();
-        byte k1d[] = new byte[Hash.HASH_LENGTH];
-        RandomSource.getInstance().nextBytes(k1d);
-        k1.setData(k1d);
-
-        for (int i = 0; i < 10; i++) {
-            System.out.println("K1:  " + k1);
-            Hash k1m = RoutingKeyGenerator.getInstance().getRoutingKey(k1);
-            System.out.println("MOD: " + new String(RoutingKeyGenerator.getInstance().getModData()));
-            System.out.println("K1M: " + k1m);
+        if (args.length <= 0) {
+            System.err.println("Usage: RouterKeyGenerator [-days] [+days] hash|hostname|destination");
+            System.exit(1);
+        }
+        long now = System.currentTimeMillis();
+        Hash h;
+        if (args.length > 1 && (args[0].startsWith("+") || args[0].startsWith("-"))) {
+            now += Integer.parseInt(args[0]) * 24*60*60*1000L;
+            h = ConvertToHash.getHash(args[1]);
+        } else {
+            h = ConvertToHash.getHash(args[0]);
         }
-        try {
-            Thread.sleep(2000);
-        } catch (Throwable t) { // nop
+        if (h == null) {
+            System.err.println("Bad hash");
+            System.exit(1);
         }
+        RouterKeyGenerator rkg = new RouterKeyGenerator(I2PAppContext.getGlobalContext());
+        Hash rkey = rkg.getRoutingKey(h, now);
+        System.out.println("Date:         " + rkg._fmt.format(now));
+        System.out.println("Original key: " + h.toBase64());
+        System.out.println("Routing key:  " + rkey.toBase64());
     }
-****/
 }
diff --git a/router/java/src/net/i2p/router/CommandLine.java b/router/java/src/net/i2p/router/CommandLine.java
index 60781d1548..92a060433d 100644
--- a/router/java/src/net/i2p/router/CommandLine.java
+++ b/router/java/src/net/i2p/router/CommandLine.java
@@ -15,6 +15,7 @@ public class CommandLine extends net.i2p.util.CommandLine {
 
     protected static final List<String> RCLASSES = Arrays.asList(new String[] {
         "net.i2p.data.router.RouterInfo",
+        "net.i2p.data.router.RouterKeyGenerator",
         "net.i2p.router.MultiRouter",
         "net.i2p.router.Router",
         "net.i2p.router.RouterLaunch",
-- 
GitLab