Description
Compute the MD-5 hash of a string, and return a truncated int-value of the hash.
License
Open Source License
Parameter
Parameter | Description |
---|
text | Text to compute the MD-5 hash of |
Exception
Parameter | Description |
---|
NoSuchAlgorithmException | thrown if the MD-5 algorithm cannot be found |
UnsupportedEncodingException | thrown if the encoding is not supported |
Return
a truncated int-value of the hash.
Declaration
public static int md5HashInt(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException
Method Source Code
//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Main {
private static final String HASH_MD5 = "MD5";
/**//from w w w . ja v a2s .c om
* Compute the MD-5 hash of a string, and return a truncated int-value of the hash. The incoming text is assumed to be
* UTF-8 encoded. An exception will be thrown if this encoding is not supported.
*
* @param text Text to compute the MD-5 hash of
* @return a truncated int-value of the hash.
* @throws NoSuchAlgorithmException thrown if the MD-5 algorithm cannot be found
* @throws UnsupportedEncodingException thrown if the encoding is not supported
*/
public static int md5HashInt(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
return truncateHashToInt(computeHash(text, HASH_MD5));
}
/**
* Computes a truncated code from the given hash-value and returns it as int-variable.
*
* @param hash the hash-string
* @return truncated code as int
*/
public static int truncateHashToInt(String hash) {
return truncateHashToInt(hexToBytes(hash));
}
/**
* Computes a truncated code from the given hash-value and returns it as int-variable.
*
* @param bytes hash-value as a byte-array
* @return truncated code as int
*/
public static int truncateHashToInt(byte[] bytes) {
int offset = bytes[bytes.length - 1] & 0x0f;
return (bytes[offset] & (0x7f << 24)) | (bytes[offset + 1] & (0xff << 16))
| (bytes[offset + 2] & (0xff << 8)) | (bytes[offset + 3] & 0xff);
}
private static byte[] computeHash(String text, String algorithm)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(text.getBytes("UTF-8"), 0, text.length());
return md.digest();
}
/**
* Convert a string consisting of hex-numbers into an array of bytes, which contains the binary representation of
* those hexadecimal-numbers (takes pairs of numbers, so make sure the number of characters is even).
*
* @param hex String of pairs of hexadecimal numbers
* @return byte-array with the binary representation of the hex-string
*/
public static byte[] hexToBytes(String hex) {
byte[] bytes = new byte[hex.length() >> 1];
for (int i = 0; i < bytes.length; i++) {
int baseIndex = i << 1;
// in order to be able to parse the full range of 0x00 to 0xFF, we need a Short or Integer
// to do the parsing, as Byte will throw an exception for values above or equal to 0x80.
bytes[i] = (byte) Integer.parseInt(hex.substring(baseIndex, baseIndex + 2), 16);
}
return bytes;
}
}
Related
- md5hash(String key)
- md5hash(String pass)
- md5Hash(String source)
- md5Hash(String text)
- md5HashBytesToBytes(byte[] buf)