iaik.utils
Class Util

java.lang.Object
  |
  +--iaik.utils.Util

public class Util
extends java.lang.Object

This class provides some general useful methods.


Method Summary
static X509Certificate[] arrangeCertificateChain(X509Certificate[] certArray, boolean topdown)
          Tries to construct a certificate chain from an array of certificates.
static byte[] Base64Decode(byte[] in)
          Decodes a Base64 encoded byte array.
static byte[] Base64Encode(byte[] in)
          Encodes a binary byte array into Base64.
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 void compareTables(java.lang.String text, int[] T0, int[] T1)
           
static X509Certificate[] convertCertificateChain(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.lang.String encodeByteArray(byte[] b)
          Encode an 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.util.Vector getVector(java.lang.Object[] array)
          This method converts an array to a Vector.
static boolean loadClass(java.lang.String name, boolean createInstance)
          Tries to dynamically load a class.
static void printTable(java.lang.String text, byte[] table)
           
static void printTable(java.lang.String text, int[] table)
           
static byte[] readFile(java.lang.String fileName)
          Reads the content of a file into a byte array.
static byte[] readStream(java.io.InputStream in)
          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 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)
          Convert the String to a byte[] in the ASCII encoding.
static java.lang.String toASCIIString(byte[] bytes)
          Convert the bytes to a String using the ASCII encoding.
static java.lang.String toASCIIString(byte[] bytes, int ofs, int cnt)
          Convert the bytes to a String using the ASCII encoding.
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(Certificate cert)
          Convert the given Certificate to a PEM style base64 encoded string.
static java.lang.String toPemString(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(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 void waitKey()
          Wait for the user to press the return key on System.in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

loadClass

public static boolean loadClass(java.lang.String name,
                                boolean createInstance)
Tries to dynamically load a class.

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.
Parameters:
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;
Returns:
true, if the class has been loaded successfully and a new instance has been created (if desired); false otherwise

readFile

public static byte[] readFile(java.lang.String fileName)
                       throws java.io.IOException
Reads the content of a file into a byte array.
Parameters:
fileName - the name of the file to read
Returns:
the content of the file in a byte array
Throws:
java.io.IOException - if an IO error occurs

readStream

public static byte[] readStream(java.io.InputStream in)
                         throws java.io.IOException
Read the contents of the stream into a byte array. The stream is read until it returns EOF.

saveToFile

public static void saveToFile(byte[] array,
                              java.lang.String fileName)
                       throws java.io.IOException
Saves the content of the given byte array to a file.
Parameters:
array - the byte array supplying the data to be written to the file
fileName - the name of the file to which the data shall be written
Throws:
java.io.IOException - if an I/O error occurs

toString

public static java.lang.String toString(byte b)
Converts a byte into a hexadecimal string.

For instance:

 byte b = ...;
 System.out.println(Util.toString(b));
 
Parameters:
b - the byte to convert
Returns:
the byte as hex value

toString

public 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... .

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));
 
Parameters:
byteArray - the byte array containing the data
off - the offset indicating the start position within the given byte array; the following len bytes will be converted into a hexadecimal string
len - how many bytes shall be written
Returns:
the string representation

toString

public static java.lang.String toString(byte[] byteArray)
Converts a byte array into a hexadecimal string of the format: 01:23:34:56... .

For instance:

 byte[] arr = ...;
 System.out.println(Util.toString(arr));
 
Parameters:
byteArray - the byte array containing the data
Returns:
the string representation

toString

public static java.lang.String toString(int i)
Converts an int into a hexadecimal string of the format: 01:23:34:56... .

For instance:

 int i = ...;
 System.out.println(Util.toString(i));
 
Parameters:
i - the int to convert
Returns:
the int as hexadecimal string representation

toString

public static java.lang.String toString(long l)
Converts an int into a hexadecimal string of the format: 01:23:34:56... . Uses big endian byte order.

Base64Encode

public static byte[] Base64Encode(byte[] in)
Encodes a binary byte array into Base64.

Base64 is the encoding format used by Multipurpose Internet Mail Extension (Mime) for transmitting non-text material over text-only communications channels. Base64 is based on a 65-character subset of US-ASCII, enabling 6 bits to be represented per printable character (see RFC 1521).

This method may be used for Base64 encoding some already DER encoded ASN1 object to get the PEM (Base64 DER) encoding, e.g.:

 ASN1 asn1 = ...;
 //DER encode the ASN1 data structure just created:
 byte[] der_array = asn1.toByteArray();
 //Base64 encode the DER encoded byte array just created to get the
 //PEM encoding:
 byte[] pem_array = Util.Base64Encode(der_array);
 
Parameters:
in - the binary data to be Base64 encoded
Returns:
the Base64 encoded data

Base64Decode

public static byte[] Base64Decode(byte[] in)
                           throws Base64Exception
Decodes a Base64 encoded byte array.

Base64 is the encoding format used by Multipurpose Internet Mail Extension (Mime) for transmitting non-text material over text-only communications channels. Base64 is based on a 65-character subset of US-ASCII, enabling 6 bits to be represented per printable character (see RFC 1521).

For instance:

 byte[] data = ...;
 byte[] encoded_data = Util.Base64Encode(data);
 byte[] decoded_data = Util.Base64Decode(encoded_data);
 
Parameters:
in - the Base64 encoded data to be decoded
Returns:
the decoded data
Throws:
Base64Exception - if there occurs an error while decoding

resizeArray

public static byte[] resizeArray(byte[] array,
                                 int len)
Resizes a byte array to the specified length.

If len is smaller than the array size some bytes are lost.

Parameters:
array - the array to resize
len - the new length of the array
Returns:
the resized array

resizeArray

public static java.lang.Object[] resizeArray(java.lang.Object[] array,
                                             int len)
Resizes a byte array to the specified length.

If len is smaller than the array size some bytes are lost.

Parameters:
array - the array to resize
len - the new length of the array
Returns:
the resized array

fillArray

public static void fillArray(byte[] array,
                             java.io.InputStream is)
                      throws java.io.IOException
This method tries to fill the given array with data read from the InputStream. This method will not return until the array is full or an exception occurs.
Parameters:
array - the array to fill
is - the InputStream to read from

getVector

public static java.util.Vector getVector(java.lang.Object[] array)
This method converts an array to a Vector.
Parameters:
array - the array to convert
Returns:
the created Vector

toArray

public static java.lang.Object[] toArray(java.util.Vector aVector)
This method converts a Vector to an array. ATTENTION: It is not possible to cast the returned array to an type other than (Object[]). To enable casting use the method: @see toArray(Vector, Class);
Parameters:
aVector - the Vector to convert
Returns:
the created array

toArray

public static java.lang.Object[] toArray(java.util.Vector aVector,
                                         java.lang.Class cls)
This method converts a Vector to an array. This method must be used to get an array of a special type. You need this special type for casting an array.
 AType[] test = (AType[])Util.toArray(aVector, AType.class);
 
Without using the specified class a ClassCastException would occur.
Parameters:
aVector - the Vector to convert
cls - specifies the type of the elements
Returns:
the created array

waitKey

public static void waitKey()
Wait for the user to press the return key on System.in.

toByte

public static int toByte(char c,
                         int radix)
Convert the given char to an int. '0' maps to 0, 'a' and 'A' to 10, 'z' and 'Z' to 36, and so on. For all other characters and for resulting values equal to or larger than radix -1 is returned.

toByteArray

public static byte[] toByteArray(java.lang.String s)
Convert the given string with hex values to a byte array. For example "001122" is turned into {0, 0x11, 0x22}. All characters outside the range of '0'-'9', 'a'-'z', and 'A'-'Z' or simply ignored.

decodeByteArray

public static byte[] decodeByteArray(java.lang.String st)
                              throws java.lang.RuntimeException
Decode base64 encoded data from the given string and return it as a byte array. Base64Exceptions cause a RuntimeException.

encodeByteArray

public static java.lang.String encodeByteArray(byte[] b)
Encode an byte array in base64 and return it as a string. It can be decoded again using decodeByteArray.

decodeIntArray

public static int[] decodeIntArray(java.lang.String st)
                            throws java.lang.RuntimeException
Decode base64 encoded data from the given string and return it as an int array. It must have been encoded MSB first (big endian). Base64Exceptions cause a RuntimeException.

encodeIntArray

public static java.lang.String encodeIntArray(int[] a)
Encode an int array in base64 and return it as a string. It can be decoded again using decodeIntArray.

toPemString

public static java.lang.String toPemString(Certificate cert)
Convert the given Certificate to a PEM style base64 encoded string. The returned string will look something like:
 -----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).

toPemString

public static java.lang.String toPemString(PrivateKey privKey)
Convert the given private key to a PEM style base64 encoded string. The returned string will look something like:
 -----BEGIN RSA PRIVATE KEY-----
 BASE64DATA
 -----END RSA PRIVATE KEY-----
 
All lines will be terminated by '\n'.

bubbleSort

public static long[] bubbleSort(long[] a)
Sort the given array of longs in ascending order and return it. This method uses bubble sort which is on average very slow on large array (complexity O(n*n)). However, it is ok to be used on small array, especially if they are 'almost' sorted.

bubbleSort

public static int[] bubbleSort(int[] a)
Sort the given array of ints in ascending order and return it. This method uses bubble sort which is on average very slow on large array (complexity O(n*n)). However, it is ok to be used on small array, especially if they are 'almost' sorted.

convertCertificateChain

public static X509Certificate[] convertCertificateChain(Certificate[] certificateChain)
                                                 throws CertificateException
This method converts an array of Certificates into an array of X509Certificates.
Parameters:
certificateChain - the array of certificates to convert
Returns:
the new certificate chain
Throws:
CertificateException - if there occurs an error while parsing the certificate chain

arrangeCertificateChain

public static X509Certificate[] arrangeCertificateChain(X509Certificate[] certArray,
                                                        boolean topdown)
Tries to construct a certificate chain from an array of certificates. If a certificate chain that contains all certificates from the array can be build the new certificate chain is returned. Otherwise null is returned. This method builds the chain by checking issuer and subject and testing the signatures.
Parameters:
certArray - an array of certificates
topdown - 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.
Returns:
the rearranged certificate chain or null if the array did not contain a certificate chain.

convertEndian32

public static byte[] convertEndian32(byte[] in)
Convert between little and big endian 32 bit formats.

toASCIIString

public static java.lang.String toASCIIString(byte[] bytes,
                                             int ofs,
                                             int cnt)
Convert the bytes to a String using the ASCII encoding.

toASCIIString

public static java.lang.String toASCIIString(byte[] bytes)
Convert the bytes to a String using the ASCII encoding.

toASCIIBytes

public static byte[] toASCIIBytes(java.lang.String s)
Convert the String to a byte[] in the ASCII encoding.

compareTables

public static void compareTables(java.lang.String text,
                                 int[] T0,
                                 int[] T1)

printTable

public static void printTable(java.lang.String text,
                              byte[] table)

printTable

public static void printTable(java.lang.String text,
                              int[] table)