public class Util
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static int |
INI_CERT_SIZE
Limit used for the INI file certificate size
|
static java.lang.String |
ISO8859_1
The ISO8859-1 encoding string used by the platform.
|
| Modifier and Type | Method and Description |
|---|---|
static X509Certificate[] |
arrangeCertificateChain(X509Certificate[] certArray,
boolean topdown)
Tries to construct a certificate chain from an array of certificates.
|
static byte[] |
Base64Decode(byte[] in)
Base64 decodes the provided input data (byte representation of a Base64
encoded value).
|
static byte[] |
Base64Decode(byte[] in,
boolean setIgnoreInvalidCharacters)
Base64 decodes the provided input data (byte representation of a Base64
encoded value).
|
static byte[] |
Base64DecodeFromString(java.lang.String in)
Base64 decodes the provided input data (string representation of a Base64
encoded value).
|
static byte[] |
Base64DecodeFromString(java.lang.String in,
boolean ignoreInvalidCharacters)
Base64 decodes the provided input data (string representation of a Base64
encoded value).
|
static byte[] |
Base64Encode(byte[] in)
Base64 encodes (with line breaks) the provided input data and returns the
byte representation of the Base64 encoded value.
|
static byte[] |
Base64Encode(byte[] in,
boolean includeLineBreaks)
Base64 encodes (with or without line breaks) the provided input data and
returns the byte representation of the Base64 encoded value.
|
static java.lang.String |
Base64EncodeToString(byte[] in)
Base64 encodes (with line breaks) the provided input data and returns the
string representation of the Base64 encoded value.
|
static java.lang.String |
Base64EncodeToString(byte[] in,
boolean includeLineBreaks)
Base64 encodes (with or without line breaks) the provided input data and
returns the string representation of the Base64 encoded value.
|
static int[] |
bubbleSort(int[] a)
Sort the given array of ints in ascending order and return it.
|
static long[] |
bubbleSort(long[] a)
Sort the given array of longs in ascending order and return it.
|
static int |
calculateHashCode(byte[] value)
Creates an integer suitable for hash table indexing.
|
static void |
compareTables(java.lang.String text,
int[] T0,
int[] T1) |
static X509Certificate[] |
convertCertificateChain(java.security.cert.Certificate[] certificateChain)
This method converts an array of Certificates into an array of X509Certificates.
|
static byte[] |
convertEndian32(byte[] in)
Convert between little and big endian 32 bit formats.
|
static byte[] |
decodeByteArray(java.lang.String st)
Decode base64 encoded data from the given string and
return it as a byte array.
|
static int[] |
decodeIntArray(java.lang.String st)
Decode base64 encoded data from the given string
and return it as an int array.
|
static java.security.Key |
decodeKey(int keyType,
java.lang.String keyAlgorithm,
byte[] encodedKey)
Construct a key object from and encoded key.
|
static boolean |
doCheckWritePermission()
This checks the value of the
com.entrust.toolkit.NoWritePermissionCheck system
property. |
static java.lang.String |
encodeByteArray(byte[] b)
Encodes a byte array in base64 and return it as a string.
|
static java.lang.String |
encodeIntArray(int[] a)
Encode an int array in base64 and return it as a string.
|
static void |
fillArray(byte[] array,
java.io.InputStream is)
This method tries to fill the given array with data read from the InputStream.
|
static java.lang.String |
fromBooleanArray(boolean[] bits)
Converts the given array of boolean values into a string of zeros and ones.
|
static java.lang.String |
getASCIIEncoding()
Returns the name of the character encoding that should be used for ASCII
character/byte conversions.
|
static java.lang.String |
getPath(java.lang.String pathLocation)
Get a valid path for writing to a Java properties file.
|
static RSAPrivateKey |
getRSAPrivateKey(java.security.Key key)
Convert a key to an IAIK RSAPrivateKey.
|
static RSAPublicKey |
getRSAPublicKey(java.security.Key key)
Convert a key to an IAIK RSAPublicKey.
|
static java.util.Vector |
getVector(java.lang.Object[] array)
This method converts an array to a Vector.
|
static byte[] |
intToNetworkBytes(int toConvert)
Converts an int to an array of 4 bytes that represent the integer,
in big-endian order.
|
static boolean |
loadClass(java.lang.String name,
boolean createInstance)
Tries to dynamically load a class.
|
static int |
networkBytesToInt(byte[] toConvert)
Converts an array of 4 bytes to an integer.
|
static void |
printTable(java.lang.String text,
byte[] table) |
static void |
printTable(java.lang.String text,
int[] table) |
static X509Certificate[] |
readCertificateChain(java.io.InputStream is)
Tries to read a certificate chain from the given input stream.
|
static X509CRL[] |
readCRLChain(java.io.InputStream is)
Tries to read a crl list from the given input stream.
|
static byte[] |
readFile(java.lang.String fileName)
Reads the content of a file into a byte array.
|
static X509Certificate[] |
readPEMCerts(java.io.InputStream location,
int MaxSize)
Reads X509Certificates in a PEM format into an array of
X509Certificates.
|
static byte[] |
readStream(java.io.InputStream in)
Read the contents of the stream into a byte array.
|
static byte[] |
readStream(java.io.InputStream in,
int maxsize)
Read the contents of the stream into a byte array.
|
static byte[] |
resizeArray(byte[] array,
int len)
Resizes a byte array to the specified length.
|
static java.lang.Object[] |
resizeArray(java.lang.Object[] array,
int len)
Resizes a byte array to the specified length.
|
static void |
saveToFile(byte[] array,
java.lang.String fileName)
Saves the content of the given byte array to a file.
|
static void |
setEncoding(java.lang.String encoding)
Deprecated.
since 8.0; by default 'US-ASCII' is used which (according to
Sun) must be supported by all implementations of the Java 2
platform, Standard Edition, v. 1.3 or higher
|
static java.lang.Object[] |
toArray(java.util.Vector aVector)
This method converts a Vector to an array.
|
static java.lang.Object[] |
toArray(java.util.Vector aVector,
java.lang.Class cls)
This method converts a Vector to an array.
|
static byte[] |
toASCIIBytes(java.lang.String s)
Encodes a
String to a sequence of bytes using the configured
ASCII character encoding. |
static java.lang.String |
toASCIIString(byte[] bytes)
Decodes a
String from a sequence of bytes using the
configured ASCII character encoding. |
static java.lang.String |
toASCIIString(byte[] bytes,
int offset,
int length)
Decodes a
String from a sequence of bytes using the
configured ASCII character encoding. |
static boolean[] |
toBooleanArray(java.lang.String bits)
Transforms the characters of the given bit string into boolean values and returns
the given string as an array of boolean values.
|
static int |
toByte(char c,
int radix)
Convert the given char to an int.
|
static byte[] |
toByteArray(java.lang.String s)
Convert the given string with hex values to a byte array.
|
static java.lang.String |
toPemString(java.security.cert.Certificate cert)
Convert the given Certificate to a PEM style base64 encoded string.
|
static java.lang.String |
toPemString(java.security.PrivateKey privKey)
Convert the given private key to a PEM style base64 encoded string.
|
static java.lang.String |
toString(byte b)
Converts a byte into a hexadecimal string.
|
static java.lang.String |
toString(byte[] byteArray)
Converts a byte array into a hexadecimal string of the format: 01:23:34:56...
|
static java.lang.String |
toString(byte[] byteArray,
int off,
int len)
Converts the specified subsequence of the given byte array into a hexadecimal
string of the format: 01:23:34:56...
|
static java.lang.String |
toString(byte[] byteArray,
int off,
int len,
java.lang.String delimiter)
Converts the specified subsequence of the given byte array into a hexadecimal
string of the format: 01:23:34:56...
|
static java.lang.String |
toString(byte[] byteArray,
java.lang.String delimiter)
Converts a byte array into a hexadecimal string of the format: 01:23:34:56...
|
static java.lang.String |
toString(int i)
Converts an int into a hexadecimal string of the format: 01:23:34:56...
|
static java.lang.String |
toString(long l)
Converts an int into a hexadecimal string of the format: 01:23:34:56...
|
static byte[] |
toUTF8Bytes(java.lang.String s)
Encodes a
String to a sequence of bytes using the 'UTF-8'
character encoding. |
static java.lang.String |
toUTF8String(byte[] ba)
Decodes a
String from a sequence of bytes using the 'UTF-8'
character encoding. |
static void |
waitKey()
Wait for the user to press the return key on System.in.
|
static void |
writePEMCerts(java.io.OutputStream os,
X509Certificate[] x509certs)
Write the specifed array of certificates to the specified file location.
|
public static final int INI_CERT_SIZE
public static java.lang.String ISO8859_1
public static boolean loadClass(java.lang.String name,
boolean createInstance)
The class to be loaded has to be specified by its name (including the whole package hierachy), e.g.:
Util.loadClass("iaik.test.testdemo.Test1", true);
For actually creating a new instance of the requested class - some
implementations of the Java-VM only execute the static part of a class
if a new instance is created - set the createInstance
parameter to true.name - the name of the class to load (e.g. "iaik.Test")createInstance - if set to true a new instance of the
specified class is created;true, if the class has been loaded successfully and a new
instance has been created (if desired); false
otherwisepublic static void writePEMCerts(java.io.OutputStream os,
X509Certificate[] x509certs)
throws java.io.IOException,
java.security.cert.CertificateEncodingException
-----BEGIN CERTIFICATE----- MIIDRzCCArCgAwIBAgIEO5kvRTANBgkqhkiG9w0BAQUFADBQMQswCQYDVQQGEwJV UzEQMA4GA1UEChMHRW50cnVzdDEvMC0GA1UECxMmRW50cnVzdCBQS0kgRGVtb25z ... w4KN8Iz+E5BTly+vJkP7WCOcchXg8aDxI8kCySlkxqJ+hcX7/hdvOzEAPkkJRboz VonUPwEk+elHGXQDcr5nXllPwl9UzFkgrxBZ= -----END CERTIFICATE----- -----BEGIN CERTIFICATE----- MYICLzCCAiswggGUoAMCAQICBgDu11ycbTANBgkqhkiG9w0BAQUFADA0MRAwDgYD VQQKEwdlbnRydXN0MQwwCgYDVQQLEwNlbmcxEjAQBgNVBAMTCWdhTG9jYWxDQTAe ... yZGEu7pnpwyAmN4IAiz6wPROqPHOgJ65FjTE+OCvtm9qVodrp3CpibG10Q45sVH7 H5/2RuVOFh8EVZJiF0ACttn95xdW6LRTNiSKzqlyK4+4vNI= -----END CERTIFICATE-----
os - The output streamx509certs - An array of X509Certificate objectsjava.io.IOExceptionjava.security.cert.CertificateEncodingExceptionpublic static X509Certificate[] readPEMCerts(java.io.InputStream location, int MaxSize) throws java.io.IOException, java.security.cert.CertificateException, Base64Exception
-----BEGIN CERTIFICATE----- MIIDRzCCArCgAwIBAgIEO5kvRTANBgkqhkiG9w0BAQUFADBQMQswCQYDVQQGEwJV UzEQMA4GA1UEChMHRW50cnVzdDEvMC0GA1UECxMmRW50cnVzdCBQS0kgRGVtb25z ... w4KN8Iz+E5BTly+vJkP7WCOcchXg8aDxI8kCySlkxqJ+hcX7/hdvOzEAPkkJRboz VonUPwEk+elHGXQDcr5nXllPwl9UzFkgrxBZ -----END CERTIFICATE----- -----BEGIN CERTIFICATE----- MYICLzCCAiswggGUoAMCAQICBgDu11ycbTANBgkqhkiG9w0BAQUFADA0MRAwDgYD VQQKEwdlbnRydXN0MQwwCgYDVQQLEwNlbmcxEjAQBgNVBAMTCWdhTG9jYWxDQTAe ... yZGEu7pnpwyAmN4IAiz6wPROqPHOgJ65FjTE+OCvtm9qVodrp3CpibG10Q45sVH7 H5/2RuVOFh8EVZJiF0ACttn95xdW6LRTNiSKzqlyK4+4vNI= -----END CERTIFICATE-----
location - The inputstream that contains the PEM encoded certificatesMaxSize - The approximate number of bytes which should be read from the file. A
very large file could cause an OutOfMemoryException.X509Certificatejava.security.cert.CertificateException - if the was a problem reading the certificateBase64Exception - if there was a problem decoding the Base64 encoded certificate datajava.io.IOException - if an I/O error occured when reading the certificates from the filepublic static java.lang.String getPath(java.lang.String pathLocation)
dir/sub1/sub2/sub3/
The String returned should be separated by the '/' character as this can be used across all platforms. The end of the directory should end with a '/'.pathLocation - The path to be converted to proper formatpublic static byte[] readFile(java.lang.String fileName)
throws java.io.IOException
fileName - the name of the file to readjava.io.IOException - if an IO error occurspublic static byte[] readStream(java.io.InputStream in)
throws java.io.IOException
java.io.IOExceptionpublic static byte[] readStream(java.io.InputStream in,
int maxsize)
throws java.io.IOException
java.io.IOExceptionpublic static void saveToFile(byte[] array,
java.lang.String fileName)
throws java.io.IOException
array - the byte array supplying the data to be written to the filefileName - the name of the file to which the data shall be writtenjava.io.IOException - if an I/O error occurspublic static java.lang.String toString(byte b)
For instance:
byte b = ...; System.out.println(Util.toString(b));
b - the byte to convertpublic static java.lang.String toString(byte[] byteArray,
int off,
int len)
Starting at off, len bytes will be converted and
returned as hexadecimal string, e.g.:
byte[] arr = ...; System.out.println(Util.toString(arr, 0, 3));
byteArray - the byte array containing the dataoff - the offset indicating the start position within the given byte array; the
following len bytes will be converted into a hexadecimal stringlen - how many bytes shall be writtenpublic static java.lang.String toString(byte[] byteArray,
int off,
int len,
java.lang.String delimiter)
Starting at off, len bytes will be converted and
returned as hexadecimal string, e.g.:
byte[] arr = ...; System.out.println(Util.toString(arr, 0, 3));
byteArray - the byte array containing the dataoff - the offset indicating the start position within the given byte array; the
following len bytes will be converted into a hexadecimal stringlen - how many bytes shall be writtendelimiter - the delimiter to be used for separating the hex values, e.g. ":"
(specify "" for not using a delimiter)public static java.lang.String toString(byte[] byteArray)
For instance:
byte[] arr = ...; System.out.println(Util.toString(arr));
byteArray - the byte array containing the datapublic static java.lang.String toString(byte[] byteArray,
java.lang.String delimiter)
For instance:
byte[] arr = ...; System.out.println(Util.toString(arr));
byteArray - the byte array containing the datadelimiter - the delimiter to be used for separating the hex values, e.g. ":"
(specify "" for not using a delimiter)public static java.lang.String toString(int i)
For instance:
int i = ...; System.out.println(Util.toString(i));
i - the int to convertpublic static java.lang.String toString(long l)
public static byte[] Base64Encode(byte[] in)
A line break (CRLF) is inserted after every 64th character; this style of Base64 encoding is used in Privacy Enhanced Mail (PEM).
in - input data (data to be encoded)public static byte[] Base64Encode(byte[] in,
boolean includeLineBreaks)
If configured to do so via includeLineBreaks, a line break
(CRLF) is inserted after every 64th character; this style of Base64
encoding is used in Privacy Enhanced Mail (PEM).
in - input data (data to be encoded)includeLineBreaks - indicates whether or not line breaks should be included in the
Base64 encoded valuepublic static java.lang.String Base64EncodeToString(byte[] in)
A line break (CRLF) is inserted after every 64th character; this style of Base64 encoding is used in Privacy Enhanced Mail (PEM).
in - input data (data to be encoded)public static java.lang.String Base64EncodeToString(byte[] in,
boolean includeLineBreaks)
If configured to do so via includeLineBreaks, a line break
(CRLF) is inserted after every 64th character; this style of Base64
encoding is used in Privacy Enhanced Mail (PEM).
in - input data (data to be encoded)includeLineBreaks - indicates whether or not line breaks should be included in the
Base64 encoded valuepublic static byte[] Base64Decode(byte[] in,
boolean setIgnoreInvalidCharacters)
throws Base64Exception
Line breaks (CRLF) are always ignored (skipped). If configured to do so
via ignoreInvalidCharacters, invalid characters are also
ignored.
in - Base64 encoded data in byte representationsetIgnoreInvalidCharacters - indicates whether or not invalid characters should be ignored
in the Base64 encoded valueBase64Exception - if an error occurs while Bas64 decoding; indicates invalid
Bas64 encoded input datapublic static byte[] Base64Decode(byte[] in)
throws Base64Exception
Line breaks (CRLF) are always ignored (skipped); invalid characters are also ignored.
in - Base64 encoded data in byte representationBase64Exception - if an error occurs while Bas64 decoding; indicates invalid
Bas64 encoded input datapublic static byte[] Base64DecodeFromString(java.lang.String in)
throws Base64Exception
Line breaks (CRLF) are always ignored (skipped); invalid characters are also ignored.
in - Base64 encoded data in string representationjava.lang.NullPointerException - if the input, in, is nullBase64Exception - if an error occurs while Base64 decoding; indicates invalid
Base64 encoded input datapublic static byte[] Base64DecodeFromString(java.lang.String in,
boolean ignoreInvalidCharacters)
throws Base64Exception
Line breaks (CRLF) are always ignored (skipped). If configured to do so
via ignoreInvalidCharacters, invalid characters are also
ignored.
in - Base64 encoded data in string representationignoreInvalidCharacters - indicates whether or not invalid characters should be ignored
in the Base64 encoded valuejava.lang.NullPointerException - if the input, in, is nullBase64Exception - if an error occurs while Base64 decoding; indicates invalid
Base64 encoded input datapublic static byte[] resizeArray(byte[] array,
int len)
If len is smaller than the array size some bytes are lost.
array - the array to resizelen - the new length of the arraypublic static java.lang.Object[] resizeArray(java.lang.Object[] array,
int len)
If len is smaller than the array size some bytes are lost.
array - the array to resizelen - the new length of the arraypublic static void fillArray(byte[] array,
java.io.InputStream is)
throws java.io.IOException
array - the array to fillis - the InputStream to read fromjava.io.IOExceptionpublic static java.util.Vector getVector(java.lang.Object[] array)
array - the array to convertpublic static java.lang.Object[] toArray(java.util.Vector aVector)
aVector - the Vector to convertpublic static java.lang.Object[] toArray(java.util.Vector aVector,
java.lang.Class cls)
AType[] test = (AType[])Util.toArray(aVector, AType.class);Without using the specified class a ClassCastException would occur.
aVector - the Vector to convertcls - specifies the type of the elementspublic static void waitKey()
public static int toByte(char c,
int radix)
public static byte[] toByteArray(java.lang.String s)
public static byte[] decodeByteArray(java.lang.String st)
throws java.lang.RuntimeException
java.lang.RuntimeExceptionpublic static java.lang.String encodeByteArray(byte[] b)
decodeByteArray.
Base64Encode for getting the raw base64 encoded byte array.b - the byte array to be encodedpublic static int[] decodeIntArray(java.lang.String st)
throws java.lang.RuntimeException
java.lang.RuntimeExceptionpublic static java.lang.String encodeIntArray(int[] a)
public static java.lang.String fromBooleanArray(boolean[] bits)
bits - the array of boolean values to be convertedpublic static boolean[] toBooleanArray(java.lang.String bits)
bits - the string of bits to be transformed into booleanspublic static java.lang.String toPemString(java.security.cert.Certificate cert)
-----BEGIN CERTIFICATE----- BASE64DATA -----END CERTIFICATE-----All lines will be terminated by '\n'. In case of a CertificateEncodingException null will be returned (should never occur with the IAIK JCE).
public static java.lang.String toPemString(java.security.PrivateKey privKey)
-----BEGIN RSA PRIVATE KEY----- BASE64DATA -----END RSA PRIVATE KEY-----All lines will be terminated by '\n'.
public static long[] bubbleSort(long[] a)
public static int[] bubbleSort(int[] a)
public static boolean doCheckWritePermission()
com.entrust.toolkit.NoWritePermissionCheck system
property. If the value is not set, or is equal to anything other than case insensitive "true"
then this method will return true. If the value of the system property is case insensitive "true"
then the method will return false.public static X509Certificate[] convertCertificateChain(java.security.cert.Certificate[] certificateChain) throws java.security.cert.CertificateException
certificateChain - the array of certificates to convertjava.security.cert.CertificateException - if there occurs an error while parsing the certificate chainpublic static X509Certificate[] arrangeCertificateChain(X509Certificate[] certArray, boolean topdown)
null is returned. This method builds the chain by checking
issuer and subject and testing the signatures.certArray - an array of certificatestopdown - if true the certificate chain will be returned with the
top-CA-certificate at index 0, otherwise with the
top-CA-certificate at the array's last index.null if the array did not
contain a certificate chain.public static X509Certificate[] readCertificateChain(java.io.InputStream is) throws java.io.IOException
is - the inputstream supplying the certificatesjava.io.IOException - if an error occurs while reading/parsing the chainpublic static X509CRL[] readCRLChain(java.io.InputStream is) throws java.io.IOException
is - the inputstream supplying the crlsjava.io.IOException - if an error occurs while reading/parsing the chainpublic static byte[] convertEndian32(byte[] in)
public static void setEncoding(java.lang.String encoding)
encoding - the encoding to be usedpublic static java.lang.String getASCIIEncoding()
public static java.lang.String toASCIIString(byte[] bytes,
int offset,
int length)
String from a sequence of bytes using the
configured ASCII character encoding.
Refer to setEncoding(String) for details on how to configure the ASCII
character encoding.
bytes - the bytes to be decoded into charactersoffset - the index of the first byte to decodelength - the number of bytes to decodepublic static java.lang.String toASCIIString(byte[] bytes)
String from a sequence of bytes using the
configured ASCII character encoding.
Refer to setEncoding(String) for details on how to configure the ASCII
character encoding.
bytes - a byte array (representing encoded characters)public static byte[] toASCIIBytes(java.lang.String s)
String to a sequence of bytes using the configured
ASCII character encoding.
Refer to setEncoding(String) for details on how to configure the ASCII
character encoding.
s - a stringpublic static java.lang.String toUTF8String(byte[] ba)
String from a sequence of bytes using the 'UTF-8'
character encoding.ba - a byte array (representing encoded characters)public static byte[] toUTF8Bytes(java.lang.String s)
String to a sequence of bytes using the 'UTF-8'
character encoding.s - a stringpublic static void compareTables(java.lang.String text,
int[] T0,
int[] T1)
public static void printTable(java.lang.String text,
byte[] table)
public static void printTable(java.lang.String text,
int[] table)
public static int calculateHashCode(byte[] value)
value - the byte array for which a hash code shall be computedpublic static java.security.Key decodeKey(int keyType,
java.lang.String keyAlgorithm,
byte[] encodedKey)
throws java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException
java.security.NoSuchAlgorithmExceptionjava.security.InvalidKeyExceptionpublic static RSAPublicKey getRSAPublicKey(java.security.Key key) throws java.security.InvalidKeyException
java.security.InvalidKeyExceptionpublic static RSAPrivateKey getRSAPrivateKey(java.security.Key key) throws java.security.InvalidKeyException
java.security.InvalidKeyExceptionpublic static byte[] intToNetworkBytes(int toConvert)
toConvert - the integer to convert to bytespublic static int networkBytesToInt(byte[] toConvert)
toConvert - the byte array to convert to an integertoConvert