Package marauroa.common.crypto
Class Hash
java.lang.Object
marauroa.common.crypto.Hash
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
converts a BigInteger to a byte arraystatic BigInteger
bytesToBigInt(byte[] b)
converts a byte array into a BigInteger objectstatic 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[]
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.
-
Constructor Details
-
Hash
public Hash()
-
-
Method Details
-
hash
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
Convert and array of bytes to a Hex string.- Parameters:
bs
- array of bytes- Returns:
- a string representing a hexadecimal number.
-
bigIntToBytes
converts a BigInteger to a byte array- Parameters:
b
- BigInteger- Returns:
- byte array
-
bytesToBigInt
converts a byte array into a BigInteger object- Parameters:
b
- byte array- Returns:
- BigInteger
-