MD5 is a common cryptographic algorithm and is often used to verify information integrity, such as file integrity. In terms of terminology, MD5 is a message digest algorithm (msg Digest algorithm). In addition, there is a commonly used message digest algorithm SHA1. If you want to know these words, you can go to Baidu Encyclopedia: MD5, SHA1, Message digest algorithm.
Java has implemented the MD5,SHA1 algorithm. Take advantage of Java. Security. The MessageDigest class can get MD5 and SHA1 results for strings and files .
1. MD5 of the string (the following code has a detailed comment)
Public Staticstring stringMD5 (String input) {Try { //Get a MD5 converter (if you want to change the SHA1 parameter to "SHA1")messagedigest messagedigest=messagedigest.getinstance ("MD5"); //the input string is converted into a byte array byte[] Inputbytearray =input.getbytes (); //Inputbytearray is an array of bytes converted from the input stringmessagedigest.update (Inputbytearray); //converts and returns the result, which is also a byte array containing 16 elements byte[] Resultbytearray =messagedigest.digest (); //converts a character array into a string return returnBytearraytohex (Resultbytearray); } Catch(nosuchalgorithmexception e) {return NULL; }}
//The following function is used to change the byte array into a 16-binary string Public StaticString Bytearraytohex (byte[] byteArray) { //First initialize a character array to hold each 16-character Char[] hexdigits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' }; //new A character array, which is used to compose the result string (explanation: A byte is a eight-bit binary, which is the 2-bit hexadecimal character (2 of 8 is equal to 16 of the 2-second party)) Char[] Resultchararray =New Char[Bytearray.length * 2]; //iterates through byte arrays, converts characters into character arrays by bitwise operations (high efficiency of bitwise operations) intindex = 0; for(byteB:bytearray) {Resultchararray[index+ +] = hexdigits[b>>> 4 & 0xf]; Resultchararray[index+ +] = hexdigits[b& 0xf]; } //character array group composition string return return NewString (Resultchararray);}
As can be seen from the above code, using MessageDigest to MD5 the string algorithm is the first to convert the string into a byte array, in the MD5 algorithm, the final return is also a byte array, to our own to turn into a 32-bit string.
2. File MD5
MD5 A file can also be like a string MD5, first to convert the file into a byte array, followed by the string MD5 exactly the same.
However, if it is a very large file, all of a sudden the array of a file is read into memory, then the memory is also estimated to be unbearable.
For large files, you can use Digestinputstream.
Public StaticString fileMD5 (String inputfile)throwsIOException {//buffer Size (this can take a parameter) intbuffersize = 256 * 1024; FileInputStream FileInputStream=NULL; Digestinputstream Digestinputstream=NULL; Try { //Get a MD5 converter (again, here you can switch to SHA1)messagedigest messagedigest=messagedigest.getinstance ("MD5"); //using DigestinputstreamFileInputStream=NewFileInputStream (inputfile); Digestinputstream=NewDigestinputstream (fileinputstream,messagedigest); //MD5 processing During the read process until the file is finished byte[] buffer =New byte[buffersize]; while(Digestinputstream.read (buffer) > 0); //get the final messagedigestMessageDigest=digestinputstream.getmessagedigest (); //Get the result, also a byte array, containing 16 elements byte[] Resultbytearray =messagedigest.digest (); //Similarly, converting a byte array to a string returnBytearraytohex (Resultbytearray); } Catch(nosuchalgorithmexception e) {return NULL; } finally { Try{digestinputstream.close (); } Catch(Exception e) {}Try{fileinputstream.close (); } Catch(Exception e) {} }}
The above method I personally measured the size of about 4G files, the MD5 value and the online download of a MD5 gadget to get the MD5 value, the way above shows no problem. However, the MD5 of large files is very slow, 4G files run for a minute (I5 processor 6G Memory 64-bit XP system notebook).
Attached 1: I also see a way to file MD5 on the Internet
Public Static throws IOException { new fileinputstream (file); = In.getchannel (); = Ch.map (FileChannel.MapMode.READ_ONLY, 0, file.length ()); Messagedigest.update (Bytebuffer); return Bytearraytohex (Messagedigest.digest ()); }
I've tried this way too, but if the file is larger than 2G, it will be an exception. So not recommended.
Attached 2: Main method of test file MD5
Public Static void Main (string[] args) { long startTime = system.currenttimemillis (); Try { System.out . println (FileMD5 ("e:/software/vs2008proedition90daytrialchsx1435983.iso")); Catch (IOException e) { e.printstacktrace (); } long endTime = system.currenttimemillis (); -StartTime); }
Transferred from: https://my.oschina.net/shootercn/blog/159839
Java encrypts strings or files using the MD5 algorithm provided by MessageDigest