net.handle.hdllib
Class Util

java.lang.Object
  extended by net.handle.hdllib.Util

public abstract class Util
extends java.lang.Object


Field Summary
static byte CASE_DIFF
           
 
Constructor Summary
Util()
           
 
Method Summary
static boolean checkJavaVersion()
           
static int countValuesOfType(HandleValue[] values, byte[] type)
           
static java.lang.String decodeHexString(byte[] buf, boolean formatNicely)
           
static java.lang.String decodeHexString(byte[] buf, int offset, int len, boolean formatNicely)
           
static java.lang.String decodeString(byte[] buf)
           
static java.lang.String decodeString(byte[] buf, int offset, int len)
           
static byte[] decrypt(byte[] ciphertext, byte[] secretKey)
          Decrypt the given set of bytes using the specified secret key
static byte[] decrypt(java.security.PrivateKey privKey, byte[] ciphertext)
           
static byte[] doDigest(byte digestType, byte[] buf)
           
static byte[] doDigest(byte digestAlg, byte[] buf1, byte[] buf2, byte[] buf3, byte[] buf4)
           
static byte[] doMD5Digest(byte[] buf)
           
static byte[] doMD5Digest(byte[] buf1, byte[] buf2, byte[] buf3, byte[] buf4)
           
static byte[] doSHA1Digest(byte[] buf)
           
static byte[] doSHA1Digest(byte[] buf1, byte[] buf2, byte[] buf3, byte[] buf4)
           
static byte[] duplicateByteArray(byte[] buf)
           
static byte[] encodeHexString(java.lang.String s)
           
static byte[] encodeString(java.lang.String s)
          Encoded the specified string into a byte array
static byte[] encrypt(byte[] cleartext, byte[] secretKey)
          Encrypt the given set of bytes using the specified secret key and the default encryption algorithm.
static byte[] encrypt(byte[] cleartext, byte[] secretKey, int encType)
          Encrypt the given set of bytes using the specified secret key and encryption algorithm.
static byte[] encrypt(java.security.PublicKey encryptingKey, byte[] secretKey)
          encrypt with Public key
static byte[] encryptIfPossible(byte[] cleartext, byte[] secretKey)
          I know, I know, "encrypt if possible?!?!".
static boolean equals(byte[] b1, byte[] b2)
          compare the two arrays.
static boolean equals(byte[] b1, int b1Start, byte[] b2, int b2Start)
          compare the two arrays starting at the given index.
static boolean equalsCI(byte[] b1, byte[] b2)
          Determine if the first parameter equals the second parameter in a case insensitive comparison.
static boolean equalsCI(byte[] b1, int b1Len, byte[] b2, int b2Len)
          Determine if the first parameter equals the second parameter in a case insensitive manner over the given length.
static byte[] getBytesFromFile(java.io.File file)
           
static byte[] getBytesFromFile(java.lang.String file)
          convert a file into a byte stream
static byte[] getBytesFromPrivateKey(java.security.PrivateKey key)
           
static byte[] getBytesFromPublicKey(java.security.PublicKey key)
           
static byte[] getHashAlgIdFromSigId(java.lang.String signatureAlgorithm)
          Get the ID that the handle system uses to identify the hash algorithm used in the given signature algorithm descriptor.
static byte[] getIDPart(byte[] handle)
          Deprecated. use getSuffixPart(byte[]) instead
static byte[] getNAHandle(byte[] handle)
          Deprecated. use getZeroNAHandle(byte[]) instead
static NamespaceInfo getNamespaceFromValues(HandleValue[] values)
          Extract and return the namespace information contained in the given handle values.
static byte[] getNAPart(byte[] handle)
          Deprecated. use getPrefixPart(byte[]) instead
static byte[] getParentNAOfNAHandle(byte[] naHandle)
          Get the parent naming authority handle for the given naming authority handle.
static byte[] getPassphrase(java.lang.String prompt)
          Get a passphrase from the user.
static byte[] getPrefixPart(byte[] handle)
          Get only the prefix part of this handle.
static java.security.PrivateKey getPrivateKeyFromBytes(byte[] pkBuf, int offset)
           
static java.security.PrivateKey getPrivateKeyFromFileWithPassphrase(java.io.File privKeyFile, java.lang.String passphrase)
           
static java.security.PublicKey getPublicKeyFromBytes(byte[] pkBuf, int offset)
           
static java.security.PublicKey getPublicKeyFromFile(java.lang.String filename)
           
static java.lang.String getSigIdFromHashAlgId(byte[] hashAlgId, java.lang.String sigKeyType)
           
static SiteInfo[] getSitesFromValues(HandleValue[] values)
          Extract and return all of the SiteInfo records from the given list of handle values.
static byte[] getSuffixPart(byte[] handle)
          Get only the suffix part of this handle.
static byte[] getZeroNAHandle(byte[] handle)
          Get the 0.NA authority handle that applies to the specified handle
static boolean hasSlash(byte[] handle)
          Return whether a handle has a slash
static int indexOf(byte[] b, byte ch)
           
static boolean isInArray(byte[][] a, byte[] val)
          returns true if the given byte array is contained in the specified byte array array.
static boolean isInArray(int[] a, int val)
          returns true if the given int value is in the specified array.
static boolean isMatchingKeyPair(java.security.PublicKey pubkey, java.security.PrivateKey privkey)
          check that a given PublicKey and a given PrivateKey are a pair
static boolean isParentTypeInArray(byte[][] a, byte[] val)
          Types in the array are either exact types (not ending in '.') or prefixes of type families (ending in '.').
static boolean isSubNAHandle(byte[] handle)
          only for 0.NA/derived.prefix handles
static boolean isValidString(byte[] buf, int offset, int len)
          Returns true is the given byte array represents a valid text string in the encoding used by the handle system (utf8).
static boolean looksLikeBinary(byte[] buf)
           
static SiteInfo[] orderSitesByPreference(SiteInfo[] sites)
          rearranges the given sites in a more efficient order so that resolution from the current location should tend to access the faster sites first.
static boolean requiresSecretKey(byte[] ciphertext)
          Returns true is the given ciphertext requires a secret key to be decrypted (ie if the encryption algorithm is ENCRYPT_NONE).
static void sortNumberArray(java.lang.Number[] a)
           
static boolean startsWith(byte[] b1, byte[] b2)
           
static boolean startsWithCI(byte[] b1, byte[] b2)
          Determine if the first parameter begins with the second parameter in a case insensitive comparison.
static byte[] substring(byte[] b, int i1)
          determine if the second UTF8 encoded parameter begins with the second parameter in a case sensitive comparison.
static byte[] substring(byte[] b, int i1, int i2)
           
static byte[] upperCase(byte[] b)
          create and return an upper-case copy of the given UTF8 byte array
static byte[] upperCaseInPlace(byte[] b)
          Convert all of the characters in the given utf-8 byte array to upper case.
static boolean writeBytesToFile(java.io.File file, byte[] keyBytes)
           
static boolean writeBytesToFile(java.lang.String file, byte[] keyBytes)
          write byte array into a given file name
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CASE_DIFF

public static final byte CASE_DIFF
See Also:
Constant Field Values
Constructor Detail

Util

public Util()
Method Detail

looksLikeBinary

public static final boolean looksLikeBinary(byte[] buf)

duplicateByteArray

public static final byte[] duplicateByteArray(byte[] buf)

decodeHexString

public static final java.lang.String decodeHexString(byte[] buf,
                                                     int offset,
                                                     int len,
                                                     boolean formatNicely)

decodeHexString

public static final java.lang.String decodeHexString(byte[] buf,
                                                     boolean formatNicely)

encodeHexString

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

encodeString

public static final byte[] encodeString(java.lang.String s)
Encoded the specified string into a byte array


decodeString

public static final java.lang.String decodeString(byte[] buf)

decodeString

public static final java.lang.String decodeString(byte[] buf,
                                                  int offset,
                                                  int len)

isValidString

public static final boolean isValidString(byte[] buf,
                                          int offset,
                                          int len)
Returns true is the given byte array represents a valid text string in the encoding used by the handle system (utf8).


hasSlash

public static final boolean hasSlash(byte[] handle)
Return whether a handle has a slash


getIDPart

@Deprecated
public static final byte[] getIDPart(byte[] handle)
Deprecated. use getSuffixPart(byte[]) instead

Get only the suffix part of this handle.


getNAPart

@Deprecated
public static final byte[] getNAPart(byte[] handle)
Deprecated. use getPrefixPart(byte[]) instead

Get only the prefix part of this handle.


getNAHandle

@Deprecated
public static final byte[] getNAHandle(byte[] handle)
Deprecated. use getZeroNAHandle(byte[]) instead

Get the 0.NA authority handle that applies to the specified handle


getZeroNAHandle

public static final byte[] getZeroNAHandle(byte[] handle)
Get the 0.NA authority handle that applies to the specified handle


isSubNAHandle

public static final boolean isSubNAHandle(byte[] handle)
only for 0.NA/derived.prefix handles


getParentNAOfNAHandle

public static final byte[] getParentNAOfNAHandle(byte[] naHandle)
Get the parent naming authority handle for the given naming authority handle. The given handle MUST be a naming authority handle of form 0.NA/derived.prefix.


getPrefixPart

public static final byte[] getPrefixPart(byte[] handle)
Get only the prefix part of this handle.


getSuffixPart

public static final byte[] getSuffixPart(byte[] handle)
Get only the suffix part of this handle.


startsWith

public static final boolean startsWith(byte[] b1,
                                       byte[] b2)

equals

public static final boolean equals(byte[] b1,
                                   byte[] b2)
compare the two arrays. If they are the same true is returned.


equals

public static final boolean equals(byte[] b1,
                                   int b1Start,
                                   byte[] b2,
                                   int b2Start)
compare the two arrays starting at the given index. If they are the same true is returned.


upperCase

public static final byte[] upperCase(byte[] b)
create and return an upper-case copy of the given UTF8 byte array


upperCaseInPlace

public static final byte[] upperCaseInPlace(byte[] b)
Convert all of the characters in the given utf-8 byte array to upper case. Return the same array.


equalsCI

public static final boolean equalsCI(byte[] b1,
                                     byte[] b2)
Determine if the first parameter equals the second parameter in a case insensitive comparison.


equalsCI

public static final boolean equalsCI(byte[] b1,
                                     int b1Len,
                                     byte[] b2,
                                     int b2Len)
Determine if the first parameter equals the second parameter in a case insensitive manner over the given length.


startsWithCI

public static final boolean startsWithCI(byte[] b1,
                                         byte[] b2)
Determine if the first parameter begins with the second parameter in a case insensitive comparison.


substring

public static final byte[] substring(byte[] b,
                                     int i1)
determine if the second UTF8 encoded parameter begins with the second parameter in a case sensitive comparison.


substring

public static final byte[] substring(byte[] b,
                                     int i1,
                                     int i2)

indexOf

public static final int indexOf(byte[] b,
                                byte ch)

countValuesOfType

public static final int countValuesOfType(HandleValue[] values,
                                          byte[] type)

isParentTypeInArray

public static final boolean isParentTypeInArray(byte[][] a,
                                                byte[] val)
Types in the array are either exact types (not ending in '.') or prefixes of type families (ending in '.'). Returns true when the given type is equal to an exact type in the array, or is equal to a prefix (ignoring the '.'), or has a prefix ending with '.' in the array. For example: isParentInArray( { "url.", "email", "public_key" }, "url.us" ) returns true isParentInArray( { "url", "email", "public_key" }, "url.us" ) returns false isParentInArray( { "url.jp", "email", "public_key" }, "url" ) returns false


isInArray

public static final boolean isInArray(int[] a,
                                      int val)
returns true if the given int value is in the specified array.


isInArray

public static final boolean isInArray(byte[][] a,
                                      byte[] val)
returns true if the given byte array is contained in the specified byte array array.


getSitesFromValues

public static SiteInfo[] getSitesFromValues(HandleValue[] values)
Extract and return all of the SiteInfo records from the given list of handle values. Returns null if no site values were found.


getNamespaceFromValues

public static NamespaceInfo getNamespaceFromValues(HandleValue[] values)
Extract and return the namespace information contained in the given handle values. If there are multiple values with type HS_NAMESPACE then the one with the lowest index value will be used. If no valid namespace values are encountered then this will return null.


orderSitesByPreference

public static final SiteInfo[] orderSitesByPreference(SiteInfo[] sites)
rearranges the given sites in a more efficient order so that resolution from the current location should tend to access the faster sites first. If a preferred site is listed in the server configuration file, it is accessed first.


getPassphrase

public static final byte[] getPassphrase(java.lang.String prompt)
                                  throws java.lang.Exception
Get a passphrase from the user.

Throws:
java.lang.Exception

getHashAlgIdFromSigId

public static byte[] getHashAlgIdFromSigId(java.lang.String signatureAlgorithm)
                                    throws HandleException
Get the ID that the handle system uses to identify the hash algorithm used in the given signature algorithm descriptor.

Throws:
HandleException

getSigIdFromHashAlgId

public static java.lang.String getSigIdFromHashAlgId(byte[] hashAlgId,
                                                     java.lang.String sigKeyType)
                                              throws HandleException
Throws:
HandleException

getBytesFromPrivateKey

public static byte[] getBytesFromPrivateKey(java.security.PrivateKey key)
                                     throws java.lang.Exception
Throws:
java.lang.Exception

getPrivateKeyFromBytes

public static java.security.PrivateKey getPrivateKeyFromBytes(byte[] pkBuf,
                                                              int offset)
                                                       throws java.io.IOException,
                                                              HandleException,
                                                              java.security.spec.InvalidKeySpecException
Throws:
java.io.IOException
HandleException
java.security.spec.InvalidKeySpecException

getBytesFromPublicKey

public static byte[] getBytesFromPublicKey(java.security.PublicKey key)
                                    throws java.lang.Exception
Throws:
java.lang.Exception

getPublicKeyFromFile

public static java.security.PublicKey getPublicKeyFromFile(java.lang.String filename)
                                                    throws java.lang.Exception
Throws:
java.lang.Exception

getPublicKeyFromBytes

public static java.security.PublicKey getPublicKeyFromBytes(byte[] pkBuf,
                                                            int offset)
                                                     throws java.lang.Exception
Throws:
java.lang.Exception

encrypt

public static byte[] encrypt(byte[] cleartext,
                             byte[] secretKey)
                      throws java.lang.Exception
Encrypt the given set of bytes using the specified secret key and the default encryption algorithm.

Throws:
java.lang.Exception

encrypt

public static byte[] encrypt(byte[] cleartext,
                             byte[] secretKey,
                             int encType)
                      throws java.lang.Exception
Encrypt the given set of bytes using the specified secret key and encryption algorithm.

Throws:
java.lang.Exception

encryptIfPossible

public static byte[] encryptIfPossible(byte[] cleartext,
                                       byte[] secretKey)
                                throws java.lang.Exception
I know, I know, "encrypt if possible?!?!". Yes, this is for storing private keys, and if the crypto library isn't available, then we will just store the private key in the clear (with a warning to stderr of course).

Throws:
java.lang.Exception

requiresSecretKey

public static final boolean requiresSecretKey(byte[] ciphertext)
                                       throws java.lang.Exception
Returns true is the given ciphertext requires a secret key to be decrypted (ie if the encryption algorithm is ENCRYPT_NONE).

Throws:
java.lang.Exception

decrypt

public static byte[] decrypt(byte[] ciphertext,
                             byte[] secretKey)
                      throws java.lang.Exception
Decrypt the given set of bytes using the specified secret key

Throws:
java.lang.Exception

doSHA1Digest

public static final byte[] doSHA1Digest(byte[] buf)
                                 throws HandleException
Throws:
HandleException

doSHA1Digest

public static final byte[] doSHA1Digest(byte[] buf1,
                                        byte[] buf2,
                                        byte[] buf3,
                                        byte[] buf4)
                                 throws HandleException
Throws:
HandleException

doMD5Digest

public static final byte[] doMD5Digest(byte[] buf)
                                throws HandleException
Throws:
HandleException

doMD5Digest

public static final byte[] doMD5Digest(byte[] buf1,
                                       byte[] buf2,
                                       byte[] buf3,
                                       byte[] buf4)
                                throws HandleException
Throws:
HandleException

doDigest

public static final byte[] doDigest(byte digestAlg,
                                    byte[] buf1,
                                    byte[] buf2,
                                    byte[] buf3,
                                    byte[] buf4)
                             throws HandleException
Throws:
HandleException

doDigest

public static final byte[] doDigest(byte digestType,
                                    byte[] buf)
                             throws HandleException
Throws:
HandleException

sortNumberArray

public static void sortNumberArray(java.lang.Number[] a)

encrypt

public static byte[] encrypt(java.security.PublicKey encryptingKey,
                             byte[] secretKey)
                      throws java.lang.Exception
encrypt with Public key

Throws:
java.lang.Exception

getBytesFromFile

public static byte[] getBytesFromFile(java.lang.String file)
convert a file into a byte stream


getBytesFromFile

public static byte[] getBytesFromFile(java.io.File file)

writeBytesToFile

public static boolean writeBytesToFile(java.lang.String file,
                                       byte[] keyBytes)
write byte array into a given file name


writeBytesToFile

public static boolean writeBytesToFile(java.io.File file,
                                       byte[] keyBytes)

isMatchingKeyPair

public static boolean isMatchingKeyPair(java.security.PublicKey pubkey,
                                        java.security.PrivateKey privkey)
                                 throws HandleException
check that a given PublicKey and a given PrivateKey are a pair

Throws:
HandleException

decrypt

public static byte[] decrypt(java.security.PrivateKey privKey,
                             byte[] ciphertext)
                      throws HandleException
Throws:
HandleException

getPrivateKeyFromFileWithPassphrase

public static java.security.PrivateKey getPrivateKeyFromFileWithPassphrase(java.io.File privKeyFile,
                                                                           java.lang.String passphrase)
                                                                    throws java.lang.Exception
Throws:
java.lang.Exception

checkJavaVersion

public static boolean checkJavaVersion()