package com.yeshi.buwan.util;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.RandomAccessFile;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
|
public class SHA1Util {
|
public static String getFileMD5(File file) {
|
if (!file.isFile()) {
|
return null;
|
}
|
MessageDigest digest = null;
|
FileInputStream in = null;
|
byte buffer[] = new byte[1024];
|
int len;
|
try {
|
digest = MessageDigest.getInstance("SHA-1");
|
in = new FileInputStream(file);
|
while ((len = in.read(buffer, 0, 1024)) != -1) {
|
digest.update(buffer, 0, len);
|
}
|
in.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
return byte2hex(digest.digest());
|
}
|
|
public static String byte2hex(byte abyte0[]) {
|
StringBuilder stringbuilder = new StringBuilder("");
|
int i = 0;
|
while (i < abyte0.length) {
|
String s = Integer.toHexString(0xff & abyte0[i]);
|
String s1;
|
if (s.length() == 1)
|
s1 = (new StringBuilder()).append("0").append(s).toString();
|
else
|
s1 = s;
|
stringbuilder.append(s1);
|
i++;
|
}
|
|
return stringbuilder.toString().trim();
|
}
|
|
public static String leshiYunSHA1(File file)
|
throws NoSuchAlgorithmException, IOException {
|
MessageDigest messagedigest = MessageDigest.getInstance("SHA-1");
|
RandomAccessFile randomaccessfile1 = new RandomAccessFile(file, "r");
|
byte abyte0[] = new byte[0x10000];
|
long l;
|
int j;
|
int k;
|
if (file.length() % 8L == 0L)
|
l = file.length() / 8L;
|
else
|
l = 1L + file.length() / 8L;
|
for (int i = 0; i < 8; i++) {
|
randomaccessfile1.seek(l * (long) i);
|
j = 0;
|
do {
|
if (j >= 0x10000)
|
break;
|
k = randomaccessfile1.read(abyte0);
|
if (k <= 0)
|
break;
|
messagedigest.update(abyte0, 0, k);
|
j += k;
|
} while (true);
|
}
|
messagedigest.update(String.valueOf(file.length()).getBytes());
|
return byte2hex(messagedigest.digest());
|
}
|
|
|
|
|
|
}
|