码迷,mamicode.com
首页 > 其他好文 > 详细

CRC校验

时间:2020-04-13 12:39:59      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:prot   files   pac   ble   pos   protected   bin   coding   length   

 

 

using System;
using System.Collections.Generic;
using System.Text;

namespace CRC.Util
{
using System;
using System.IO;

/// <summary>
/// CRC Verification
/// </summary>
public class CRC32
{

protected ulong[] crc32Table;


/// <summary>
/// Initializes a new instance of the <see cref="CRC32"/> class.
/// </summary>
public CRC32()
{
const ulong ulPolynomial = 0xEDB88320;
ulong dwCrc;
crc32Table = new ulong[256];
int i, j;
for (i = 0; i < 256; i++)
{
dwCrc = (ulong)i;
for (j = 8; j > 0; j--)
{
if ((dwCrc & 1) == 1)
dwCrc = (dwCrc >> 1) ^ ulPolynomial;
else
dwCrc >>= 1;
}
crc32Table[i] = dwCrc;
}
}

/// <summary>
/// CRC Bytes.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns></returns>
public ulong ByteCRC(ref byte[] buffer)
{
ulong ulCRC = 0xffffffff;
ulong len;
len = (ulong)buffer.Length;
for (ulong buffptr = 0; buffptr < len; buffptr++)
{
ulong tabPtr = ulCRC & 0xFF;
tabPtr = tabPtr ^ buffer[buffptr];
ulCRC = ulCRC >> 8;
ulCRC = ulCRC ^ crc32Table[tabPtr];
}
return ulCRC ^ 0xffffffff;
}


/// <summary>
/// String CRC
/// </summary>
/// <param name="sInputString">the string</param>
/// <returns></returns>
public ulong StringCRC(string sInputString)
{
byte[] buffer = Encoding.Default.GetBytes(sInputString);
return ByteCRC(ref buffer);
}

/// <summary>
/// File CRC
/// </summary>
/// <param name="sInputFilename">the input file</param>
/// <returns></returns>
public long FileCRC(string sInputFilename)
{
FileStream inFile = new System.IO.FileStream(sInputFilename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] bInput = new byte[inFile.Length];
inFile.Read(bInput, 0, bInput.Length);
inFile.Close();

return (long)ByteCRC(ref bInput);
}

/// <summary>
/// Stream CRC
/// </summary>
/// <param name="sInputFilename">the input stream</param>
/// <returns></returns>
public long StreamCRC(Stream inFile)
{
try
{
byte[] bInput = new byte[inFile.Length];
inFile.Read(bInput, 0, bInput.Length);
inFile.Close();

return (long)ByteCRC(ref bInput);
}
catch (IOException)
{
throw;
}
}
}

}

 

CRC校验

标签:prot   files   pac   ble   pos   protected   bin   coding   length   

原文地址:https://www.cnblogs.com/zt2710/p/12690237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!