[J2SE]Java SE Security初探 -- Hash值

文件Hash值生成

public class HashUtils {

/**
* 文件SHA-1 Hash值生成器
*
* @param in
* @return
* @throws Exception
*/
public static String generateFileSHA1(String fileUrl) throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream(fileUrl));
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] buffer = new byte[8192];
int length = -1;
while ((length = in.read(buffer)) != -1) {
md.update(buffer, 0, length);
}
return StringBytesTransformUtils.bytesToHexString(md.digest());
}

/**
* 文件MD5 Hash值生成器
*
* @param fileUrl
* @return
* @throws Exception
*/
public static String generateFileMD5(String fileUrl) throws Exception {
InputStream in = new BufferedInputStream(new FileInputStream(fileUrl));
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[8192];
int length = -1;
while ((length = in.read(buffer)) != -1) {
md.update(buffer, 0, length);
}
return StringBytesTransformUtils.bytesToHexString(md.digest());
}
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页