110,561
社区成员
发帖
与我相关
我的任务
分享
//获取文件SHA1校验码
public static String getSha1Str(File file){
String str = "";
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = newbyte[2048];
int c;
while ((c = fis.read(buf)) > 0) {
baos.write(buf, 0, c);
}
fis.close();
str = SHA1(baos.toByteArray());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return str;
}
//处理字节数组获取SHA1摘要
public static String SHA1(byte[] byteArray) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(byteArray);
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString().toUpperCase();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return"";
}