唯一标记文件内容的方式
方式1:
1.1.MD5 (容易生成30位的值)
public static MessageDigest MD5;
static {
try {
MD5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static String mkMd5(byte[] bs) {
MD5.reset();
MD5.update(bs);
return toHex(MD5.digest());
}
public static String toHex(byte[] bs) {
StringBuilder builder = new StringBuilder();
for (byte b : bs) {
builder.append(Integer.toHexString(b & 0xff));
}
return builder.toString();
}
//测试
String readMd5 = mkMd5(ByteStreams.toByteArray(objectContent));
1.2. getCRC(是long类型的值)
public long getCrc(InputStream inputStream)
throws IOException
{
CRC32 crc32 = new CRC32();
byte[] data = new byte[1024];
int i = 0;
while ((i = inputStream.read(data)) > 0) {
crc32.update(data, 0, i);
}
return crc32.getValue();
}
//测试
long readMd5 = getCrc(objectContent);
方式2
2.1. MD5(只会生成32位的值, 建议采用)
@Test
public void testMD5() throws NoSuchAlgorithmException {
byte[] content = new byte[100];
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(content);
byte[] md5Bytes = digest.digest();
String md5 = BaseEncoding.base16().lowerCase().encode(md5Bytes);
}
2.2.getCRC
@Test
public void testCrc() {
byte[] content = new byte[100];
CRC32 crc32 = new CRC32();
crc32.update(content);
long crcCode = crc32.getValue();
}
9300

被折叠的 条评论
为什么被折叠?



