You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2016/08/02 03:31:31 UTC

mina-sshd git commit: [SSHD-685] Add KeyRandomArt#combine(KeyIdentityProvider}

Repository: mina-sshd
Updated Branches:
  refs/heads/master 626dca723 -> 9d34cd6e8


[SSHD-685] Add KeyRandomArt#combine(KeyIdentityProvider}


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/9d34cd6e
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/9d34cd6e
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/9d34cd6e

Branch: refs/heads/master
Commit: 9d34cd6e8a6a2720d1c0126b1db01087eb67d0d2
Parents: 626dca7
Author: Lyor Goldstein <ly...@gmail.com>
Authored: Tue Aug 2 06:33:00 2016 +0300
Committer: Lyor Goldstein <ly...@gmail.com>
Committed: Tue Aug 2 06:33:00 2016 +0300

----------------------------------------------------------------------
 .../sshd/common/config/keys/KeyRandomArt.java   | 64 ++++++++++++++++++++
 .../common/config/keys/KeyRandomArtTest.java    | 14 +++--
 .../common/keyprovider/KeyIdentityProvider.java |  3 +-
 3 files changed, 76 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9d34cd6e/sshd-contrib/src/main/java/org/apache/sshd/common/config/keys/KeyRandomArt.java
----------------------------------------------------------------------
diff --git a/sshd-contrib/src/main/java/org/apache/sshd/common/config/keys/KeyRandomArt.java b/sshd-contrib/src/main/java/org/apache/sshd/common/config/keys/KeyRandomArt.java
index 19fc80f..1d775d2 100644
--- a/sshd-contrib/src/main/java/org/apache/sshd/common/config/keys/KeyRandomArt.java
+++ b/sshd-contrib/src/main/java/org/apache/sshd/common/config/keys/KeyRandomArt.java
@@ -21,14 +21,19 @@ package org.apache.sshd.common.config.keys;
 
 import java.io.IOException;
 import java.io.StreamCorruptedException;
+import java.security.KeyPair;
 import java.security.PublicKey;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Objects;
 
 import org.apache.sshd.common.Factory;
 import org.apache.sshd.common.digest.Digest;
+import org.apache.sshd.common.keyprovider.KeyIdentityProvider;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.ValidateUtils;
 
@@ -190,6 +195,65 @@ public class KeyRandomArt {
     }
 
     /**
+     * Creates the combined representation of the random art entries for the provided keys
+     *
+     * @param separator The separator to use between the arts - if empty char
+     * ('\0') then no separation is done
+     * @param provider The {@link KeyIdentityProvider} - ignored if {@code null}
+     * or has no keys to provide
+     * @return The combined representation
+     * @throws Exception If failed to extract or combine the entries
+     * @see #combine(Appendable, char, KeyIdentityProvider)
+     */
+    public static String combine(char separator, KeyIdentityProvider provider) throws Exception {
+        return combine(new StringBuilder(4 * (FLDSIZE_X + 4) * (FLDSIZE_Y + 3)), separator, provider).toString();
+    }
+
+    /**
+     * Appends the combined random art entries for the provided keys
+     *
+     * @param <A> The {@link Appendable} output writer
+     * @param sb The writer
+     * @param separator The separator to use between the arts - if empty char
+     * ('\0') then no separation is done
+     * @param provider The {@link KeyIdentityProvider} - ignored if {@code null}
+     * or has no keys to provide
+     * @return The updated writer instance
+     * @throws Exception If failed to extract or write the entries
+     * @see #generate(KeyIdentityProvider)
+     * @see #combine(Appendable, char, Collection)
+     */
+    public static <A extends Appendable> A combine(A sb, char separator, KeyIdentityProvider provider) throws Exception {
+        return combine(sb, separator, generate(provider));
+    }
+
+    /**
+     * Extracts and generates random art entries for all key in the provider
+     *
+     * @param provider The {@link KeyIdentityProvider} - ignored if {@code null}
+     * or has no keys to provide
+     * @return The extracted {@link KeyRandomArt}s
+     * @throws Exception If failed to extract the entries
+     * @see KeyIdentityProvider#loadKeys()
+     */
+    public static Collection<KeyRandomArt> generate(KeyIdentityProvider provider) throws Exception {
+        Iterable<KeyPair> keys = (provider == null) ? null : provider.loadKeys();
+        Iterator<KeyPair> iter = (keys == null) ? null : keys.iterator();
+        if ((iter == null) || (!iter.hasNext())) {
+            return Collections.emptyList();
+        }
+
+        Collection<KeyRandomArt> arts = new LinkedList<>();
+        do {
+            KeyPair kp = iter.next();
+            KeyRandomArt a = new KeyRandomArt(kp.getPublic());
+            arts.add(a);
+        } while(iter.hasNext());
+
+        return arts;
+    }
+
+    /**
      * Combines the arts in a user-friendly way so they are aligned with each other
      *
      * @param <A> The {@link Appendable} output writer

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9d34cd6e/sshd-contrib/src/test/java/org/apache/sshd/common/config/keys/KeyRandomArtTest.java
----------------------------------------------------------------------
diff --git a/sshd-contrib/src/test/java/org/apache/sshd/common/config/keys/KeyRandomArtTest.java b/sshd-contrib/src/test/java/org/apache/sshd/common/config/keys/KeyRandomArtTest.java
index edea5f1..a0c472c 100644
--- a/sshd-contrib/src/test/java/org/apache/sshd/common/config/keys/KeyRandomArtTest.java
+++ b/sshd-contrib/src/test/java/org/apache/sshd/common/config/keys/KeyRandomArtTest.java
@@ -26,6 +26,7 @@ import java.util.LinkedList;
 import java.util.List;
 
 import org.apache.sshd.common.cipher.ECCurves;
+import org.apache.sshd.common.keyprovider.KeyIdentityProvider;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.util.test.BaseTestSupport;
 import org.apache.sshd.util.test.Utils;
@@ -40,7 +41,7 @@ import org.junit.runners.Parameterized.Parameters;
  */
 @RunWith(Parameterized.class)   // see https://github.com/junit-team/junit/wiki/Parameterized-tests
 public class KeyRandomArtTest extends BaseTestSupport {
-    private static final Collection<KeyRandomArt> ARTS = new LinkedList<>();
+    private static final Collection<KeyPair> KEYS = new LinkedList<>();
 
     private final String algorithm;
     private final int keySize;
@@ -50,6 +51,7 @@ public class KeyRandomArtTest extends BaseTestSupport {
         this.algorithm = algorithm;
         this.keySize = keySize;
         this.keyPair = Utils.generateKeyPair(algorithm, keySize);
+        KEYS.add(this.keyPair);
     }
 
     @Parameters(name = "algorithm={0}, key-size={1}")
@@ -72,7 +74,13 @@ public class KeyRandomArtTest extends BaseTestSupport {
 
     @AfterClass
     public static void dumpAllArts() throws Exception {
-        KeyRandomArt.combine(System.out, ' ', ARTS);
+        KeyRandomArt.combine(System.out, ' ', new KeyIdentityProvider() {
+            @Override
+            @SuppressWarnings("synthetic-access")
+            public Iterable<KeyPair> loadKeys() {
+                return KEYS;
+            }
+        });
     }
 
     @Test
@@ -95,7 +103,5 @@ public class KeyRandomArtTest extends BaseTestSupport {
 
             assertTrue("Mismatched line length #" + (index + 1) + ": " + l.length(), l.length() >= (KeyRandomArt.FLDSIZE_X + 2));
         }
-
-        ARTS.add(art);
     }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9d34cd6e/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/KeyIdentityProvider.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/KeyIdentityProvider.java b/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/KeyIdentityProvider.java
index 98ebaea..4656190 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/KeyIdentityProvider.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/keyprovider/KeyIdentityProvider.java
@@ -34,6 +34,7 @@ import org.apache.sshd.common.util.Transformer;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
+@FunctionalInterface
 public interface KeyIdentityProvider {
     /**
      * An &quot;empty&quot; implementation of {@link KeyIdentityProvider} that
@@ -178,7 +179,7 @@ public interface KeyIdentityProvider {
                 return Collections.emptyList();
             }
 
-            Collection<Supplier<Iterable<KeyPair>>> suppliers = new ArrayList<Supplier<Iterable<KeyPair>>>(providers.size());
+            Collection<Supplier<Iterable<KeyPair>>> suppliers = new ArrayList<>(providers.size());
             for (final KeyIdentityProvider p : providers) {
                 if (p == null) {
                     continue;