java.lang.Object
com.docuverse.identicon.IdenticonUtil
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final intIdenticon size used when none is asked for, in pixels.private static final intAddress bits used when none are masked away.private static intAddress bits currently used for the pattern.private static StringSalt mixed into the address before hashing.private static final intLargest identicon that can be drawn, in pixels.private static final intSmallest identicon that can be drawn, in pixels.Constructor Summary
ConstructorsMethod Summary
All MethodsModifier and TypeMethodDescriptionstatic intgetIdenticonCode(String codeParam, String remoteAddr) Returns identicon code specified as an input parameter or derived from an IP address.static intgetIdenticonCode(InetAddress inetAddr) Returns identicon code for given IP address.static StringgetIdenticonETag(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.static intgetIdenticonSize(String param) Resolve the pixel size of a requested identicon.static intReturns current IP address mask.static StringThe salt string mixed into the hashed IP address.static voidsetInetMask(int inetMask) Sets current IP address mask.static voidsetInetSalt(String inetSalt) Set the salt string mixed into the hashed IP address.
Field Details
DEFAULT_IDENTICON_SIZE
private static final int DEFAULT_IDENTICON_SIZEIdenticon size used when none is asked for, in pixels.- See Also:
MINIMUM_IDENTICON_SIZE
private static final int MINIMUM_IDENTICON_SIZESmallest identicon that can be drawn, in pixels.- See Also:
MAXIMUM_IDENTICON_SIZE
private static final int MAXIMUM_IDENTICON_SIZELargest identicon that can be drawn, in pixels.- See Also:
DEFAULT_INET_MASK
private static final int DEFAULT_INET_MASKAddress bits used when none are masked away.- See Also:
inetMask
private static int inetMaskAddress bits currently used for the pattern.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
The salt string mixed into the hashed IP address.- Returns:
- the inet salt
setInetSalt
Set the salt string mixed into the hashed IP address.- Parameters:
inetSalt- string mixed into the hashed IP; set it before getIdenticonCode()
getIdenticonCode
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
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, ifnullremoteAddr 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
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
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 hexadecimalsize- the identicon size in pixels, rendered as decimalversion- the cache-busting version number, rendered after a 'v'- Returns:
- the identicon e tag