From d222c7a9986dc7bfdfb9dc1159ae46df9388ba01 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Wed, 25 Feb 2009 01:18:38 +0000
Subject: [PATCH] move dest-to-hash conversion to new helper class

---
 .../i2p/router/web/ConfigKeyringHandler.java  | 21 +----
 core/java/src/net/i2p/util/ConvertToHash.java | 76 +++++++++++++++++++
 2 files changed, 79 insertions(+), 18 deletions(-)
 create mode 100644 core/java/src/net/i2p/util/ConvertToHash.java

diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigKeyringHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigKeyringHandler.java
index 09f0905bf3..b43bc4d1f1 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigKeyringHandler.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigKeyringHandler.java
@@ -2,9 +2,9 @@ package net.i2p.router.web;
 
 import net.i2p.I2PAppContext;
 import net.i2p.data.DataFormatException;
-import net.i2p.data.Destination;
 import net.i2p.data.Hash;
 import net.i2p.data.SessionKey;
+import net.i2p.util.ConvertToHash;
 
 /**
  *  Support additions via B64 Destkey, B64 Desthash, or blahblah.i2p
@@ -19,27 +19,12 @@ public class ConfigKeyringHandler extends FormHandler {
                 addFormError("You must enter a destination and a key");
                 return;
             }
-            Hash h = new Hash();
-            try {
-                h.fromBase64(_peer);
-            } catch (DataFormatException dfe) {}
-            if (h.getData() == null) {
-                try {
-                    Destination d = new Destination();
-                    d.fromBase64(_peer);
-                    h = d.calculateHash();
-                } catch (DataFormatException dfe) {}
-            }
-            if (h.getData() == null) {
-                Destination d = _context.namingService().lookup(_peer);
-                if (d != null)
-                    h = d.calculateHash();
-            }
+            Hash h = ConvertToHash.getHash(_peer);
             SessionKey sk = new SessionKey();
             try {
                 sk.fromBase64(_key);
             } catch (DataFormatException dfe) {}
-            if (h.getData() != null && sk.getData() != null) {
+            if (h != null && h.getData() != null && sk.getData() != null) {
                 _context.keyRing().put(h, sk);
                 addFormNotice("Key for " + h.toBase64() + " added to keyring");
             } else {
diff --git a/core/java/src/net/i2p/util/ConvertToHash.java b/core/java/src/net/i2p/util/ConvertToHash.java
new file mode 100644
index 0000000000..0878556400
--- /dev/null
+++ b/core/java/src/net/i2p/util/ConvertToHash.java
@@ -0,0 +1,76 @@
+package net.i2p.util;
+
+import net.i2p.I2PAppContext;
+import net.i2p.data.Base32;
+import net.i2p.data.DataFormatException;
+import net.i2p.data.Destination;
+import net.i2p.data.Hash;
+
+/**
+ *  Convert any kind of destination String to a hash
+ *  Supported:
+ *    Base64 dest
+ *    Base64 dest.i2p
+ *    Base64 Hash
+ *    Base32 Hash
+ *    Base32 desthash.b32.i2p
+ *    example.i2p
+ *
+ *  @return null on failure
+ *
+ *  @author zzz
+ */
+public class ConvertToHash {
+    
+    public static Hash getHash(String peer) {
+        if (peer == null)
+            return null;
+        Hash h = new Hash();
+        String peerLC = peer.toLowerCase();
+        // b64 hash
+        if (peer.length() == 44 && !peerLC.endsWith(".i2p")) {
+            try {
+                h.fromBase64(peer);
+            } catch (DataFormatException dfe) {}
+        }
+        // b64 dest.i2p
+        if (h.getData() == null && peer.length() >= 520 && peerLC.endsWith(".i2p")) {
+            try {
+                Destination d = new Destination();
+                d.fromBase64(peer.substring(0, peer.length() - 4));
+                h = d.calculateHash();
+            } catch (DataFormatException dfe) {}
+        }
+        // b64 dest
+        if (h.getData() == null && peer.length() >= 516 && !peerLC.endsWith(".i2p")) {
+            try {
+                Destination d = new Destination();
+                d.fromBase64(peer);
+                h = d.calculateHash();
+            } catch (DataFormatException dfe) {}
+        }
+        // b32 hash.b32.i2p
+        // do this here rather than in naming service so it will work
+        // even if the leaseset is not found
+        if (h.getData() == null && peer.length() == 60 && peerLC.endsWith(".b32.i2p")) {
+            byte[] b = Base32.decode(peer.substring(0, 52));
+            if (b != null && b.length == Hash.HASH_LENGTH)
+                h.setData(b);
+        }
+        // b32 hash
+        if (h.getData() == null && peer.length() == 52 && !peerLC.endsWith(".i2p")) {
+            byte[] b = Base32.decode(peer);
+            if (b != null && b.length == Hash.HASH_LENGTH)
+                h.setData(b);
+        }
+        // example.i2p
+        if (h.getData() == null) {
+            Destination d = I2PAppContext.getGlobalContext().namingService().lookup(peer);
+            if (d != null)
+                h = d.calculateHash();
+        }
+        if (h.getData() == null)
+            return null;
+        return h;
+    }
+}
-- 
GitLab