java.lang.Object
com.docuverse.identicon.IdenticonUtil

public class IdenticonUtilextends Object
Utility methods useful for implementing identicon functionality. Methods are class methods for convenience.

Key method of interest is getIdenticonCode(InetAddress) which converts IP address into identicon code.
IMPORTANT: inetSalt value must be set to reasonably long random string prior to invoking this method.

  • Field Details

    • DEFAULT_IDENTICON_SIZE

      private static final int DEFAULT_IDENTICON_SIZE
      Identicon size used when none is asked for, in pixels.
      See Also:
    • MINIMUM_IDENTICON_SIZE

      private static final int MINIMUM_IDENTICON_SIZE
      Smallest identicon that can be drawn, in pixels.
      See Also:
    • MAXIMUM_IDENTICON_SIZE

      private static final int MAXIMUM_IDENTICON_SIZE
      Largest identicon that can be drawn, in pixels.
      See Also:
    • DEFAULT_INET_MASK

      private static final int DEFAULT_INET_MASK
      Address bits used when none are masked away.
      See Also:
    • inetMask

      private static int inetMask
      Address bits currently used for the pattern.
    • inetSalt

      private static String inetSalt
      Salt mixed into the address before hashing.
  • Constructor Details

    • IdenticonUtil

      public IdenticonUtil()
      Constructor. The mask and salt below are static settings shared by every caller, so an instance holds no state of its own.
  • Method Details

    • getInetMask

      public static int getInetMask()
      Returns current IP address mask. Default is 0xffffffff.
      Returns:
      current IP address mask
    • setInetMask

      public static void setInetMask(int inetMask)
      Sets current IP address mask. Default is 0xffffffff.
      Parameters:
      inetMask - bits kept from the 32 bit IP address before hashing, 0xffffffff for all
    • getInetSalt

      public static String getInetSalt()
      The salt string mixed into the hashed IP address.
      Returns:
      the inet salt
    • setInetSalt

      public static void setInetSalt(String inetSalt)
      Set the salt string mixed into the hashed IP address.
      Parameters:
      inetSalt - string mixed into the hashed IP; set it before getIdenticonCode()
    • getIdenticonCode

      public static int getIdenticonCode(InetAddress inetAddr) throws Exception
      Returns identicon code for given IP address.

      Current implementation uses first four bytes of SHA1(int(mask(ip))+salt) where mask(ip) uses inetMask to remove unwanted bits from IP address. Also, since salt is a string for convenience sake, int(mask(ip)) is converted into a string and combined with inetSalt prior to hashing.

      Parameters:
      inetAddr - IP address
      Returns:
      identicon code for inetAddr
      Throws:
      Exception - if inetSalt has not been set yet
    • getIdenticonCode

      public static int getIdenticonCode(String codeParam, String remoteAddr)
      Returns identicon code specified as an input parameter or derived from an IP address.

      This method is a convenience method intended to be used by servlets as in the following example:

      int code = IdenticonUtil.getIdenticonCode(request.getParameter("code"), request
                      .getRemoteAddr());
      
      Parameters:
      codeParam - code parameter, if null remoteAddr parameter will be used to determine the value.
      remoteAddr - HTTP requester's IP address. Optional if code was specified.
      Returns:
      the identicon code, or 0 if the input could not be parsed or resolved
    • getIdenticonSize

      public static int getIdenticonSize(String param)
      Resolve the pixel size of a requested identicon. A supplied value is clamped to the 15 to 64 pixel range the renderer supports; a null or unparseable one leaves the 16 pixel default in place.
      Parameters:
      param - the requested width and height in pixels, clamped to the supported range
      Returns:
      the identicon size
    • getIdenticonETag

      public static String getIdenticonETag(int code, int size, int version)
      Build the weak validator a client revalidates an identicon against, so the rendered image is refetched only when its code, size, or version changes.
      Parameters:
      code - the identicon code, rendered as hexadecimal
      size - the identicon size in pixels, rendered as decimal
      version - the cache-busting version number, rendered after a 'v'
      Returns:
      the identicon e tag