Class Hash

java.lang.Object
marauroa.common.crypto.Hash

public class Hash extends Object
This class is used to create Hashes of byte arrays. It is thread safe. This class require from JVM that it provides MD5 digest and SHA1PRNG random number generator.
Author:
quisar
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    converts a BigInteger to a byte array
    static BigInteger
    bytesToBigInt​(byte[] b)
    converts a byte array into a BigInteger object
    static int
    compare​(byte[] b1, byte[] b2)
    Compare two arrays of bytes so that it returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
    static byte[]
    hash​(byte[] value)
    Return an insecure hash of an array of bytes.
    static byte[]
    hash​(String value)
    Return an insecure hash of the given argument.
    static int
    Return the length of the hash digest in bytes.
    static byte[]
    random​(int nbBytes)
    Generate an array of bytes of nbBytes size.
    static String
    toHexString​(byte[] bs)
    Convert and array of bytes to a Hex string.
    static byte[]
    xor​(byte[] b1, byte[] b2)
    Returns the XOR of two arrays of bytes of the same size otherwise it returns null.

    Methods inherited from class java.lang.Object

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

    • Hash

      public Hash()
  • Method Details

    • hash

      public static final byte[] hash(String value)
      Return an insecure hash of the given argument. It is thread safe.
      Parameters:
      value - a string
      Returns:
      the hash of the string.
    • hashLength

      public static final int hashLength()
      Return the length of the hash digest in bytes.
      Returns:
      the length of the hash digest in bytes.
    • hash

      public static final byte[] hash(byte[] value)
      Return an insecure hash of an array of bytes. This method is thread safe.
      Parameters:
      value - an array of bytes.
      Returns:
      the hash of an array of bytes.
    • xor

      public static final byte[] xor(byte[] b1, byte[] b2)
      Returns the XOR of two arrays of bytes of the same size otherwise it returns null.
      Parameters:
      b1 - an array of bytes.
      b2 - an array of bytes.
      Returns:
      an array of bytes containing the xor of b1 and b2
    • compare

      public static final int compare(byte[] b1, byte[] b2)
      Compare two arrays of bytes so that it returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
      Parameters:
      b1 - an array of bytes.
      b2 - an array of bytes.
      Returns:
      a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
    • random

      public static final byte[] random(int nbBytes)
      Generate an array of bytes of nbBytes size.
      Parameters:
      nbBytes - size of the array.
      Returns:
      an array of bytes of nbBytes size.
    • toHexString

      public static final String toHexString(byte[] bs)
      Convert and array of bytes to a Hex string.
      Parameters:
      bs - array of bytes
      Returns:
      a string representing a hexadecimal number.
    • bigIntToBytes

      public static final byte[] bigIntToBytes(BigInteger b)
      converts a BigInteger to a byte array
      Parameters:
      b - BigInteger
      Returns:
      byte array
    • bytesToBigInt

      public static final BigInteger bytesToBigInt(byte[] b)
      converts a byte array into a BigInteger object
      Parameters:
      b - byte array
      Returns:
      BigInteger