Java generates MD5 checksum code

Source: Internet
Author: User

In Java, Java.security.MessageDigest (Rt.jar) has already defined MD5 's calculations, so we simply call to get a MD5 128-bit integer. Then convert this 128-bit meter 16 bytes to a 16-binary representation.

The following is an example of a string or file MD5 check code can be generated, tested, can be used as a tool class directly, the most important of which is getmd5string (string s) and getfilemd5string (file file) two methods, The MD5 checksum for generating a string and the MD5 checksum of the generated file, respectively

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.nio.MappedByteBuffer;
Import Java.nio.channels.FileChannel;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;

public class Md5util {
/**
* Default password string combination, used to convert bytes to 16 binary characters, Apache check the correctness of the downloaded file is the default of this combination
*/
protected static char hexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '};
protected static MessageDigest messagedigest = null;
static{
try{
MessageDigest = Messagedigest.getinstance ("MD5");
}catch (NoSuchAlgorithmException nsaex) {
System.err.println (MD5Util.class.getName () + "initialization failed, MessageDigest does not support Md5util. ");
Nsaex.printstacktrace ();
}
}

public static void Main (string[] args) throws IOException {
Long begin = System.currenttimemillis ();

File Big = new file ("E:/xxxx.rar");

String md5=getfilemd5string (BIG);

Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("MD5:" +md5+ "Time:" + ((end-begin)/1000) + "s");
}

MD5 code for generated files
public static String getfilemd5string (file file) throws IOException {
FileInputStream in = new FileInputStream (file);
FileChannel ch = in.getchannel ();
Mappedbytebuffer Bytebuffer = Ch.map (FileChannel.MapMode.READ_ONLY, 0, File.length ());
Messagedigest.update (Bytebuffer);
Return Buffertohex (Messagedigest.digest ());
}

MD5 code to generate a string
public static string getmd5string (string s) {
Return getmd5string (S.getbytes ());
}

public static String getmd5string (byte[] bytes) {
Messagedigest.update (bytes);
Return Buffertohex (Messagedigest.digest ());
}

private static String Buffertohex (byte bytes[]) {
Return Buffertohex (bytes, 0, bytes.length);
}

private static String Buffertohex (byte bytes[], int m, int n) {
StringBuffer StringBuffer = new StringBuffer (2 * n);
int k = m + N;
for (int l = m; l < K; l++) {
Appendhexpair (Bytes[l], stringbuffer);
}
return stringbuffer.tostring ();
}


private static void Appendhexpair (Byte bt, StringBuffer StringBuffer) {
char C0 = hexdigits[(BT & 0XF0) >> 4];
Char C1 = HEXDIGITS[BT & 0xf];
Stringbuffer.append (C0);
Stringbuffer.append (C1);
}

public static Boolean Checkpassword (string password, string md5pwdstr) {
String s = getmd5string (password);
Return S.equals (MD5PWDSTR);
}
}

Java generates MD5 checksum code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.