Skip to content

Commit

Permalink
文件hash算法速度比较
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaodj committed Mar 12, 2013
1 parent 6d717a3 commit 82dbd00
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions java/demo/src/main/java/org/zhaodj/util/FileHasher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.zhaodj.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class FileHasher {

private static final char[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

public static String hash(String algorithm,String filepath) throws NoSuchAlgorithmException, IOException{
MessageDigest md=MessageDigest.getInstance(algorithm);
InputStream is=new FileInputStream(filepath);
int numRead=0;
byte[] buffer=new byte[1024];
while((numRead=is.read(buffer))>0){
md.update(buffer, 0, numRead);
}
is.close();
return new String(encodeHex(md.digest()));
}

public static char[] encodeHex(byte[] data) {

int l = data.length;

char[] out = new char[l << 1];

// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ];
out[j++] = DIGITS[ 0x0F & data[i] ];
}

return out;
}

public static void test(String algorithm,String filepath,int times) throws NoSuchAlgorithmException, IOException{
long begin=System.nanoTime();
for(int i=0;i<times;i++){
hash(algorithm,filepath);
}
System.out.println(algorithm+":"+(System.nanoTime()-begin));
}

public static void main(String[] args) throws NoSuchAlgorithmException, IOException{
String file="/home/zhaodj/图片/照片/IMG_3643.JPG";
System.out.println("file:"+file);
test("MD5",file,1000);
test("SHA1",file,1000);
file="/home/zhaodj/图片/照片/ada.jpg";
System.out.println("file:"+file);
test("MD5",file,1000);
test("SHA1",file,1000);
}

}

0 comments on commit 82dbd00

Please sign in to comment.