View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.crypto;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.URL;
23  import java.util.Properties;
24  
25  import org.apache.commons.crypto.cipher.CryptoCipherFactory;
26  import org.apache.commons.crypto.random.CryptoRandomFactory;
27  
28  /**
29   * Provides diagnostic information about Commons Crypto and keys for native class loading
30   */
31  public final class Crypto {
32  
33      /**
34       * The prefix of all crypto configuration keys
35       */
36      public static final String CONF_PREFIX = "commons.crypto.";
37  
38      // native lib related configuration keys
39      /**
40       * The configuration key of the path for loading crypto library.
41       */
42      public static final String LIB_PATH_KEY = Crypto.CONF_PREFIX
43              + "lib.path";
44      /**
45       * The configuration key of the file name for loading crypto library.
46       */
47      public static final String LIB_NAME_KEY = Crypto.CONF_PREFIX
48              + "lib.name";
49      /**
50       * The configuration key of temp directory for extracting crypto library.
51       * Defaults to "java.io.tempdir" if not found.
52       */
53      public static final String LIB_TEMPDIR_KEY = Crypto.CONF_PREFIX
54              + "lib.tempdir";
55  
56      private static class ComponentPropertiesHolder {
57          static final Properties PROPERTIES = getComponentProperties();
58  
59          /**
60           * Get component properties from component.properties.
61           *
62           * @return Properties contains project version.
63           */
64          private static Properties getComponentProperties() {
65              URL url = Crypto.class.getResource("/org/apache/commons/crypto/component.properties");
66              if (url != null) {
67                  Properties versionData = new Properties();
68                  try (InputStream openStream = url.openStream()) {
69                      versionData.load(openStream);
70                      return versionData;
71                  } catch (IOException e) {  // NOPMD
72                  }
73              }
74              return new Properties(); // make sure field is not null
75          }
76       }
77  
78      /**
79       * Checks whether the native code has been successfully loaded for the platform.
80       * 
81       * @return true if the native code has been loaded successfully.
82       */
83      public static boolean isNativeCodeLoaded() {
84          return NativeCodeLoader.isNativeCodeLoaded();
85      }
86  
87      /**
88       * The loading error throwable, if loading failed.
89       * 
90       * @return null, unless loading failed.
91       */
92      public static Throwable getLoadingError() {
93          return NativeCodeLoader.getLoadingError();
94      }
95  
96      /**
97       * Gets the component version of Apache Commons Crypto.
98       * <p>
99       * This implementation relies on the VERSION properties file which
100      * must be set up with the correct contents by the build process.
101      * This is done automatically by Maven.
102      *
103      * @return the version; may be null if not found
104      */
105     public static String getComponentVersion() {
106         // Note: the component properties file allows the method to work without needing the jar
107         return ComponentPropertiesHolder.PROPERTIES.getProperty("VERSION");
108     }
109 
110     /**
111      * Gets the component version of Apache Commons Crypto.
112      * <p>
113      * This implementation relies on the VERSION properties file which
114      * must be set up with the correct contents by the build process.
115      * This is done automatically by Maven.
116      *
117      * @return the version; may be null if not found
118      */
119     public static String getComponentName() {
120         // Note: the component properties file allows the method to work without needing the jar
121         return ComponentPropertiesHolder.PROPERTIES.getProperty("NAME");
122     }
123 
124     /**
125      * The Main of Crypto
126      * @param args don't use the args
127      * @throws Exception if getCryptoRandom or getCryptoCipher get error.
128      */
129     public static void main(String args[]) throws Exception {
130         System.out.println(getComponentName() + " " + getComponentVersion());
131         if (isNativeCodeLoaded()) {
132             System.out.println("Native code loaded OK " + OpenSslInfoNative.NativeVersion());
133             System.out.println("Native Name " + OpenSslInfoNative.NativeName());
134             System.out.println("Native Built " + OpenSslInfoNative.NativeTimeStamp());
135             System.out.println("OpenSSL library loaded OK, version: 0x" + Long.toHexString(OpenSslInfoNative.SSLeay()));
136             System.out.println(OpenSslInfoNative.SSLeayVersion(0));
137             {
138                 Properties props = new Properties();
139                 props.setProperty(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());
140                 CryptoRandomFactory.getCryptoRandom(props);
141                 System.out.println("Random instance created OK");
142             }
143             {
144                 Properties props = new Properties();
145                 props.setProperty(CryptoCipherFactory.CLASSES_KEY, CryptoCipherFactory.CipherProvider.OPENSSL.getClassName());
146                 CryptoCipherFactory.getCryptoCipher("AES/CTR/NoPadding", props);
147                 System.out.println("Cipher instance created OK");
148             }
149             System.out.println("Additional SSLeay_version(n) details:");
150             for(int j=1;j<6;j++) {
151                 System.out.println(j+": "+ OpenSslInfoNative.SSLeayVersion(j));
152             }
153         } else {
154             System.out.println("Native load failed: " + getLoadingError());            
155         }
156     }
157 }