1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.CryptoCipher;
26 import org.apache.commons.crypto.cipher.CryptoCipherFactory;
27 import org.apache.commons.crypto.random.CryptoRandom;
28 import org.apache.commons.crypto.random.CryptoRandomFactory;
29 import org.apache.commons.crypto.utils.AES;
30
31
32
33
34
35 public final class Crypto {
36
37 private static class ComponentPropertiesHolder {
38
39 static final Properties PROPERTIES = getComponentProperties();
40
41
42
43
44
45
46 private static Properties getComponentProperties() {
47 final URL url = Crypto.class.getResource("/org/apache/commons/crypto/component.properties");
48 final Properties versionData = new Properties();
49 if (url != null) {
50 try (InputStream inputStream = url.openStream()) {
51 versionData.load(inputStream);
52 } catch (final IOException e) {
53 }
54 }
55 return versionData;
56 }
57 }
58
59
60
61
62 public static final String CONF_PREFIX = "commons.crypto.";
63
64
65
66
67
68 public static final String LIB_NAME_KEY = Crypto.CONF_PREFIX + "lib.name";
69
70
71
72
73
74 public static final String LIB_PATH_KEY = Crypto.CONF_PREFIX + "lib.path";
75
76
77
78
79
80 public static final String LIB_TEMPDIR_KEY = Crypto.CONF_PREFIX + "lib.tempdir";
81
82 private static boolean quiet = false;
83
84
85
86
87
88
89
90
91
92
93
94 public static String getComponentName() {
95
96
97 return ComponentPropertiesHolder.PROPERTIES.getProperty("NAME");
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public static String getComponentVersion() {
111
112
113 return ComponentPropertiesHolder.PROPERTIES.getProperty("VERSION");
114 }
115
116
117
118
119
120
121 public static Throwable getLoadingError() {
122 return NativeCodeLoader.getLoadingError();
123 }
124
125
126
127
128
129
130
131 private static void info(final String format, final Object... args) {
132 if (!quiet) {
133 System.out.println(String.format(format, args));
134 }
135 }
136
137
138
139
140
141
142 public static boolean isNativeCodeLoaded() {
143 return NativeCodeLoader.isNativeCodeLoaded();
144 }
145
146
147
148
149
150
151
152 public static void main(final String[] args) throws Exception {
153 quiet = args.length == 1 && args[0].equals("-q");
154 info("%s %s", getComponentName(), getComponentVersion());
155 if (isNativeCodeLoaded()) {
156 info("Native code loaded OK: %s", OpenSslInfoNative.NativeVersion());
157 info("Native name: %s", OpenSslInfoNative.NativeName());
158 info("Native built: %s", OpenSslInfoNative.NativeTimeStamp());
159 info("OpenSSL library loaded OK, version: 0x%s", Long.toHexString(OpenSslInfoNative.OpenSSL()));
160 info("OpenSSL library info: %s", OpenSslInfoNative.OpenSSLVersion(0));
161 info("DLL name: %s", OpenSslInfoNative.DLLName());
162 info("DLL path: %s", OpenSslInfoNative.DLLPath());
163 {
164 final Properties props = new Properties();
165 props.setProperty(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());
166 try (CryptoRandom cryptoRandom = CryptoRandomFactory.getCryptoRandom(props)) {
167 info("Random instance created OK: %s", cryptoRandom);
168 }
169 }
170 {
171 final Properties props = new Properties();
172 props.setProperty(CryptoCipherFactory.CLASSES_KEY, CryptoCipherFactory.CipherProvider.OPENSSL.getClassName());
173 try (CryptoCipher cryptoCipher = CryptoCipherFactory.getCryptoCipher(AES.CTR_NO_PADDING, props)) {
174 info("Cipher %s instance created OK: %s", AES.CTR_NO_PADDING, cryptoCipher);
175 }
176 }
177 info("Additional OpenSSL_version(n) details:");
178 for (int j = 1; j < 6; j++) {
179 info("%s: %s", j, OpenSslInfoNative.OpenSSLVersion(j));
180 }
181 } else {
182 info("Native load failed: %s", getLoadingError());
183 }
184 }
185
186 }