Package Cn.net. comsys. ut. util;
ImportJava. Io. file;
ImportJava. Io. fileinputstream;
ImportJava. Io. ioexception;
ImportJava. Io. inputstream;
ImportJava. NiO. bytebuffer;
ImportJava. NiO. channels. filechannel;
ImportJava. Security. messagedigest;
ImportJava. Security. nosuchalgorithmexception;
Public class md5util {
/**
* 123456 after encryption: 123456: e10adc3949ba59abbe56e057f20f883e
*/
/** * hexadecimal 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 as MD5 messagedigest */
private static messagedigest = null ;
/*** Initialize the messagedigest encryption algorithm to MD5.*/
Static{
Try{
Messagedigest = messagedigest. getinstance ("MD5 ");
}Catch(Nosuchalgorithmexception e ){
E. printstacktrace ();
}
}
/**
** Obtain the MD5 value of an object
*
* @ Param File
* Target file
*
* @ Return MD5 string
*/
Public Static String getfilemd5string (File file ){
String ret = "";
Fileinputstream in = Null ;
Filechannel CH = Null ;
Try {
In = New Fileinputstream (File );
Ch = in. getchannel ();
Bytebuffer = CH. Map (filechannel. mapmode. read_only, 0,
File. Length ());
Messagedigest. Update (bytebuffer );
Ret = bytestohex (messagedigest. Digest ());
} Catch (Ioexception e ){
E. printstacktrace ();
} Finally {
If (In! = Null ){
Try {
In. Close ();
} Catch (Ioexception e ){
E. printstacktrace ();
}
}
If (Ch! = Null ){
Try {
Ch. Close ();
} Catch (Ioexception e ){
E. printstacktrace ();
}
}
}
Return RET;
}
/**
** Obtain the MD5 value of an object
*
* @ Param Filename
* Complete name of the target file
*
* @ Return MD5 string
*/
Public Static String getfilemd5string (string filename ){
Return Getfilemd5string ( New File (filename ));
}
/**
** MD5 encrypted string
*
*@ ParamStr
* Target string
*
*@ ReturnMD5 encrypted string
*/
Public StaticString getmd5string (string Str ){
ReturnGetmd5string (Str. getbytes ());
}
/**
** MD5 encryption refers to the string represented by byte arrays.
*
*@ ParamBytes
* Target byte array
*
*@ ReturnMD5 encrypted string
*/
Public StaticString getmd5string (Byte[] Bytes ){
Messagedigest. Update (bytes );
ReturnBytestohex (messagedigest. Digest ());
}
/**
** Verify that the password is consistent with the MD5 value.
*
* @ Param PWD
* Password string
*
* @ Param MD5
* Base MD5 Value
*
* @ Return Inspection result
*/
Public Static Boolean Checkpassword (string PWD, string MD5 ){
Return Getmd5string (PWD). inclusignorecase (MD5 );
}
/**
** Verify that the password is consistent with the MD5 value.
*
* @ Param PWD
* Password in character array
*
* @ Param MD5
* Base MD5 Value
*
* @ Return Inspection result
*/
Public Static Boolean Checkpassword ( Char [] PWD, string MD5 ){
Return Checkpassword ( New String (PWD), MD5 );
}
/**
** Verify the MD5 value of the file
*
* @ Param File
* Target file
*
* @ Param MD5
* Base MD5 Value
*
* @ Return Inspection result
*/
Public Static Boolean Checkfilemd5 (File file, string MD5 ){
Return Getfilemd5string (file). inclusignorecase (MD5 );
}
/**
** Verify the MD5 value of the file
*
* @ Param Filename
* Complete name of the target file
*
* @ Param MD5
* Base MD5 Value
*
* @ Return Inspection result
*/
Public Static Boolean Checkfilemd5 (string filename, string MD5 ){
Return Checkfilemd5 (New File (filename), MD5 );
}
/**
** Convert byte arrays into hexadecimal strings
*
* @ Param Bytes
* Target byte array
*
* @ Return Conversion Result
*/
Public Static String bytestohex ( Byte Bytes []) {
Return Bytestohex (bytes, 0, bytes. Length );
}
/**
** Convert the subarray of the specified interval in the byte array to a hexadecimal string.
*
* @ Param Bytes
* Target byte array
*
* @ Param Start
* Start position (including this position)
*
* @ Param End
* End position (excluding this position)
*
* @ Return Conversion Result
*/
Public Static String bytestohex ( Byte Bytes [], Int Start, Int End ){
Stringbuilder sb = New Stringbuilder ();
For (Int I = start; I <start + end; I ++ ){
SB. append (bytetohex (Bytes [I]);
}
Return SB. tostring ();
}
/**
** Convert a single bytecode to a hexadecimal string
*
* @ Param BT
* Target Byte
*
* @ Return Conversion Result
*/
Public Static String bytetohex ( Byte BT ){
Return Hex_digits [(BT & 0xf0)> 4] + "" + hex_digits [BT & 0xf];
}
// //
Public Static Void Main (string [] ARGs) Throws Ioexception {
Long Begin = system. currenttimemillis ();
String MD5 = getfilemd5string ( New File ("C: // AA. Cer "));
Long End = system. currenttimemillis ();
System. Out. println ("MD5: \ t" + MD5 + "\ ntime: \ t" + (end-begin) + "Ms ");
}
}