anlavn.hash.MD5 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AL-Library_EN Show documentation
Show all versions of AL-Library_EN Show documentation
Java library for many thing wonderful
The newest version!
package anlavn.hash;
// Make By Bình An || AnLaVN || KatoVN
import anlavn.file.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**The MD5 class supports object and string encryption, decryption it is impossible.
* MD5 uses the Message-Digest algorithm 5 cryptographic hash function with a 128-bit long hash value.
*
Make sure your object class was "implements Serializable".
* @author AnLaVN - https://github.com/AnLaVN
* @deprecated The security of the MD5 hash function is severely compromised.
* A collision attack exists that can find collisions within seconds on a computer with a 2.6 GHz Pentium 4 processor
* (complexity of 224.1). Further, there is also a chosen-prefix collision attack that can produce a collision
* for two inputs with specified prefixes within seconds, using off-the-shelf computing hardware (complexity 239).
*/
@Deprecated
public class MD5 {
private static String encrypt(final byte[] bytes){
try {
final MessageDigest md = MessageDigest.getInstance("MD5");
final byte[] hash = md.digest(bytes);
final BigInteger no = new BigInteger(1, hash);
String hashtext = no.toString(16);
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}catch (NoSuchAlgorithmException e) {
Log.add("!!! Error try to Encrypt MD5. !!!\n\tError code: " + e.toString());
return null;
}
}
/**Use this method to encrypt the original object.
* @param objToEncrypt is the object need encrypt.
* @return a MD5 hash code of the original object.
*/
public static final String encrypt(final Object objToEncrypt){
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(objToEncrypt); oos.flush();
return encrypt(bos.toByteArray());
}catch (IOException e) {
Log.add("!!! Error try to Encrypt MD5 an Object. Can not parse Object to bytes. !!!\n\tError code: " + e.toString());
return null;
}
}
/**Use this method to encrypt the original string.
* @param strToEncrypt is the string need encrypt.
* @return a MD5 hash code of the original string.
*/
public static final String encrypt(final String strToEncrypt){
return encrypt(strToEncrypt.getBytes());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy