MD5 Tool class, provides string MD5 encryption, file MD5 value acquisition (checksum) function

Source: Internet
Author: User
Tags benchmark

MD5 Tool class, provides string MD5 encryption (checksum), file MD5 value acquisition (checksum) function:

 Packagecom.yzu.utils;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.nio.ByteBuffer;ImportJava.nio.channels.FileChannel;Importjava.security.MessageDigest;Importjava.security.NoSuchAlgorithmException;Importorg.apache.commons.io.IOUtils;/*** MD5 Tool class, provides string MD5 encryption (checksum), file MD5 value acquisition (checksum) function*/ Public classMd5utils {/*** 16 binary Character Set*/    Private Static Final Char[] hex_digits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' }; /*** Specify the algorithm for MD5 messagedigest*/    Private StaticMessageDigest messagedigest =NULL; /*** Initialize the MessageDigest encryption algorithm to MD5*/    Static {        Try{messagedigest= Messagedigest.getinstance ("MD5"); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); }    }    /*** Get the MD5 value of the file * *@paramfile * Target files *@returnMD5 String*/     Public Staticstring getfilemd5string (file file) {string ret= ""; FileInputStream in=NULL; FileChannel CH=NULL; Try{ in=Newfileinputstream (file); CH=In.getchannel (); Bytebuffer Bytebuffer= Ch.map (FileChannel.MapMode.READ_ONLY, 0, File.length ());            Messagedigest.update (Bytebuffer); RET=Bytestohex (Messagedigest.digest ()); } Catch(IOException e) {e.printstacktrace (); } finally{ioutils.closequietly (ch, in); }        returnret; }    /*** * Get the MD5 value of the file * *@paramFileName * Full name of the target file *@returnMD5 String*/     Public Staticstring getfilemd5string (String fileName) {returnGetfilemd5string (NewFile (fileName)); }    /*** * MD5 Encrypted String * *@paramSTR * Target String *@returnMD5 the encrypted string*/     Public Staticstring getmd5string (String str) {returngetmd5string (Str.getbytes ()); }    /*** * MD5 encrypts the string represented in a byte array * *@parambytes * Target byte array *@returnMD5 the encrypted string*/     Public StaticString getmd5string (byte[] bytes)        {messagedigest.update (bytes); returnBytestohex (Messagedigest.digest ()); }    /*** * Verify that the password is consistent with its MD5 * *@parampwd * Password String *@paramMD5 * Benchmark MD5 value *@returnTest Results*/     Public Static BooleanCheckpassword (string pwd, string md5) {returngetmd5string (PWD). Equalsignorecase (MD5); }    /*** * Verify that the password is consistent with its MD5 * *@parampwd * Password represented as a character array *@paramMD5 * Benchmark MD5 value *@returnTest Results*/     Public Static BooleanCheckpassword (Char[] pwd, String MD5) {        returnCheckpassword (NewString (PWD), MD5); }    /*** * MD5 value of the test file * *@paramfile * Target files * *@paramMD5 * Benchmark MD5 value * *@returnTest Results **/     Public Static BooleancheckFileMD5 (file file, String MD5) {returngetfilemd5string (file). equalsignorecase (MD5); }    /*** * MD5 value of the test file * *@paramFileName * Full name of the target file * *@paramMD5 * Benchmark MD5 value * *@returnTest Results **/     Public Static BooleancheckFileMD5 (String fileName, string md5) {returnCheckFileMD5 (NewFile (fileName), MD5); }    /*** * convert byte array to 16 binary string * *@parambytes * Destination byte array * *@returnConversion Results **/     Public StaticString Bytestohex (bytebytes[]) {        returnBytestohex (Bytes, 0, bytes.length); }    /*** * Converts a subarray of a specified interval in a byte array into a 16-character string *@parambytes * Destination byte array * *@paramstart * Start position (including the position) * *@paramEnd * Ending position (excluding this position) *@returnConversion Results **/     Public StaticString Bytestohex (byteBytes[],intStartintend) {StringBuilder SB=NewStringBuilder ();  for(inti = start; I < start + end; i++) {sb.append (Bytetohex (bytes[i)); }        returnsb.tostring (); }    /*** Double-layer Encrypted MD5 value *@paramstrsrc*/     Public Staticstring getSimenMD5 (String strsrc) {string str1=getmd5string (STRSRC). toLowerCase (); StringBuffer str2=NewStringBuffer (""); Str2.append (Str1.substring (0, 2)). Append ("|").). Append (Str1.substring (4). Append ("|").). Append (Str1.substring (8). Append ("|").). Append (Str1.substring (). Append ("|").). Append (Str1.substring (1, 9)). Append ("|").). Append (Str1.substring (24, 27)); returngetmd5string (str2.tostring ()); }    /*** Converts a single bytecode into a 16 string *@paramBT * Target byte *@returnConversion Results*/     Public StaticString Bytetohex (byteBT) {        returnHex_digits[(BT & 0XF0) >> 4] + "" + HEX_DIGITS[BT & 0xf]; }         Public Static voidMain (string[] args)throwsIOException {String fileName= "D:/javawed/debug.js"; String MD5= Getfilemd5string (NewFile (fileName)); System.out.println (FileName+ "file MD5:" +MD5); System.out.println (GetSimenMD5 ("123456")); }}

MD5 Tool class, provides string MD5 encryption, file MD5 value acquisition (checksum) function

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.