您的位置:首页 > 编程语言 > Java开发

Java文件的MD5校验和CRC校验

2015-09-05 22:04 447 查看
1、CRC校验

使用7Z 开源SDK中提供的功能类

// SevenZip/CRC.java

package SevenZip;

public class CRC
{
static public int[] Table = new int[256];

static
{
for (int i = 0; i < 256; i++)
{
int r = i;
for (int j = 0; j < 8; j++)
if ((r & 1) != 0)
r = (r >>> 1) ^ 0xEDB88320;
else
r >>>= 1;
Table[i] = r;
}
}

int _value = -1;

public void Init()
{
_value = -1;
}

public void Update(byte[] data, int offset, int size)
{
for (int i = 0; i < size; i++)
_value = Table[(_value ^ data[offset + i]) & 0xFF] ^ (_value >>> 8);
}

public void Update(byte[] data)
{
int size = data.length;
for (int i = 0; i < size; i++)
_value = Table[(_value ^ data[i]) & 0xFF] ^ (_value >>> 8);
}

public void UpdateByte(int b)
{
_value = Table[(_value ^ b) & 0xFF] ^ (_value >>> 8);
}

public int GetDigest()
{
return _value ^ (-1);
}
}
边读取文件内容边计算CRC

public static int CalCRC(String strFile){
int nResult = 0;
File file = new File(strFile);
if ( !file.exists() ){
System.out.println("文件:" + strFile + "不存在!");
return nResult;
}
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
byte[] buff = new byte[1024];
CRC crc = new CRC();
crc.Init();
while ( true ){
int nRead = fis.read(buff);
if ( nRead >0 )
crc.Update(buff, 0, nRead);
if ( nRead < 1024 )
break;
}
nResult = crc.GetDigest();
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
if ( fis != null )
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return nResult;
}
返回的是一个int类型数据,为了更便于阅读我们通常将其转成16进制显示

//10进制转16进制
public static String IntToHex(int n, boolean bUseFlag){
char[] ch = new char[20];
int nIndex = 0;
while ( true ){
int m = n/16;
int k = n%16;
if ( k == 15 )
ch[nIndex] = 'F';
else if ( k == 14 )
ch[nIndex] = 'E';
else if ( k == 13 )
ch[nIndex] = 'D';
else if ( k == 12 )
ch[nIndex] = 'C';
else if ( k == 11 )
ch[nIndex] = 'B';
else if ( k == 10 )
ch[nIndex] = 'A';
else
ch[nIndex] = (char)('0' + k);
nIndex++;
if ( m == 0 )
break;
n = m;
}
StringBuffer sb = new StringBuffer();
sb.append(ch, 0, nIndex);
sb.reverse();
String strHex = null;
if ( bUseFlag )
strHex = new String("0x");
else
strHex = new String();
strHex += sb.toString();
return strHex;
}


2、MD5校验

使用java.security.MessageDigest中为我们提供的接口

public static String CalFileMd5(String strFile) throws Exception{
File file = new File(strFile);
if ( !file.exists() ){
throw new Exception("file not exists!");
}
String str = new String();
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
MessageDigest md5 = MessageDigest.getInstance("MD5");
int nBuffLen = 10*1024; //10KB
byte[] buff = new byte[nBuffLen];
while( true ){
int nRead = fis.read(buff);
if ( nRead>0 )
md5.update(buff, 0, nRead);
if ( nRead<nBuffLen )
break;
}
byte[] bMd5 = md5.digest();
StringBuffer sb = new StringBuffer("0x");
for ( int i=0; i<bMd5.length; ++i ){
sb.append(ByteToHex(bMd5[i]));
}
str = sb.toString();
}
catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
if ( fis != null )
fis.close();
}
return str;
}


测试代码:

String strFile = new String("C:\\2.gif");
int nRet = CalCRC(strFile);
System.out.println("CRC32 = " + IntToHex(nRet, true));

try {
System.out.println("MD5 = " + Md5.CalFileMd5(strFile));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


输出:

CRC32 = 0xD4AC9DE

MD5 = 0xC09CF4902BFB284B5C34D28C545090

与其他软件计算的值对比:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: