|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--iaik.utils.Util
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 int |
calculateHashCode(byte[] value)
Creates an integer suitable for hash table indexing. |
static void |
compareTables(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 AttributeCertificate[] |
convertToAttributeCertificateChain(Certificate[] certificateChain)
This method converts an array of Certificates into an array of AttributeCertificates. |
static void |
copyStream(InputStream source,
OutputStream destination,
byte[] buffer)
Reads all data (until EOF is reached) from the given source to the destination stream. |
static byte[] |
decodeByteArray(String st)
Decode base64 encoded data from the given string and return it as a byte array. |
static int[] |
decodeIntArray(String st)
Decode base64 encoded data from the given string and return it as an int array. |
static Key |
decodeKey(int keyType,
String keyAlgorithm,
byte[] encodedKey)
Construct a key object from and encoded key. |
static String |
encodeByteArray(byte[] b)
Encodes a byte array in base64 and return it as a string. |
static String |
encodeIntArray(int[] a)
Encode an int array in base64 and return it as a string. |
static void |
fillArray(byte[] array,
InputStream is)
This method tries to fill the given array with data read from the InputStream. |
static String |
fromBooleanArray(boolean[] bits)
Converts the given array of boolean values into a string of zeros and ones. |
static int |
getDigestLength(AlgorithmID hashAlgID)
Tries to determine the length of the digest value produced by the given hash algorithm. |
static int |
getKeyLength(PrivateKey key)
Gets the modulus length of a private key. |
static int |
getKeyLength(PublicKey key)
Gets the modulus length of a public key. |
static String |
getRawCipherName(String transformation)
Tries to get the raw name of the cipher specified by the given transformation string. |
static RSAPrivateKey |
getRSAPrivateKey(Key key)
Convert a key to an IAIK RSAPrivateKey. |
static RSAPublicKey |
getRSAPublicKey(Key key)
Convert a key to an IAIK RSAPublicKey. |
static Vector |
getVector(Object[] array)
This method converts an array to a Vector. |
static boolean |
loadClass(String name,
boolean createInstance)
Tries to dynamically load a class. |
static String |
printIndented(String msg,
boolean indentFirst)
Creates a new String from the given (multi line) message where each line is indented with a space " " token. |
static String |
printIndented(String msg,
boolean indentFirst,
String indentToken)
Creates a new String from the given (multi line) message where each line is indented with the given indent token. |
static void |
printIndented(String msg,
boolean indentFirst,
StringBuffer buf)
Prints the given (multi line) message indented to the supplied StringBuffer. |
static void |
printIndented(String msg,
boolean indentFirst,
String indentToken,
StringBuffer buf)
Prints the given (multi line) message indented to the supplied StringBuffer. |
static void |
printTable(String text,
byte[] table)
|
static void |
printTable(String text,
int[] table)
|
static X509Certificate[] |
readCertificateChain(InputStream is)
Tries to read a certificate chain from the given input stream. |
static X509CRL[] |
readCRLChain(InputStream is)
Tries to read a crl list from the given input stream. |
static byte[] |
readFile(String fileName)
Reads the content of a file into a byte array. |
static byte[] |
readStream(InputStream in)
Reads 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 Object[] |
resizeArray(Object[] array,
int len)
Resizes an array of objects to the specified length. |
static void |
saveToFile(byte[] array,
String fileName)
Saves the content of the given byte array to a file. |
static void |
setEncoding(String encoding)
Sets the encoding to be used by ASN.1 String types for String - byte conversion. |
static Object[] |
toArray(Vector aVector)
This method converts a Vector to an array. |
static Object[] |
toArray(Vector aVector,
Class cls)
This method converts a Vector to an array. |
static byte[] |
toASCIIBytes(String s)
Convert the String to a byte[] in the ASCII encoding. |
static String |
toASCIIString(byte[] bytes)
Convert the bytes to a String using the ASCII encoding. |
static String |
toASCIIString(byte[] bytes,
int ofs,
int cnt)
Convert the bytes to a String using the ASCII encoding. |
static boolean[] |
toBooleanArray(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(String s)
Convert the given string with hex values to a byte array. |
static String |
toPemString(Certificate cert)
Convert the given Certificate to a PEM style base64 encoded string. |
static String |
toPemString(CertificateRequest request)
Convert the given Certificate request to a PEM style base64 encoded string. |
static String |
toPemString(PrivateKey privKey)
Convert the given private key to a PEM style base64 encoded string. |
static String |
toPemString(X509CRL crl)
Convert the given CRL to a PEM style base64 encoded string. |
static String |
toString(byte b)
Converts a byte into a hexadecimal string. |
static String |
toString(byte[] byteArray)
Converts a byte array into a hexadecimal string of the format: 01:23:34:56... |
static 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 String |
toString(byte[] byteArray,
int off,
int len,
String delimiter)
Converts the specified subsequence of the given byte array into a hexadecimal string of the format: 01:23:34:56... |
static String |
toString(byte[] byteArray,
String delimiter)
Converts a byte array into a hexadecimal string of the format: 01:23:34:56... |
static String |
toString(int i)
Converts an int into a hexadecimal string of the format: 01:23:34:56... |
static 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 |
public static boolean loadClass(String name, boolean createInstance)
The class to be loaded has to be specified by its name (including the whole package hierachy), e.g.:
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 theUtil.loadClass("iaik.test.testdemo.Test1", true);
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 byte[] readFile(String fileName) throws IOException
fileName
- the name of the file to readIOException
- if an IO error occurspublic static byte[] readStream(InputStream in) throws IOException
in
- the input stream to be readIOException
- if an error occurs while reading the streampublic static void copyStream(InputStream source, OutputStream destination, byte[] buffer) throws IOException
source
- The stream providing the data.destination
- The stream that takes the data. If this is null, all
data from source will be read and discarded.buffer
- The buffer to use for forwarding. If it is null, the method
allocates a buffer.IOException
- If reading from the source or writing to the
destination fails.public static void saveToFile(byte[] array, String fileName) throws 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 writtenIOException
- if an I/O error occurspublic static String toString(byte b)
For instance:
byte b = ...; System.out.println(Util.toString(b));
b
- the byte to convertpublic static 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 String toString(byte[] byteArray, int off, int len, 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 String toString(byte[] byteArray)
For instance:
byte[] arr = ...; System.out.println(Util.toString(arr));
byteArray
- the byte array containing the datapublic static String toString(byte[] byteArray, 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 String toString(int i)
For instance:
int i = ...; System.out.println(Util.toString(i));
i
- the int to convertpublic static String toString(long l)
public static byte[] Base64Encode(byte[] in)
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);
in
- the binary data to be Base64 encodedpublic static byte[] Base64Decode(byte[] in) throws Base64Exception
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);
in
- the Base64 encoded data to be decodedBase64Exception
- if there occurs an error while decodingpublic 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 Object[] resizeArray(Object[] array, int len)
If len
is smaller than the array size some entries are lost.
array
- the array to resizelen
- the new length of the arraypublic static void fillArray(byte[] array, InputStream is) throws IOException
array
- the array to fillis
- the InputStream to read frompublic static Vector getVector(Object[] array)
array
- the array to convertpublic static Object[] toArray(Vector aVector)
aVector
- the Vector to convertpublic static Object[] toArray(Vector aVector, 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(String s)
public static byte[] decodeByteArray(String st) throws RuntimeException
public static String encodeByteArray(byte[] b)
decodeByteArray
.
Base64Encode
for getting the raw base64 encoded byte array.b
- the byte array to be encodedpublic static int[] decodeIntArray(String st) throws RuntimeException
public static String encodeIntArray(int[] a)
public static String fromBooleanArray(boolean[] bits)
bits
- the array of boolean values to be convertedpublic static boolean[] toBooleanArray(String bits)
bits
- the string of bits to be transformed into booleanspublic static String toPemString(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 String toPemString(X509CRL crl)
-----BEGIN CRL----- BASE64DATA -----END CRL-----All lines will be terminated by '\n'. In case of a CRLException null will be returned (should never occur with the IAIK JCE).
public static String toPemString(PrivateKey privKey)
-----BEGIN RSA PRIVATE KEY----- BASE64DATA -----END RSA PRIVATE KEY-----All lines will be terminated by '\n'.
public static String toPemString(CertificateRequest request)
-----BEGIN NEW CERTIFICATE REQUEST----- BASE64DATA -----END NEW CERTIFICATE REQUEST-----All lines will be terminated by '\n'.
public static long[] bubbleSort(long[] a)
public static int[] bubbleSort(int[] a)
public static X509Certificate[] convertCertificateChain(Certificate[] certificateChain) throws CertificateException
certificateChain
- the array of certificates to convertCertificateException
- if there occurs an error while parsing the certificate chainpublic static AttributeCertificate[] convertToAttributeCertificateChain(Certificate[] certificateChain) throws CertificateException
certificateChain
- the array of certificates to convertCertificateException
- 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(InputStream is) throws IOException
is
- the inputstream supplying the certificatesIOException
- if an error occurs while reading/parsing the chainpublic static X509CRL[] readCRLChain(InputStream is) throws IOException
is
- the inputstream supplying the crlsIOException
- if an error occurs while reading/parsing the chainpublic static byte[] convertEndian32(byte[] in)
public static void setEncoding(String encoding)
encoding
- the encoding to be usedpublic static String toASCIIString(byte[] bytes, int ofs, int cnt)
public static String toASCIIString(byte[] bytes)
public static byte[] toASCIIBytes(String s)
public static void compareTables(String text, int[] T0, int[] T1)
public static void printTable(String text, byte[] table)
public static void printTable(String text, int[] table)
public static int calculateHashCode(byte[] value)
value
- the byte array for which a hash code shall be computedpublic static Key decodeKey(int keyType, String keyAlgorithm, byte[] encodedKey) throws NoSuchAlgorithmException, InvalidKeyException
public static RSAPublicKey getRSAPublicKey(Key key) throws InvalidKeyException
public static RSAPrivateKey getRSAPrivateKey(Key key) throws InvalidKeyException
public static int getKeyLength(PublicKey key)
key
- the public keypublic static int getKeyLength(PrivateKey key)
key
- the private keypublic static int getDigestLength(AlgorithmID hashAlgID)
hashAlgID
- the algorithm id of the hash algorithm for which to determine the
digest value lengthpublic static String getRawCipherName(String transformation)
public static String printIndented(String msg, boolean indentFirst)
msg
- the (multi line) message to be printedpublic static String printIndented(String msg, boolean indentFirst, String indentToken)
msg
- the (multi line) message to be printedindentFirst
- whether to indent the first line or leave it unindentedindentToken
- the token which shall be printed in front of each linepublic static void printIndented(String msg, boolean indentFirst, StringBuffer buf)
msg
- the (multi line) message to be printedindentFirst
- whether to indent the first line or leave it unindentedbuf
- the StringBuffer to which to print the messagepublic static void printIndented(String msg, boolean indentFirst, String indentToken, StringBuffer buf)
msg
- the (multi line) message to be printedindentFirst
- whether to indent the first line or leave it unindentedindentToken
- the token which shall be printed in front of each linebuf
- the StringBuffer to which to print the message
|
This Javadoc may contain text parts from Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |