Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4f6f0f92 authored by Ying Wang's avatar Ying Wang Committed by Gerrit Code Review
Browse files

Merge "If a console doesn't exist, read password from stdin."

parents ac39bef4 50c7c5a7
Loading
Loading
Loading
Loading
+16 −5
Original line number Diff line number Diff line
@@ -167,20 +167,31 @@ class SignApk {
    }

    /**
     * Reads the password from console and returns it as a string.
     * If a console doesn't exist, reads the password from stdin
     * If a console exists, reads the password from console and returns it as a string.
     *
     * @param keyFile The file containing the private key.  Used to prompt the user.
     */
    private static String readPassword(File keyFile) {
        Console console;
        char[] pwd;
        if((console = System.console()) != null &&
           (pwd = console.readPassword("[%s]", "Enter password for " + keyFile)) != null){
        if ((console = System.console()) == null) {
            System.out.print("Enter password for " + keyFile + " (password will not be hidden): ");
            System.out.flush();
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
            try {
                return stdin.readLine();
            } catch (IOException ex) {
                return null;
            }
        } else {
            if ((pwd = console.readPassword("[%s]", "Enter password for " + keyFile)) != null) {
                return String.valueOf(pwd);
            } else {
                return null;
            }
        }
    }

    /**
     * Decrypt an encrypted PKCS#8 format private key.