Java实现FTP服务器中文件的MD5校验和CRC校验(适用于所有的文件校验)

1、CRC校验
1.1使用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);
	}
}

1.2边读取文件内容边计算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;
	}

1.3返回的是一个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校验
2.1使用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;
	}

3、测试代码:

	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();
		}

4、输出结果:

CRC32 = 0xD4AC9DE
MD5 = 0xC09CF4902BFB284B5C34D28C545090
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值