added an binary function
This commit is contained in:
parent
2627894e19
commit
c53449208d
1 changed files with 26 additions and 20 deletions
|
|
@ -14,11 +14,9 @@ public class Hasher {
|
|||
/**
|
||||
* Returns a hash of a file
|
||||
*
|
||||
* @param file The path to the file
|
||||
* @param hashType The hash type
|
||||
* @return A String with the hash
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws IOException
|
||||
* @param file is the path to the file
|
||||
* @param hashType is the hash type
|
||||
* @return a String with the hash
|
||||
*/
|
||||
public static String hash(File file, String hashType) throws NoSuchAlgorithmException, IOException {
|
||||
MessageDigest digest = MessageDigest.getInstance(hashType);//"MD5"
|
||||
|
|
@ -46,8 +44,8 @@ public class Hasher {
|
|||
/**
|
||||
* Returns the MD5 hash of the given object
|
||||
*
|
||||
* @param object The object to hash
|
||||
* @return String containing the hash
|
||||
* @param object is the object to hash
|
||||
* @return an String containing the hash
|
||||
*/
|
||||
public static String MD5(Serializable object){
|
||||
try {
|
||||
|
|
@ -61,8 +59,8 @@ public class Hasher {
|
|||
/**
|
||||
* Returns the SHA-1 hash of the given object
|
||||
*
|
||||
* @param object The object to hash
|
||||
* @return String containing the hash
|
||||
* @param object is the object to hash
|
||||
* @return an String containing the hash
|
||||
*/
|
||||
public static String SHA1(Serializable object){
|
||||
try {
|
||||
|
|
@ -76,29 +74,37 @@ public class Hasher {
|
|||
/**
|
||||
* Returns the hash of the given object
|
||||
*
|
||||
* @param object The object to hash
|
||||
* @param hashType The hash method (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 )
|
||||
* @return String containing the hash
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws IOException
|
||||
* @param object is the object to hash
|
||||
* @param hashType is the hash method (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 )
|
||||
* @return an String containing the hash
|
||||
*/
|
||||
public static String hash(Serializable object, String hashType) throws Exception {
|
||||
return hash(Converter.toBytes(object), hashType);//(new BASE64Encoder()).encode(raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hashes a given byte array
|
||||
*
|
||||
* @param data is the byte array to hash
|
||||
* @param hashType is the hash method (MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512 )
|
||||
* @return an String containing the hash
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String hash(byte[] data, String hashType) throws Exception {
|
||||
MessageDigest md = null;
|
||||
md = MessageDigest.getInstance(hashType); //MD5 || SHA
|
||||
md.update(Converter.toBytes(object));
|
||||
md.update(data);
|
||||
|
||||
byte raw[] = md.digest();
|
||||
return Converter.toHexString(raw);//(new BASE64Encoder()).encode(raw);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* MurmurHash2 ported from cpp source
|
||||
* MurmurHash2 ported from c++ source
|
||||
*
|
||||
* @param object The Key
|
||||
* @param seed Seed
|
||||
* @param object is the Key
|
||||
* @param seed is the seed
|
||||
* @return A MurmurHash of the key
|
||||
* @throws Exception
|
||||
*/
|
||||
public static int MurmurHash(Serializable object, int seed) throws Exception{
|
||||
byte[] data = Converter.toBytes(object);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue