Class IdenticonUtil


  • public class IdenticonUtil
    extends Object
    Utility methods useful for implementing identicon functionality. Methods are class methods for convenience.

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

    Author:
    don
    • Constructor Detail

      • IdenticonUtil

        public IdenticonUtil()
    • Method Detail

      • 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 -
      • getInetSalt

        public static String getInetSalt()
        Returns current inetSalt value.
        Returns:
        the value
      • setInetSalt

        public static void setInetSalt​(String inetSalt)
        Sets current inetSalt value.
        Parameters:
        inetSalt -
      • 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 converetd into a string and combined with inetSalt prior to hashing.

        Parameters:
        inetAddr - IP address
        Returns:
        identicon code for inetAddr
        Throws:
        Exception
      • 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 like below:

         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 code
      • getIdenticonSize

        public static int getIdenticonSize​(String param)
      • getIdenticonETag

        public static String getIdenticonETag​(int code,
                                              int size,
                                              int version)