用Java的security包生成撮要

用Java的security包生成摘要

贴代码,主要备忘一下直接对文件摘要的写法。

package digest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil {

	private static int BUFFERSIZE = 4096;

	@SuppressWarnings("unused")
	public static byte[] digestFile(String filepath, String algorithm)
			throws NoSuchAlgorithmException, FileNotFoundException, IOException {
		byte[] fileDigestBytes;
		InputStream is = new FileInputStream(new File(filepath));
		MessageDigest md = MessageDigest.getInstance(algorithm);
		DigestInputStream dis = new DigestInputStream(is, md);
		byte[] temp = new byte[BUFFERSIZE];
		int len = -1;
		while ((len = dis.read(temp)) > 0)
			;
		fileDigestBytes = md.digest();
		dis.close();
		is.close();
		return fileDigestBytes;
	}

	public static byte[] digestBytes(byte[] message, String algorithm)
			throws NoSuchAlgorithmException {
		MessageDigest messagedigest = MessageDigest.getInstance(algorithm);
		messagedigest.update(message);
		return messagedigest.digest();
	}

	public static String toHexString(byte byteArr[]) {
		StringBuilder strBuilder = new StringBuilder();
		for (int i = 0; i < byteArr.length; i++) {
			String strHex = Integer.toHexString(byteArr[i] & 0xff);
			if (strHex.length() == 1) {
				strBuilder.append("0");
			}
			strBuilder.append(strHex);
			if (i < byteArr.length - 1) {
				strBuilder.append(":");
			}
		}
		return strBuilder.toString();
	}

}

 测试

package digest;

import static org.junit.Assert.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;

import org.junit.Test;

public class TestDigestUtil {
	@Test
	public void testDigestFile() {
		byte[] expected = { -116, 30, 12, -4, -114, 79, -37, 99, -124, 46,
				-101, -75, -64, -14, -56, -117 };
		byte[] target = null;
		try {
			target = DigestUtil.digestFile("C:\\Android中文文档.pdf", "MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		assertArrayEquals(expected, target);
	}

	@Test
	public void testDigestBytes() {
		byte[] byteMessage = { 77, 68, 53, 58, 32, 56, 67, 49, 69, 48, 67, 70,
				67, 56, 69, 52, 70, 68, 66, 54, 51, 56, 52, 50, 69, 57, 66, 66,
				53, 67, 48, 70, 50, 67, 56, 56, 66, 13, 10, 77, 68, 53, 58, 32,
				56, 67, 49, 69, 48, 67, 70, 67, 56, 69, 52, 70, 68, 66, 54, 51,
				56, 52, 50, 69, 57, 66, 66, 53, 67, 48, 70, 50, 67, 56, 56, 66,
				61, 61, 61, 13, 10, 77, 68, 53, 58, 32, 56, 67, 49, 69, 48, 67,
				70, 67, 56, 69, 52, 70, 68, 66, 54, 51, 56, 52, 50, 69, 57, 66,
				66, 53, 67, 48, 70, 50, 67, 56, 56, 66, 45, 45, 45, 45, 45, 45,
				13, 10, 13, 10, 83, 72, 65, 49, 58, 32, 51, 55, 57, 56, 68, 70,
				55, 67, 70, 65, 68, 70, 57, 48, 57, 69, 67, 54, 68, 67, 69, 51,
				69, 49, 69, 67, 52, 52, 69, 54, 48, 48, 66, 57, 53, 53, 56, 65,
				50, 57, 13, 10, 83, 72, 65, 49, 58, 32, 51, 55, 57, 56, 68, 70,
				55, 67, 70, 65, 68, 70, 57, 48, 57, 69, 67, 54, 68, 67, 69, 51,
				69, 49, 69, 67, 52, 52, 69, 54, 48, 48, 66, 57, 53, 53, 56, 65,
				50, 57, 61, 61, 61, 13, 10, 83, 72, 65, 49, 58, 32, 51, 55, 57,
				56, 68, 70, 55, 67, 70, 65, 68, 70, 57, 48, 57, 69, 67, 54, 68,
				67, 69, 51, 69, 49, 69, 67, 52, 52, 69, 54, 48, 48, 66, 57, 53,
				53, 56, 65, 50, 57, 45, 45, 45, 45, 45, 45, 13, 10 };
		byte[] expected = { -14, 68, 120, 16, -108, 71, 91, 84, 8, -61, 35,
				-78, -60, -35, 41, -49 };
		byte[] target = null;
		try {
			target = DigestUtil.digestBytes(byteMessage, "MD5");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		assertArrayEquals(expected, target);
	}

	@Test
	public void testToHexString() {
		String expected = "8c:1e:0c:fc:8e:4f:db:63:84:2e:9b:b5:c0:f2:c8:8b";
		byte[] byteMge = { -116, 30, 12, -4, -114, 79, -37, 99, -124, 46, -101,
				-75, -64, -14, -56, -117 };
		assertEquals(expected, DigestUtil.toHexString(byteMge));
	}
}