Skip navigation links

Oracle Fusion Middleware Crypto Java API Reference for Oracle Security Developer Tools
11g Release 1 (11.1.1)

E10668-05


oracle.security.crypto.util
Class Utils

java.lang.Object
  extended by oracle.security.crypto.util.Utils


public class Utils
extends java.lang.Object

A set of utility functions. All conversions use network (big-endian) order.


Constructor Summary
Utils()
           

 

Method Summary
static boolean areEqual(byte[] a, byte[] b)
          Compares two byte arrays for equality.
static boolean areEqual(byte[] a, int aOff, byte[] b, int bOff, int len)
          Compares two byte arrays for equality from the given offsets over the given length.
static void bytesToChars(byte[] b, char[] c)
           
static void bytesToLongs(byte[] b, int bOff, long[] l, int lOff, int lLen)
          Converts an array of bytes into an array of longs.
static short bytesToShort(int b1, int b2)
          Constructs a short value from two bytes, assuming a big-endian representation.
static void bytesToShorts(byte[] b, short[] s)
          Converts an array of bytes into an array of shorts.
static java.lang.String bytesToUnicode(byte[] b)
           
static int bytesToWord(int b1, int b2, int b3, int b4)
          Constructs an int value from four bytes, assuming a big-endian representation.
static void bytesToWords(byte[] b, int[] w)
          Converts an array of bytes into an array of ints.
static void bytesToWords(byte[] b, int bOff, int[] w, int wOff, int wLen)
          Converts an array of bytes into an array of ints.
static void charsToBytes(char[] c, byte[] b)
          Converts an array of 16-bit chararacters into an array of bytes.
static java.util.Date daysFrom(java.util.Date d, int days)
           
static java.util.Date daysFromNow(int days)
           
static void dumpHex(java.io.PrintStream out, byte[] data)
          Dumps a two-column hex/printable char mapping of a bytestream to the specified output stream, like "hexdump" programs of yore.
static java.lang.String formatDate(java.util.Date d)
           
static java.lang.String formatDate(java.util.Date d, boolean longYear)
           
static byte[] fromBase64(java.lang.String s)
           Returns the decoded form of the given Base-64 encoded string.
static byte[] fromHexString(java.lang.String s)
          Converts the specified hex string into an array of bytes.
static java.lang.String fromURLEncoded(java.lang.String s)
           
static java.lang.String fromUTF8(byte[] bytes)
          Creates a string by decoding the given UTF-8 representation.
static int hashBytes(Streamable o)
           
static int input24bit(java.io.InputStream in)
          Reads three bytes from the given input stream and uses them to construct an int value, assuming a big-endian representation.
static java.math.BigInteger inputBignum(int nBytes, java.io.InputStream is)
          Input an unsigned integer.
static byte inputByte(java.io.InputStream is)
          Reads one byte from the given input stream.
static byte[] inputByteArray(byte[] buf, java.io.InputStream is)
          Fills up the specified byte array by reading from the input stream, then returns that byte array.
static void inputByteArray(byte[] buf, int off, int len, java.io.InputStream is)
          Fills up the specified byte array by reading from the input stream, then returns that byte array.
static byte[] inputBytes(java.io.InputStream is, int len)
          Reads the indicated number of bytes from the given input stream.
static short inputShort(java.io.InputStream in)
          Reads two bytes from the given input stream and uses them to construct a short value, assuming a big-endian representation.
static int inputWord(java.io.InputStream in)
          Reads four bytes from the given input stream and uses them to construct an int value, assuming a big-endian representation.
static int length(oracle.security.crypto.core.math.BigInt val)
          Returns the number of bytes needed to represent a given nonnegative integer, not including the sign bit.
static int length(java.math.BigInteger val)
          Returns the number of bytes needed to represent a given nonnegative integer, not including the sign bit.
static void longsToBytes(long[] l, byte[] b)
          Converts an array of long values into an array of bytes.
static void longsToBytes(long[] l, int lOff, byte[] b, int bOff, int lLen)
          Converts an array of long values into an array of bytes.
static byte[] longToBytes(long l)
          Returns the big endian representation of the given long value, as an array of eight bytes.
static java.util.Date minutesFrom(java.util.Date d, int minutes)
           
static void output(java.math.BigInteger n, java.io.OutputStream out)
          Outputs the big endian representation of the given big integer to the specified output stream.
static void output(int w, java.io.OutputStream out)
          Outputs the big endian representation of the given int value to the specified output stream.
static void output(long l, java.io.OutputStream out)
          Outputs the big endian representation of the given long value to the specified output stream.
static void output(short s, java.io.OutputStream out)
          Outputs the big endian representation of the given short value to the specified output stream.
static void output24bit(int w, java.io.OutputStream out)
          Outputs the big endian representation of least significant 24 bits of the given int value to the specified output stream.
static void outputBignum(java.math.BigInteger val, java.io.OutputStream os)
          Output an unsigned integer.
static void outputLong(long l, java.io.OutputStream out)
          Outputs the big endian representation of the given long value to the specified output stream.
static void outputShort(int s, java.io.OutputStream out)
          Outputs the big endian representation of the given short value to the specified output stream.
static void outputString(java.lang.String s, java.io.OutputStream os)
          Deprecated.  
static void outputWord(int w, java.io.OutputStream out)
          Outputs the big endian representation of the given int value to the specified output stream.
static java.util.Date parseDate(java.lang.String s)
           
static java.util.Date parseDate(java.lang.String s, boolean longYear)
           
static java.lang.String plural(int n, java.lang.String noun)
           
static int rotl(int x, int s)
          Performs a left bitwise rotation.
static short rotlShort(int x, int s)
          Performs a left bitwise rotation.
static int rotr(int x, int s)
          Performs a right bitwise rotation.
static short rotrShort(int x, int s)
          Performs a right bitwise rotation.
static void setArray(byte[] arr, byte b)
          Fills the specified byte array with a given value.
static void setArray(byte[] arr, int off, int len, byte b)
           
static void setArray(int[] arr, int w)
          Fills the specified int array with a given value.
static void setArray(short[] arr, short s)
          Fills the specified short array with a given value.
static void shortsToBytes(short[] s, byte[] b)
          Converts an array of short values into an array of bytes.
static byte[] shortToBytes(short s)
          Returns the big endian representation of the given short value, as an array of two bytes.
static java.lang.String stripEOL(java.lang.String s)
          Returns a copy of the given string with all CR and LF characters removed.
static java.lang.String toBase64(byte[] data)
           Returns the Base-64 encoding of the given byte array.
static java.lang.String toBase64(byte[] data, boolean insertSeparators)
           Returns the Base-64 encoding of the given byte array.
static java.lang.String toBase64(byte[] data, java.lang.String lineSeparator)
           
static byte[] toByteArray(oracle.security.crypto.core.math.BigInt val)
          Convert an unsigned integer to a big-endian unsigned representation.
static void toByteArray(oracle.security.crypto.core.math.BigInt val, byte[] buf, int off, int len)
          Convert an unsigned integer to a big-endian unsigned representation of fixed length.
static byte[] toByteArray(java.math.BigInteger val)
          Convert an unsigned integer to a big-endian unsigned representation.
static void toByteArray(java.math.BigInteger val, byte[] buf, int off, int len)
          Convert an unsigned integer to a big-endian unsigned representation of fixed length.
static byte[] toBytes(java.io.InputStream is)
          Returns characters from the given stream until end-of-stream is reached, and stores the read characters into a byte array.
static byte[] toBytes(Streamable o)
          Stores the output of a Streamable object into a byte array.
static java.lang.String toHexString(byte[] b)
          Returns the hex representation of the specified array of bytes.
static java.lang.String toHexString(byte[] b, int off, int len)
          Returns the hex representation of the specified sequence of bytes.
static java.io.InputStream toStream(Streamable o)
           
static java.lang.String toString(java.util.List<?> v)
          Produces a string representation of the input Vector in a format consistent with the rest of the oracle.security.crypto. classes.
static java.lang.String toString(java.util.Vector<?> v)
          Deprecated. use toString(List) instead
static java.lang.String toURLEncoded(java.lang.String s)
           
static byte[] toUTF8(java.lang.String s)
          Returns the UTF-8 representation of the given string.
static byte[] unicodeToBytes(java.lang.String s)
           
static void wordsToBytes(int[] w, byte[] b)
          Converts an array of int values into an array of bytes.
static void wordsToBytes(int[] w, int wOff, byte[] b, int bOff, int wLen)
          Converts an array of int values into an array of bytes.
static byte[] wordToBytes(int w)
          Returns the big endian representation of the given int value, as an array of four bytes.
static byte[] xor(byte[] a, byte[] b)
          Utility function that performs a bitwise exclusive-or of two byte arrays.

 

Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

 

Constructor Detail

Utils

public Utils()

Method Detail

rotlShort

public static short rotlShort(int x,
                              int s)
Performs a left bitwise rotation. Only the 16 least significant bits are rotated, which are returned as a short.
Parameters:
x - value to rotate
s - number of bits to rotate
Returns:
x left-rotated s times.

rotrShort

public static short rotrShort(int x,
                              int s)
Performs a right bitwise rotation. Only the 16 least significant bits are rotated, which are returned as a short.
Parameters:
x - value to rotate
s - number of bits to rotate
Returns:
x left-rotated s times.

setArray

public static void setArray(byte[] arr,
                            byte b)
Fills the specified byte array with a given value.

setArray

public static void setArray(short[] arr,
                            short s)
Fills the specified short array with a given value.

setArray

public static void setArray(int[] arr,
                            int w)
Fills the specified int array with a given value.

setArray

public static void setArray(byte[] arr,
                            int off,
                            int len,
                            byte b)

rotl

public static int rotl(int x,
                       int s)
Performs a left bitwise rotation. All 32 bits are rotated.
Parameters:
x - value to rotate
s - number of bits to rotate
Returns:
x left-rotated s times.

rotr

public static int rotr(int x,
                       int s)
Performs a right bitwise rotation. All 32 bits are rotated.
Parameters:
x - value to rotate
s - number of bits to rotate
Returns:
x right-rotated s times.

bytesToWord

public static int bytesToWord(int b1,
                              int b2,
                              int b3,
                              int b4)
Constructs an int value from four bytes, assuming a big-endian representation.
Parameters:
b1 - The most significant 8 bits.
b2 - The next 8 bits.
b3 - The next 8 bits.
b4 - The least significant 8 bits.

bytesToShort

public static short bytesToShort(int b1,
                                 int b2)
Constructs a short value from two bytes, assuming a big-endian representation.
Parameters:
b1 - The most significant 8 bits.
b2 - The least significant 8 bits.

bytesToWords

public static void bytesToWords(byte[] b,
                                int bOff,
                                int[] w,
                                int wOff,
                                int wLen)
Converts an array of bytes into an array of ints.

bytesToWords

public static void bytesToWords(byte[] b,
                                int[] w)
Converts an array of bytes into an array of ints.

bytesToLongs

public static void bytesToLongs(byte[] b,
                                int bOff,
                                long[] l,
                                int lOff,
                                int lLen)
Converts an array of bytes into an array of longs.

bytesToShorts

public static void bytesToShorts(byte[] b,
                                 short[] s)
Converts an array of bytes into an array of shorts.

bytesToChars

public static void bytesToChars(byte[] b,
                                char[] c)

longToBytes

public static byte[] longToBytes(long l)
Returns the big endian representation of the given long value, as an array of eight bytes.

wordToBytes

public static byte[] wordToBytes(int w)
Returns the big endian representation of the given int value, as an array of four bytes.

shortToBytes

public static byte[] shortToBytes(short s)
Returns the big endian representation of the given short value, as an array of two bytes.

longsToBytes

public static void longsToBytes(long[] l,
                                byte[] b)
Converts an array of long values into an array of bytes.

longsToBytes

public static void longsToBytes(long[] l,
                                int lOff,
                                byte[] b,
                                int bOff,
                                int lLen)
Converts an array of long values into an array of bytes.

wordsToBytes

public static void wordsToBytes(int[] w,
                                int wOff,
                                byte[] b,
                                int bOff,
                                int wLen)
Converts an array of int values into an array of bytes.

wordsToBytes

public static void wordsToBytes(int[] w,
                                byte[] b)
Converts an array of int values into an array of bytes.

shortsToBytes

public static void shortsToBytes(short[] s,
                                 byte[] b)
Converts an array of short values into an array of bytes.

charsToBytes

public static void charsToBytes(char[] c,
                                byte[] b)
Converts an array of 16-bit chararacters into an array of bytes.

unicodeToBytes

public static byte[] unicodeToBytes(java.lang.String s)

bytesToUnicode

public static java.lang.String bytesToUnicode(byte[] b)

toByteArray

public static byte[] toByteArray(java.math.BigInteger val)
Convert an unsigned integer to a big-endian unsigned representation.

toByteArray

public static void toByteArray(java.math.BigInteger val,
                               byte[] buf,
                               int off,
                               int len)
                        throws java.lang.ArithmeticException
Convert an unsigned integer to a big-endian unsigned representation of fixed length.
Throws:
java.lang.ArithmeticException

length

public static int length(java.math.BigInteger val)
Returns the number of bytes needed to represent a given nonnegative integer, not including the sign bit.
Parameters:
val - the integer value, which must be nonnegative

toByteArray

public static byte[] toByteArray(oracle.security.crypto.core.math.BigInt val)
Convert an unsigned integer to a big-endian unsigned representation.

toByteArray

public static void toByteArray(oracle.security.crypto.core.math.BigInt val,
                               byte[] buf,
                               int off,
                               int len)
                        throws java.lang.ArithmeticException
Convert an unsigned integer to a big-endian unsigned representation of fixed length.
Throws:
java.lang.ArithmeticException

length

public static int length(oracle.security.crypto.core.math.BigInt val)
Returns the number of bytes needed to represent a given nonnegative integer, not including the sign bit.
Parameters:
val - the integer value, which must be nonnegative

areEqual

public static boolean areEqual(byte[] a,
                               byte[] b)
Compares two byte arrays for equality. Simple byte comparison.

areEqual

public static boolean areEqual(byte[] a,
                               int aOff,
                               byte[] b,
                               int bOff,
                               int len)
Compares two byte arrays for equality from the given offsets over the given length. Simple byte comparison.

plural

public static java.lang.String plural(int n,
                                      java.lang.String noun)

fromUTF8

public static java.lang.String fromUTF8(byte[] bytes)
Creates a string by decoding the given UTF-8 representation. Will correctly read both the UTF-8 representation defined in Unicode version 2.0 (including surrogate pairs), and the "modified" UTF-8 produced by java.io.DataOutputStream.
Throws:
InvalidFormatException - if the encoding is malformed. Note that this is a runtime exception. If the encoding consists only of ASCII values (0..127), this exception cannot occur.

toUTF8

public static byte[] toUTF8(java.lang.String s)
Returns the UTF-8 representation of the given string. This is the true UTF-8 representation, not the "modified" UTF-8 used by java.io.DataInputStream and java.io.DataOutputStream.

inputWord

public static int inputWord(java.io.InputStream in)
                     throws java.io.IOException
Reads four bytes from the given input stream and uses them to construct an int value, assuming a big-endian representation.
Throws:
java.io.IOException

input24bit

public static int input24bit(java.io.InputStream in)
                      throws java.io.IOException
Reads three bytes from the given input stream and uses them to construct an int value, assuming a big-endian representation.
Throws:
java.io.IOException

inputShort

public static short inputShort(java.io.InputStream in)
                        throws java.io.IOException
Reads two bytes from the given input stream and uses them to construct a short value, assuming a big-endian representation.
Throws:
java.io.IOException

inputByte

public static byte inputByte(java.io.InputStream is)
                      throws java.io.IOException
Reads one byte from the given input stream.
Throws:
java.io.IOException

inputBytes

public static byte[] inputBytes(java.io.InputStream is,
                                int len)
                         throws java.io.IOException
Reads the indicated number of bytes from the given input stream.
Throws:
java.io.IOException

inputByteArray

public static byte[] inputByteArray(byte[] buf,
                                    java.io.InputStream is)
                             throws java.io.IOException
Fills up the specified byte array by reading from the input stream, then returns that byte array.
Throws:
java.io.IOException

inputByteArray

public static void inputByteArray(byte[] buf,
                                  int off,
                                  int len,
                                  java.io.InputStream is)
                           throws java.io.IOException
Fills up the specified byte array by reading from the input stream, then returns that byte array.
Throws:
java.io.IOException

toBytes

public static byte[] toBytes(java.io.InputStream is)
                      throws java.io.IOException
Returns characters from the given stream until end-of-stream is reached, and stores the read characters into a byte array.
Parameters:
is - the input stream
Returns:
the characters read
Throws:
java.io.IOException

output

public static void output(long l,
                          java.io.OutputStream out)
                   throws java.io.IOException
Outputs the big endian representation of the given long value to the specified output stream.
Throws:
java.io.IOException

output

public static void output(int w,
                          java.io.OutputStream out)
                   throws java.io.IOException
Outputs the big endian representation of the given int value to the specified output stream.
Throws:
java.io.IOException

output

public static void output(short s,
                          java.io.OutputStream out)
                   throws java.io.IOException
Outputs the big endian representation of the given short value to the specified output stream.
Throws:
java.io.IOException

output

public static void output(java.math.BigInteger n,
                          java.io.OutputStream out)
                   throws java.io.IOException
Outputs the big endian representation of the given big integer to the specified output stream.
Throws:
java.io.IOException

outputLong

public static void outputLong(long l,
                              java.io.OutputStream out)
                       throws java.io.IOException
Outputs the big endian representation of the given long value to the specified output stream.
Throws:
java.io.IOException

outputWord

public static void outputWord(int w,
                              java.io.OutputStream out)
                       throws java.io.IOException
Outputs the big endian representation of the given int value to the specified output stream.
Throws:
java.io.IOException

output24bit

public static void output24bit(int w,
                               java.io.OutputStream out)
                        throws java.io.IOException
Outputs the big endian representation of least significant 24 bits of the given int value to the specified output stream.
Throws:
java.io.IOException

outputShort

public static void outputShort(int s,
                               java.io.OutputStream out)
                        throws java.io.IOException
Outputs the big endian representation of the given short value to the specified output stream.
Throws:
java.io.IOException

outputBignum

public static void outputBignum(java.math.BigInteger val,
                                java.io.OutputStream os)
                         throws java.io.IOException
Output an unsigned integer. Most significant byte comes first.
Throws:
java.io.IOException

inputBignum

public static java.math.BigInteger inputBignum(int nBytes,
                                               java.io.InputStream is)
                                        throws java.io.IOException
Input an unsigned integer. Most significant byte comes first.
Parameters:
nBytes - the length of the input
Throws:
java.io.IOException

outputString

public static void outputString(java.lang.String s,
                                java.io.OutputStream os)
                         throws java.io.IOException
Deprecated. 
Writes the given string to the specified output stream, using the system default character encoding.
Throws:
java.io.IOException

dumpHex

public static void dumpHex(java.io.PrintStream out,
                           byte[] data)
Dumps a two-column hex/printable char mapping of a bytestream to the specified output stream, like "hexdump" programs of yore.

toHexString

public static java.lang.String toHexString(byte[] b,
                                           int off,
                                           int len)
Returns the hex representation of the specified sequence of bytes. Each byte is converted into exactly two hex digits.
Parameters:
b - the byte array
off - where to start
len - number of bytes

toHexString

public static java.lang.String toHexString(byte[] b)
Returns the hex representation of the specified array of bytes. Each byte is converted into exactly two hex digits.

fromHexString

public static byte[] fromHexString(java.lang.String s)
Converts the specified hex string into an array of bytes. Each pair of characters in the string represents one byte.
Throws:
java.lang.NumberFormatException - if the string is not correctly formed (i.e., contains non-hexadecimal digits or has length not evenly divisible by 2).

toBase64

public static java.lang.String toBase64(byte[] data)

Returns the Base-64 encoding of the given byte array. The result will contain line separators.

Base-64 (a.k.a. PEM printable encoding) is described in RFC 1421.

Parameters:
data - The data to be encoded.

toBase64

public static java.lang.String toBase64(byte[] data,
                                        boolean insertSeparators)

Returns the Base-64 encoding of the given byte array. A flag is used to indicate whether line separators are to be inserted; if so, then the value of the system property line.separator is used for this purpose.

Base-64 (a.k.a. PEM printable encoding) is described in RFC 1421.

Parameters:
data - The data to be encoded.
insertSeparators - If true, then the result will contain line separators.

toBase64

public static java.lang.String toBase64(byte[] data,
                                        java.lang.String lineSeparator)

fromBase64

public static byte[] fromBase64(java.lang.String s)

Returns the decoded form of the given Base-64 encoded string.

Line breaks will be ignored, provided they are of the form CR, LF, or CR LF, and do not occur twice in a row or in the middle of a four-character block.

Base-64 (a.k.a. PEM printable encoding) is described in RFC 1421.

Parameters:
s - The string to be decoded.

stripEOL

public static java.lang.String stripEOL(java.lang.String s)
Returns a copy of the given string with all CR and LF characters removed.

fromURLEncoded

public static java.lang.String fromURLEncoded(java.lang.String s)

toURLEncoded

public static java.lang.String toURLEncoded(java.lang.String s)

parseDate

public static java.util.Date parseDate(java.lang.String s)

parseDate

public static java.util.Date parseDate(java.lang.String s,
                                       boolean longYear)

formatDate

public static java.lang.String formatDate(java.util.Date d)

formatDate

public static java.lang.String formatDate(java.util.Date d,
                                          boolean longYear)

daysFrom

public static java.util.Date daysFrom(java.util.Date d,
                                      int days)

daysFromNow

public static java.util.Date daysFromNow(int days)

minutesFrom

public static java.util.Date minutesFrom(java.util.Date d,
                                         int minutes)

toBytes

public static byte[] toBytes(Streamable o)
Stores the output of a Streamable object into a byte array.
Throws:
StreamableOutputException - If an I/O error unexpectedly occurs.

toStream

public static java.io.InputStream toStream(Streamable o)

hashBytes

public static int hashBytes(Streamable o)

toString

public static java.lang.String toString(java.util.Vector<?> v)
Deprecated. use toString(List) instead
Produces a string representation of the input Vector in a format consistent with the rest of the oracle.security.crypto. classes.

toString

public static java.lang.String toString(java.util.List<?> v)
Produces a string representation of the input Vector in a format consistent with the rest of the oracle.security.crypto. classes.

xor

public static byte[] xor(byte[] a,
                         byte[] b)
Utility function that performs a bitwise exclusive-or of two byte arrays.

If the byte arrays are of different length, the shorter one is xor'd into the trailing bytes of the larger one and the larger one's leading bytes are left unchanged.

If byte array a or byte array b are null, a NullPointerException will be thrown.

Parameters:
a - The first byte array to be xor'd.
b - The second byte array to be xor'd.
Returns:
A new array that is the result of a xor b. It will be the size of the larger of the two parameter arrays.

Skip navigation links

Oracle Fusion Middleware Crypto Java API Reference for Oracle Security Developer Tools
11g Release 1 (11.1.1)

E10668-05


Copyright © 2005, 2013, Oracle. All rights reserved.