org.apache.commons.crypto.OsInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-crypto Show documentation
Show all versions of commons-crypto Show documentation
Apache Commons Crypto is a cryptographic library optimized with AES-NI (Advanced Encryption
Standard New Instructions). It provides Java API for both cipher level and Java stream level.
Developers can use it to implement high performance AES encryption/decryption with the minimum
code and effort. Please note that Crypto doesn't implement the cryptographic algorithm such as
AES directly. It wraps to Openssl or JCE which implement the algorithms.
Features
--------
1. Cipher API for low level cryptographic operations.
2. Java stream API (CryptoInputStream/CryptoOutputStream) for high level stream encyrption/decryption.
3. Both optimized with high performance AES encryption/decryption. (1400 MB/s - 1700 MB/s throughput in modern Xeon processors).
4. JNI-based implementation to achieve comparable performance to the native C++ version based on OpenSsl.
5. Portable across various operating systems (currently only Linux/MacOSX/Windows);
Apache Commons Crypto loads the library according to your machine environment (it checks system properties, `os.name` and `os.arch`).
6. Simple usage. Add the commons-crypto-(version).jar file to your classpath.
Export restrictions
-------------------
This distribution includes cryptographic software.
The country in which you currently reside may have restrictions
on the import, possession, use, and/or re-export to another country,
of encryption software. BEFORE using any encryption software,
please check your country's laws, regulations and policies
concerning the import, possession, or use, and re-export of
encryption software, to see if this is permitted.
See <http://www.wassenaar.org/> for more information.
The U.S. Government Department of Commerce, Bureau of Industry and Security (BIS),
has classified this software as Export Commodity Control Number (ECCN) 5D002.C.1,
which includes information security software using or performing
cryptographic functions with asymmetric algorithms.
The form and manner of this Apache Software Foundation distribution makes
it eligible for export under the License Exception
ENC Technology Software Unrestricted (TSU) exception
(see the BIS Export Administration Regulations, Section 740.13)
for both object code and source code.
The following provides more details on the included cryptographic software:
* Commons Crypto use [Java Cryptography Extension](http://docs.oracle.com/javase/8/docs/technotes/guides/security/crypto/CryptoSpec.html) provided by Java
* Commons Crypto link to and use [OpenSSL](https://www.openssl.org/) ciphers
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.crypto;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
/**
* Provides OS name and architecture name. Used by the JNI make process to get
* information from the build environment.
*/
final class OsInfo {
private final static HashMap archMapping = new HashMap<>();
/**
* The constant string represents for X86 architecture, the value is:
* {@value #X86}.
*/
static final String X86 = "x86";
/**
* The constant string represents for X86_64 architecture, the value is:
* {@value #X86_64}.
*/
static final String X86_64 = "x86_64";
/**
* The constant string represents for IA64_32 architecture, the value is:
* {@value #IA64_32}.
*/
static final String IA64_32 = "ia64_32";
/**
* The constant string represents for IA64 architecture, the value is:
* {@value #IA64}.
*/
static final String IA64 = "ia64";
/**
* The constant string represents for PPC architecture, the value is:
* {@value #PPC}.
*/
static final String PPC = "ppc";
/**
* The constant string represents for PPC64 architecture, the value is:
* {@value #PPC64}.
*/
static final String PPC64 = "ppc64";
/**
* The private constructor of {@link OsInfo}.
*/
private OsInfo() {
}
static {
// x86 mappings
archMapping.put(X86, X86);
archMapping.put("i386", X86);
archMapping.put("i486", X86);
archMapping.put("i586", X86);
archMapping.put("i686", X86);
archMapping.put("pentium", X86);
// x86_64 mappings
archMapping.put(X86_64, X86_64);
archMapping.put("amd64", X86_64);
archMapping.put("em64t", X86_64);
archMapping.put("universal", X86_64); // Needed for openjdk7 in Mac
// Itanium 64-bit mappings
archMapping.put(IA64, IA64);
archMapping.put("ia64w", IA64);
// Itanium 32-bit mappings, usually an HP-UX construct
archMapping.put(IA64_32, IA64_32);
archMapping.put("ia64n", IA64_32);
// PowerPC mappings
archMapping.put(PPC, PPC);
archMapping.put("power", PPC);
archMapping.put("powerpc", PPC);
archMapping.put("power_pc", PPC);
archMapping.put("power_rs", PPC);
// TODO: PowerPC 64bit mappings
archMapping.put(PPC64, PPC64);
archMapping.put("power64", PPC64);
archMapping.put("powerpc64", PPC64);
archMapping.put("power_pc64", PPC64);
archMapping.put("power_rs64", PPC64);
}
/**
* The main method. This is used by the JNI make processing in Makefile.common
*
* @param args the argv.
*/
public static void main(final String[] args) {
if (args.length >= 1) {
if ("--os".equals(args[0])) {
System.out.print(getOSName());
return;
} else if ("--arch".equals(args[0])) {
System.out.print(getArchName());
return;
}
}
System.out.print(getNativeLibFolderPathForCurrentOS());
}
/**
* Gets the native lib folder.
*
* @return the current OS's native lib folder.
*/
static String getNativeLibFolderPathForCurrentOS() {
return getOSName() + "/" + getArchName();
}
/**
* Gets the OS name.
*
* @return the OS name.
*/
static String getOSName() {
return translateOSNameToFolderName(System.getProperty("os.name"));
}
/**
* Gets the architecture name.
*
* @return the architecture name.
*/
static String getArchName() {
// if running Linux on ARM, need to determine ABI of JVM
final String osArch = System.getProperty("os.arch");
if (osArch.startsWith("arm") && System.getProperty("os.name").contains("Linux")) {
final String javaHome = System.getProperty("java.home");
try {
// determine if first JVM found uses ARM hard-float ABI
final String[] cmdarray = { "/bin/sh", "-c",
"find '" + javaHome + "' -name 'libjvm.so' | head -1 | xargs readelf -A | "
+ "grep 'Tag_ABI_VFP_args: VFP registers'" };
final int exitCode = Runtime.getRuntime().exec(cmdarray).waitFor();
if (exitCode == 0) {
return "armhf";
}
} catch (final IOException e) { // NOPMD
// ignored: fall back to "arm" arch (soft-float ABI)
} catch (final InterruptedException e) { // NOPMD
// ignored: fall back to "arm" arch (soft-float ABI)
}
} else {
final String lc = osArch.toLowerCase(Locale.US);
if (archMapping.containsKey(lc)) {
return archMapping.get(lc);
}
}
return translateArchNameToFolderName(osArch);
}
/**
* Translates the OS name to folder name.
*
* @param osName the OS name.
* @return the folder name.
*/
private static String translateOSNameToFolderName(final String osName) {
if (osName.contains("Windows")) {
return "Windows";
} else if (osName.contains("Mac")) {
return "Mac";
} else if (osName.contains("Linux")) {
return "Linux";
} else if (osName.contains("AIX")) {
return "AIX";
} else {
return osName.replaceAll("\\W", "");
}
}
/**
* Translates the architecture name to folder name.
*
* @param archName the architecture name.
* @return the folder name.
*/
private static String translateArchNameToFolderName(final String archName) {
return archName.replaceAll("\\W", "");
}
}