diff options
author | Raphael <raphael@google.com> | 2011-10-31 13:40:46 -0700 |
---|---|---|
committer | Raphael <raphael@google.com> | 2011-11-01 09:59:11 -0700 |
commit | 4188354e1be8c8f7c629d5164ffd4a4dcfc4bfd9 (patch) | |
tree | 3cd6f0a27cf343f5103fa713abcbaa36434276a8 | |
parent | de18b5a12cb5c3baed6f41c235fac9129533cb8f (diff) | |
download | sdk-4188354e1be8c8f7c629d5164ffd4a4dcfc4bfd9.zip sdk-4188354e1be8c8f7c629d5164ffd4a4dcfc4bfd9.tar.gz sdk-4188354e1be8c8f7c629d5164ffd4a4dcfc4bfd9.tar.bz2 |
android create id: prompt for passwd
Change-Id: Id030c98501fd0b971a7377da95b452370dc7e1e7
-rw-r--r-- | sdkmanager/app/src/com/android/sdkmanager/Main.java | 104 | ||||
-rw-r--r-- | sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java | 9 |
2 files changed, 80 insertions, 33 deletions
diff --git a/sdkmanager/app/src/com/android/sdkmanager/Main.java b/sdkmanager/app/src/com/android/sdkmanager/Main.java index 7f6d415..06dea46 100644 --- a/sdkmanager/app/src/com/android/sdkmanager/Main.java +++ b/sdkmanager/app/src/com/android/sdkmanager/Main.java @@ -66,6 +66,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.atomic.AtomicBoolean; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpressionException; @@ -1276,35 +1277,43 @@ public class Main { private void createIdentity() { - String account = (String) mSdkCommandLine.getValue( - SdkCommandLine.VERB_CREATE, - SdkCommandLine.OBJECT_IDENTITY, - SdkCommandLine.KEY_ACCOUNT); - - String keystorePath = (String) mSdkCommandLine.getValue( - SdkCommandLine.VERB_CREATE, - SdkCommandLine.OBJECT_IDENTITY, - SdkCommandLine.KEY_KEYSTORE); - - String aliasName = (String) mSdkCommandLine.getValue( - SdkCommandLine.VERB_CREATE, - SdkCommandLine.OBJECT_IDENTITY, - SdkCommandLine.KEY_ALIAS); - - String keystorePass = (String) mSdkCommandLine.getValue( - SdkCommandLine.VERB_CREATE, - SdkCommandLine.OBJECT_IDENTITY, - SdkCommandLine.KEY_STOREPASS); - - String aliasPass = (String) mSdkCommandLine.getValue( - SdkCommandLine.VERB_CREATE, - SdkCommandLine.OBJECT_IDENTITY, - SdkCommandLine.KEY_KEYPASS); - - MakeIdentity mi = new MakeIdentity(account, keystorePath, keystorePass, - aliasName, aliasPass); - try { + String account = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_ACCOUNT); + + String keystorePath = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_KEYSTORE); + + String aliasName = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_ALIAS); + + String keystorePass = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_STOREPASS); + + if (keystorePass == null) { + keystorePass = promptPassword("Keystore Password: ").trim(); + } + + String aliasPass = (String) mSdkCommandLine.getValue( + SdkCommandLine.VERB_CREATE, + SdkCommandLine.OBJECT_IDENTITY, + SdkCommandLine.KEY_KEYPASS); + + if (aliasPass == null) { + aliasPass = promptPassword("Alias Password: ").trim(); + } + + MakeIdentity mi = new MakeIdentity(account, keystorePath, keystorePass, + aliasName, aliasPass); + mi.make(System.out, mSdkLog); } catch (Exception e) { errorAndExit("Unexpected error: %s", e.getMessage()); @@ -1454,6 +1463,45 @@ public class Main { } /** + * Reads a line from the input stream, masking it as much as possible. + */ + private String promptPassword(String prompt) throws IOException { + + // Setup a thread that tries to overwrite any input by + // masking the last character with a space. This is quite + // crude but is a documented workaround to the lack of a + // proper password getter. + final AtomicBoolean keepErasing = new AtomicBoolean(true); + + Thread eraser = new Thread(new Runnable() { + public void run() { + while (keepErasing.get()) { + System.err.print("\b "); //$NON-NLS-1$. \b=Backspace + try { + Thread.sleep(10 /*millis*/); + } catch (InterruptedException e) { + // Ignore + } + } + } + }, "eraser"); //$NON-NLS-1$ + + try { + System.err.print(prompt); + eraser.start(); + byte[] buffer = new byte[256]; + return readLine(buffer); + } finally { + keepErasing.set(false); + try { + eraser.join(); + } catch (InterruptedException e) { + // Ignore + } + } + } + + /** * Returns the boolean value represented by the string. * @throws IOException If the value is not a boolean string. */ diff --git a/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java b/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java index 5be7d38..e334e62 100644 --- a/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java +++ b/sdkmanager/app/src/com/android/sdkmanager/SdkCommandLine.java @@ -412,13 +412,12 @@ class SdkCommandLine extends CommandLineProcessor { define(Mode.STRING, true, VERB_CREATE, OBJECT_IDENTITY, "k", KEY_ALIAS, //$NON-NLS-1$ "The key alias.", null); - define(Mode.STRING, true, + define(Mode.STRING, false, VERB_CREATE, OBJECT_IDENTITY, "p", KEY_STOREPASS, //$NON-NLS-1$ - "The keystore password.", null); - define(Mode.STRING, true, + "The keystore password. Default is to prompt.", null); + define(Mode.STRING, false, VERB_CREATE, OBJECT_IDENTITY, "w", KEY_KEYPASS, //$NON-NLS-1$ - "The alias password.", null); - + "The alias password. Default is to prompt.", null); } @Override |