1 /* 2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.security.KeyPairGenerator; 25 import java.security.MessageDigest; 26 import java.security.NoSuchAlgorithmException; 27 import java.security.Signature; 28 import static java.util.Arrays.asList; 29 30 /* 31 * This class is used for returning some specific JDK information. 32 */ 33 public class JdkUtils { 34 35 private enum Alg { 36 KEY, SIG, DIGEST; 37 } 38 39 static final String M_JAVA_VERSION = "javaVersion"; 40 static final String M_JAVA_RUNTIME_VERSION = "javaRuntimeVersion"; 41 static final String M_IS_SUPPORTED_KEYALG = "isSupportedKeyalg"; 42 static final String M_IS_SUPPORTED_SIGALG = "isSupportedSigalg"; 43 static final String M_IS_SUPPORTED_DIGESTALG = "isSupportedDigestalg"; 44 45 // Returns the JDK build version. javaVersion()46 static String javaVersion() { 47 return System.getProperty("java.version"); 48 } 49 50 // Returns the JDK build runtime version. javaRuntimeVersion()51 static String javaRuntimeVersion() { 52 return System.getProperty("java.runtime.version"); 53 } 54 55 // Checks if the specified algorithm is supported by the JDK. isSupportedAlg(Alg algType, String algName)56 static boolean isSupportedAlg(Alg algType, String algName) { 57 try { 58 switch (algType) { 59 case KEY: 60 return KeyPairGenerator.getInstance(algName) != null; 61 case SIG: 62 return Signature.getInstance(algName) != null; 63 case DIGEST: 64 return MessageDigest.getInstance(algName) != null; 65 } 66 } catch (NoSuchAlgorithmException e) { } 67 System.out.println(algName + " is not supported yet."); 68 return false; 69 } 70 main(String[] args)71 public static void main(String[] args) { 72 if (M_JAVA_VERSION.equals(args[0])) { 73 System.out.print(javaVersion()); 74 } else if (M_JAVA_RUNTIME_VERSION.equals(args[0])) { 75 System.out.print(javaRuntimeVersion()); 76 } else if (M_IS_SUPPORTED_KEYALG.equals(args[0])) { 77 System.out.print(isSupportedAlg(Alg.KEY, args[1])); 78 } else if (M_IS_SUPPORTED_SIGALG.equals(args[0])) { 79 System.out.print(isSupportedAlg(Alg.SIG, args[1])); 80 } else if (M_IS_SUPPORTED_DIGESTALG.equals(args[0])) { 81 System.out.print(isSupportedAlg(Alg.DIGEST, args[1])); 82 } else { 83 throw new IllegalArgumentException("invalid: " + asList(args)); 84 } 85 } 86 87 } 88