Class HashUtils

java.lang.Object
de.ubs.xdm3.batch.modification.HashUtils

public class HashUtils extends Object
This class provides static helper functions for hashing input values in the same way as BCV5. It also provides several other helper functions that can be used in masking algorithms.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    Retrieves the current masking offset.
    static int
    hashInt(int x)
    Calculates a hash value for the input parameter.
    static int
    hashInt(int x, int max)
    Calculates a hash value for the input parameter.
    static int
    Calculates a hash value for the input parameter.
    static int
    hashString(String x, int max)
    Calculates a hash value for the input parameter.
    static int
    Calculates a hash value for the input string.
    static int
    Calculates a hash value for the input string.
    static long
    luhn(long in)
    Replaces the right-most digit of the input parameter with a check digit based on the Luhn algorithm.
    static void
    setMaskingOffset(int maskingOffset)
    Sets a new value for the masking offset.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getMaskingOffset

      public static int getMaskingOffset()
      Retrieves the current masking offset. The masking offset is used in the static hash functions that are provided by this class.
      Returns:
      The current masking offset to use in hash calculations
    • setMaskingOffset

      public static void setMaskingOffset(int maskingOffset)
      Sets a new value for the masking offset. The masking offset is used in the static hash functions that are provided by this class. The default is 0. Whenever this offset is set to a new value, the results of the hashing functions will change.
      Parameters:
      maskingOffset - The new masking offset to use in hash calculations
    • hashInt

      public static int hashInt(int x)
      Calculates a hash value for the input parameter. The resulting hash value is between 0 and 2^31 − 1. Unlike Integer.hashcode(), this method does not simply return the inpot value itself. Instead, it calculates a hash based on Knuth's multiplicative hashing algorithm. As a result, similar input values have totally different hash values.

      This method is designed to be compatible with the string hash function that BCV5 uses.

      Parameters:
      x - The number to hash
      Returns:
      A non-negative hash value for the input number
    • hashInt

      public static int hashInt(int x, int max)
      Calculates a hash value for the input parameter. The resulting hash value is between 1 and the specified max value.

      This method is designed to be compatible with the integer hash function that BCV5 uses.

      Parameters:
      x - The number to hash
      max - The upper bound for the output value
      Returns:
      A hash value for the input number that is between 1 and max
    • hashString

      public static int hashString(String x)
      Calculates a hash value for the input parameter. The resulting hash value is between 0 and 2^31 − 1. This method is similar, but not identical to the String.hashcode() method.

      This method is designed to be compatible with the string hash function that BCV5 uses.

      Parameters:
      x - The string to hash
      Returns:
      A non-negative hash value for the input string
    • hashString

      public static int hashString(String x, int max)
      Calculates a hash value for the input parameter. The resulting hash value is between 1 and the specified max value.

      This method is designed to be compatible with the string hash function that BCV5 uses.

      Parameters:
      x - The string to hash
      Returns:
      A non-negative hash value for the input string
    • hashStringLikeInt

      public static int hashStringLikeInt(String x)
      Calculates a hash value for the input string. The resulting hash value is between 0 and 2^31 - 1. If the input parameter contains the string representation of a non-negative integer number, this method returns the same hash value as the hashInt() method would return when called with that number. Otherwise, this method returns the same result as the hashString() method.
      Parameters:
      x - The string to hash
      Returns:
      A non-negative hash value for the string which, if the string contains a non-negative number, is identical to the hash value of that number.
    • hashStringLikeInt

      public static int hashStringLikeInt(String x, int max)
      Calculates a hash value for the input string. The resulting hash value is between 1 and the specified max value. If the input parameter contains the string representation of a non-negative integer number, this method returns the same hash value as the hashInt() method would return when called with that number. Otherwise, this method returns the same result as the hashString() method.
      Parameters:
      x - The string to hash
      Returns:
      A non-negative hash value for the string which, if the string contains a non-negative number, is identical to the hash value of that number.
    • luhn

      public static long luhn(long in)
      Replaces the right-most digit of the input parameter with a check digit based on the Luhn algorithm. The input paramter must contain a "placeholder" digit that may be any number (0-9). The Luhn check digit is calculated over all digits except the last one, and then written into the last digit.
      Parameters:
      in - A non-negative input value
      Returns:
      The same number, but with the last digit modified to represent a Luhn check digit over the other digits.