Class Hash

java.lang.Object
tenapull.data.persistence.Hash
All Implemented Interfaces:
java.lang.Comparable<Hash>

public class Hash
extends java.lang.Object
implements java.lang.Comparable<Hash>
Represents a "lazy" or "dirty" SHA-512 hash of a string lookup or object lookup. This is referred to as "lazy/drity" because if the string being hashed is less than 64 bytes, then the string itself is used in place of the hash. This still guarantees the uniqueness of the hash, while saving the need to calculate the SHA-512 hash when it would be unnecessary
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  Hash.Converter
    Converts Hash instances back and forth between the database varbinary datatype and the hash fields in the ORM.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static java.lang.String ALGORITHM
    The algorithm which we are using -- SHA-512
    static int HASH_SIZE
    The size of a SHA-512 hash -- 64 bytes.
  • Constructor Summary

    Constructors 
    Constructor Description
    Hash​(byte[] bytes)
    Instantiates a new Hash using the given array of bytes
    Hash​(java.lang.String str)
    Instantiates a new Hash, by making a "lazy hash" the given string
  • Method Summary

    Modifier and Type Method Description
    static int compareTo​(byte[] mine, byte[] theirs)
    Returns a comparison of two byte arrays
    int compareTo​(Hash other)  
    static boolean equals​(byte[] mine, byte[] theirs)
    Determines if two byte arrays are equivalent
    boolean equals​(java.lang.Object o)  
    int hashCode()  
    static int hashCode​(byte[] bytes)
    Generates a unique java hashCode from the provided bytes array by applying a cumulative bitwise XOR operation on each sequential set of 4 bytes
    static int hashCode​(java.lang.String str)
    Generates a unique java hashCode from the bytes array in the provided string
    static java.lang.String hexString​(byte[] bytes)
    Converts an array of bytes into a hexadecimal encoded string
    static java.lang.String hexString​(java.lang.String str)
    Lazily hashes the provided string and returns the resulting hash as a hexadecimal encoded
    static byte[] lazyHash​(java.lang.String str)
    Lazily hashes the provided string into an array bytes
    java.lang.String toString()  

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • Hash

      public Hash​(byte[] bytes)
      Instantiates a new Hash using the given array of bytes
      Parameters:
      bytes - the bytes
    • Hash

      public Hash​(java.lang.String str)
      Instantiates a new Hash, by making a "lazy hash" the given string
      Parameters:
      str - the str
  • Method Details

    • lazyHash

      public static byte[] lazyHash​(java.lang.String str)
      Lazily hashes the provided string into an array bytes
      Parameters:
      str - the str
      Returns:
      the byte [ ]
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object
    • hexString

      public static java.lang.String hexString​(byte[] bytes)
      Converts an array of bytes into a hexadecimal encoded string
      Parameters:
      bytes - the bytes
      Returns:
      the string
    • hexString

      public static java.lang.String hexString​(java.lang.String str)
      Lazily hashes the provided string and returns the resulting hash as a hexadecimal encoded
      Parameters:
      str - the str
      Returns:
      the string
    • equals

      public boolean equals​(java.lang.Object o)
      Overrides:
      equals in class java.lang.Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • hashCode

      public static int hashCode​(java.lang.String str)
      Generates a unique java hashCode from the bytes array in the provided string
      Parameters:
      str - the str
      Returns:
      the int
    • hashCode

      public static int hashCode​(byte[] bytes)
      Generates a unique java hashCode from the provided bytes array by applying a cumulative bitwise XOR operation on each sequential set of 4 bytes
      Parameters:
      bytes - the bytes
      Returns:
      the int
    • equals

      public static boolean equals​(byte[] mine, byte[] theirs)
      Determines if two byte arrays are equivalent
      Parameters:
      mine - byte array1
      theirs - byte array2
      Returns:
      the boolean
    • compareTo

      public int compareTo​(Hash other)
      Specified by:
      compareTo in interface java.lang.Comparable<Hash>
    • compareTo

      public static int compareTo​(byte[] mine, byte[] theirs)
      Returns a comparison of two byte arrays
      Parameters:
      mine - the comparable whose compare method was called
      theirs - the comparable being compared against
      Returns:
      the int