对图片md5加密实现
程序员文章站
2022-03-14 19:40:20
...
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Md5Util {
// 将图片转为Md5
public static String getMd5ByFile(File file) {
StringBuffer sb = new StringBuffer("");
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(FileUtils.readFileToByteArray(file));
byte b[] = md.digest();
int d;
for (int i = 0; i < b.length; i++) {
d = b[i];
if (d < 0) {
d = b[i] & 0xff;
}
if (d < 16)
sb.append("0");
sb.append(Integer.toHexString(d));
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
上一篇: uva 196 Spreadsheet
下一篇: UVA 839