Class Util

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

public abstract class Util extends Object
  • Field Details

  • Constructor Details

    • Util

      public Util()
  • Method Details

    • looksLikeBinary

      public static final boolean looksLikeBinary(byte[] buf)
    • duplicateByteArray

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

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

      public static final String decodeHexString(byte[] buf, boolean formatNicely)
    • encodeHexString

      public static final byte[] encodeHexString(String s)
    • encodeString

      public static final byte[] encodeString(String s)
      Encoded the specified string into a byte array
    • decodeString

      public static final String decodeString(byte[] buf)
    • decodeString

      public static final 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 protocol (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
    • getZeroNAHandle

      public static String getZeroNAHandle(String handle)
    • convertSlashlessHandleToZeroNaHandle

      public static final byte[] convertSlashlessHandleToZeroNaHandle(byte[] handle)
    • isSubNAHandle

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

      public static boolean isSubNAHandle(String handle)
    • getParentNAOfNAHandle

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

      public static String getParentNAOfNAHandle(String naHandle)
    • isHandleUnderPrefix

      public static boolean isHandleUnderPrefix(String handle, String prefix)
    • isDerivedFrom

      public static boolean isDerivedFrom(String handle, String ancestorHandle)
    • getPrefixPart

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

      public static String getPrefixPart(String handle)
    • getSuffixPart

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

      public static String getSuffixPart(String 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
    • upperCase

      public static String upperCase(String s)
    • 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.
    • upperCasePrefix

      public static final byte[] upperCasePrefix(byte[] b)
      create and return an upper-case (prefix only, or all of a global handle) copy of the given UTF8 byte array
    • upperCasePrefix

      public static String upperCasePrefix(String s)
    • upperCasePrefixInPlace

      public static final byte[] upperCasePrefixInPlace(byte[] b)
      Convert all of the characters in the prefix of the given utf-8 byte array to upper case; for global handles upper case all. 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 boolean equalsCI(String s1, String s2)
    • 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.
    • equalsPrefixCI

      public static final boolean equalsPrefixCI(byte[] b1, byte[] b2)
      Determine if the first parameter equals the second parameter in a case insensitive (within prefix) comparison; for global handles, entire handles are compared case insensitively.
    • equalsPrefixCI

      public static final boolean equalsPrefixCI(String s1, String s2)
      Determine if the first parameter equals the second parameter in a case insensitive (within prefix) comparison; for global handles, entire handles are compared case insensitively.
    • equalsPrefixCI

      public static final boolean equalsPrefixCI(byte[] b1, int b1Len, byte[] b2, int b2Len)
      Determine if the first parameter equals the second parameter in a case insensitive (within prefix) manner over the given length; for global handles, entire handles are compared case insensitively.
    • 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.
    • startsWithCI

      public static boolean startsWithCI(String s1, String s2)
    • 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)
    • rfcIpPortRepr

      public static String rfcIpPortRepr(InetAddress addr, int port)
    • rfcIpRepr

      public static String rfcIpRepr(byte[] ipv6Address)
    • rfcIpRepr

      public static String rfcIpRepr(InetAddress addr)
    • 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.
    • getNextUnusedIndex

      public static final int getNextUnusedIndex(HandleValue[] values, int firstIdx)
    • getAltSiteInfo

      public static SiteInfo getAltSiteInfo(SiteInfo site)
    • fill16

      public static byte[] fill16(byte[] bytes)
    • 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.
    • getSitesAndAltSitesFromValues

      public static SiteInfo[] getSitesAndAltSitesFromValues(HandleValue[] values)
      Extract and return all of the SiteInfo records from the given list of handle values. Include SiteInfos generated using the "alt_addr" attribue. Returns null if no site values were found.
    • getSitesAndAltSitesFromValues

      public static SiteInfo[] getSitesAndAltSitesFromValues(HandleValue[] values, byte[][] types)
    • 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.
    • getNamespaceFromValues

      public static NamespaceInfo getNamespaceFromValues(String handle, HandleValue[] values)
    • 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.
    • getPrimarySite

      public static SiteInfo getPrimarySite(SiteInfo[] sites)
    • filterValues

      public static HandleValue[] filterValues(HandleValue[] allValues, int[] indexList, byte[][] typeList)
    • filterOnlyPublicValues

      public static List<HandleValue> filterOnlyPublicValues(List<HandleValue> values)
    • getPassphrase

      public static final byte[] getPassphrase(String prompt) throws Exception
      Get a passphrase from the user.
      Throws:
      Exception
    • getHashAlgIdFromSigId

      public static byte[] getHashAlgIdFromSigId(String signatureAlgorithm) throws HandleException
      Get the ID that the handle protocol uses to identify the hash algorithm used in the given signature algorithm descriptor.
      Throws:
      HandleException
    • getSigIdFromHashAlgId

      public static String getSigIdFromHashAlgId(byte[] hashAlgId, String sigKeyType) throws HandleException
      Throws:
      HandleException
    • getDefaultSigId

      public static String getDefaultSigId(String algorithm)
    • getDefaultSigId

      public static String getDefaultSigId(String algorithm, AbstractMessage message) throws HandleException
      Throws:
      HandleException
    • getBytesFromPrivateKey

      public static byte[] getBytesFromPrivateKey(PrivateKey key) throws Exception
      Throws:
      Exception
    • getPrivateKeyFromBytes

      public static PrivateKey getPrivateKeyFromBytes(byte[] pkBuf) throws HandleException, InvalidKeySpecException
      Throws:
      HandleException
      InvalidKeySpecException
    • getPrivateKeyFromBytes

      public static PrivateKey getPrivateKeyFromBytes(byte[] pkBuf, int offset) throws HandleException, InvalidKeySpecException
      Throws:
      HandleException
      InvalidKeySpecException
    • getBytesFromPublicKey

      public static byte[] getBytesFromPublicKey(PublicKey key) throws HandleException
      Throws:
      HandleException
    • getPublicKeyFromFile

      public static PublicKey getPublicKeyFromFile(String filename) throws Exception
      Throws:
      Exception
    • getPublicKeyFromBytes

      public static PublicKey getPublicKeyFromBytes(byte[] pkBuf) throws Exception
      Throws:
      Exception
    • getPublicKeyFromBytes

      public static PublicKey getPublicKeyFromBytes(byte[] pkBuf, int offset) throws Exception
      Throws:
      Exception
    • getPublicKeysFromValues

      public static List<PublicKey> getPublicKeysFromValues(HandleValue[] values)
    • encrypt

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

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

      public static byte[] doPBKDF2(byte[] password, byte[] salt, int iterations, int length) throws NoSuchAlgorithmException, InvalidKeySpecException
      Throws:
      NoSuchAlgorithmException
      InvalidKeySpecException
    • constructPbkdf2Encoding

      public static byte[] constructPbkdf2Encoding(byte[] salt, int iterations, int keyLength, byte[] mac)
    • requiresSecretKey

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

      public static byte[] decrypt(byte[] ciphertext, byte[] secretKey) throws Exception
      Decrypt the given set of bytes using the specified secret key
      Throws:
      Exception
    • doSHA1Digest

      public static final byte[] doSHA1Digest(byte[]... bufs) throws HandleException
      Throws:
      HandleException
    • doHmacSHA1

      public static byte[] doHmacSHA1(byte[] buf, byte[] key) throws HandleException
      Throws:
      HandleException
    • doHmacSHA256

      public static byte[] doHmacSHA256(byte[] buf, byte[] key) throws HandleException
      Throws:
      HandleException
    • doPbkdf2HmacSHA1

      public static byte[] doPbkdf2HmacSHA1(byte[] buf, byte[] key, byte[] paramsToMatch) throws HandleException
      Throws:
      HandleException
    • doSHA256Digest

      public static byte[] doSHA256Digest(byte[]... bufs) throws HandleException
      Throws:
      HandleException
    • doMD5Digest

      public static final byte[] doMD5Digest(byte[]... bufs) throws HandleException
      Throws:
      HandleException
    • doDigest

      public static final byte[] doDigest(byte digestType, byte[]... bufs) throws HandleException
      Throws:
      HandleException
    • doMac

      public static final byte[] doMac(byte digestType, byte[] buf, byte[] key) throws HandleException
      Throws:
      HandleException
    • doMac

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

      public static final byte[] doDigest(byte[] digestType, byte[]... bufs) throws HandleException
      Throws:
      HandleException
    • doMac

      public static final byte[] doMac(byte[] digestType, byte[] buf, byte[] key) throws HandleException
      Throws:
      HandleException
    • equalsIgnoreCaseAndPunctuation

      public static boolean equalsIgnoreCaseAndPunctuation(byte[] a, byte[] b)
    • sortNumberArray

      public static void sortNumberArray(Number[] a)
    • encrypt

      @Deprecated public static byte[] encrypt(PublicKey encryptingKey, byte[] secretKey) throws Exception
      Deprecated.
      Throws:
      Exception
    • encrypt

      public static byte[] encrypt(PublicKey encryptingKey, byte[] secretKey, int majorProtocolVersion, int minorProtocolVersion) throws Exception
      encrypt with Public key
      Throws:
      Exception
    • getBytesFromFile

      public static byte[] getBytesFromFile(String file)
      convert a file into a byte stream
    • getBytesFromFile

      public static byte[] getBytesFromFile(File file)
    • getBytesFromInputStream

      public static byte[] getBytesFromInputStream(InputStream in) throws IOException
      Throws:
      IOException
    • readFully

      public static void readFully(InputStream in, byte[] b) throws IOException
      Like in.read(b), but attempts to read as many bytes as possible
      Throws:
      IOException
    • readFully

      public static void readFully(InputStream in, byte[] b, int off, int len) throws IOException
      Like in.read(b, off, len), but attempts to read as many bytes as possible
      Throws:
      IOException
    • writeBytesToFile

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

      public static boolean writeBytesToFile(File file, byte[] keyBytes)
    • isMatchingKeyPair

      public static boolean isMatchingKeyPair(PublicKey pubkey, PrivateKey privkey) throws HandleException
      check that a given PublicKey and a given PrivateKey are a pair
      Throws:
      HandleException
    • decrypt

      @Deprecated public static byte[] decrypt(PrivateKey privKey, byte[] ciphertext) throws Exception
      Deprecated.
      Throws:
      Exception
    • decrypt

      public static byte[] decrypt(PrivateKey privKey, byte[] ciphertext, int majorProtocolVersion, int minorProtocolVersion) throws Exception
      Throws:
      Exception
    • getPrivateKeyFromFileWithPassphrase

      public static PrivateKey getPrivateKeyFromFileWithPassphrase(File privKeyFile, String passphrase) throws Exception
      Throws:
      Exception
    • concat

      public static byte[] concat(byte[] first, byte[] second)
    • getAccessLogString

      public static String getAccessLogString(AbstractRequest req, AbstractResponse resp)
    • getSiteFromFile

      public static SiteInfo getSiteFromFile(String filename) throws Exception
      Reads a SiteInfo from a file formatted as either siteinfo.bin or siteinfo.json.
      Throws:
      Exception
    • getSiteFromBytes

      public static SiteInfo getSiteFromBytes(byte[] data) throws Exception
      Throws:
      Exception