CRC算法C#實現

原文地址:

接口實現:http://www.cnblogs.com/canny/archive/2004/12/27/82466.aspx

CRC8:http://www.cnblogs.com/canny/archive/2004/12/27/82468.aspx

CRC16:http://www.cnblogs.com/canny/archive/2004/12/27/82470.aspx

CRC32:http://www.cnblogs.com/canny/archive/2004/12/27/82473.aspx

1.接口實現

CRC 校驗有 CRC 8位 16位 32位 所以在實現這些校驗算髮之前,先實現它們的接口!

using System; 

namespace CRC 

 
public interface ICRC 
 

     
  
long Value 
  

    
get
  }
 
   
  
void Reset(); 
   
  
void Crc(int bval); 
   
  
void Crc(byte[] buffer); 
   
  
void Crc(byte[] buf, int off, int len); 

 }
 
}
 


2. CRC8

 

using System;

namespace CRC 

 
/// <summary> 
 
/// CRC8 的摘要說明。 
 
/// </summary> 

 public class CRC8:ICRC 
 

  
CRC 8 位校驗表 

  
uint crc = 0
   
  
/// <summary> 
  
///返回 CRC8校驗結果; 
  
/// </summary> 

  public long Value 
  

   
get 
   

    
return crc; 
   }
 
   
set 
   

    crc 
= (uint)value; 
   }
 
  }
 


  
/// <summary> 
  
/// CRC校驗前設置校驗值 
  
/// </summary> 

  public void Reset() 
  

   crc 
= 0
  }
 

  
/// <summary> 
  
/// 8 位 CRC 校驗 產生校驗碼 需要被校驗碼和校驗碼 
  
/// </summary> 
  
/// <param name="CRC"></param> 
  
/// <param name="OldCRC"> 初始爲 0 ,以後爲 返回值 ret </param> 
  
/// <returns> 產生校驗碼時 ret 爲校驗碼</returns> 

   
  
public void Crc(byte CRC,byte OldCRC) 
  

   crc 
= CRC8_Table[OldCRC ^ CRC]; 
  }
 

  
/// <summary> 
  
/// 8 位 CRC 校驗 產生校驗碼 只要被校驗碼 
  
/// </summary> 
  
/// <param name="bval"></param> 

  public void Crc(int bval) 
  

   crc 
= CRC8_Table[crc ^ bval]; 
  }
 

  
/// <summary> 
  
/// 8 位 CRC 校驗 產生校驗碼 只要被校驗的字節數組 
  
/// </summary> 
  
/// <param name="buffer"></param> 

  public void Crc(byte[] buffer) 
  


   Crc(buffer,
0,buffer.Length); 
  }
 

  
/// <summary> 
  
/// 8 位 CRC 校驗 產生校驗碼 要被校驗的字節數組、起始結果位置和字節長度 
  
/// </summary> 
  
/// <param name="buf"></param> 
  
/// <param name="off"></param> 
  
/// <param name="len"></param> 

  public void Crc(byte[] buf,int off ,int len) 
  

   
if (buf == null
   

    
throw new ArgumentNullException("buf"); 
   }
 
    
   
if (off < 0 || len < 0 || off + len > buf.Length) 
   

    
throw new ArgumentOutOfRangeException(); 
   }
 
   
for (int i = off; i < len ; i ++
   

    Crc(buf[i]); 
   }
 
  }
 
 }
 
}
 


3.CRC16

 

using System;

namespace CRC 

 
/// <summary> 
 
/// CRC16 的摘要說明。 
 
/// </summary> 

 public class CRC16:ICRC 
 

  
CRC 16 位校驗表 

  
ushort crc = 0;

  
/// <summary> 
  
/// 校驗後的結果 
  
/// </summary> 

  public long Value 
  

   
get 
   

    
return crc; 
   }
 
   
set 
   

    crc 
= (ushort)value; 
   }
 
  }
 

  
/// <summary> 
  
/// 設置crc 初始值 
  
/// </summary> 

  public void Reset() 
  

   crc 
= 0
  }
 
        
  
/// <summary> 
  
/// Crc16 
  
/// </summary> 
  
/// <param name="ucrc"></param> 
  
/// <param name="buf"></param> 

  public void Crc(ushort ucrc,byte[] buf) 
  

   crc 
= ucrc; 
   Crc(buf); 
  }
 
   
  
/// <summary> 
  
/// Crc16 
  
/// </summary> 
  
/// <param name="bval"></param> 

  public void Crc(int bval) 
  

   
ushort h = (ushort)((crc >> 12& 0x0f); 
   
ushort l = (ushort)((crc >> 8 ) & 0x0f); 
   
ushort temp = crc; 
   temp 
=(ushort)(((temp & 0x00ff<< 8| bval); 
   temp 
=(ushort)(temp ^(uppercrctab[(h -1+ 1^ lowercrctab[(l - 1+ 1])); 
   crc 
= temp; 
  }
 


  
/// <summary> 
  
/// Crc16 
  
/// </summary> 
  
/// <param name="buffer"></param> 

  public void Crc(byte[] buffer) 
  

   Crc(buffer,
0,buffer.Length); 
  }
 

  
/// <summary> 
  
/// Crc16 
  
/// </summary> 
  
/// <param name="buf"></param> 
  
/// <param name="off"></param> 
  
/// <param name="len"></param> 

  public void Crc(byte[] buf,int off,int len) 
  

   
if (buf == null
   

    
throw new ArgumentNullException("buf"); 
   }
 
    
   
if (off < 0 || len < 0 || off + len > buf.Length) 
   

    
throw new ArgumentOutOfRangeException(); 
   }
 
   
for (int i = off; i < len ; i ++
   

    Crc(buf[i]); 
   }
 
  }
 

  

 }
 
}
 


 

4.CRC32

 

using System;

namespace CRC 

  
public sealed class Crc32 : ICRC 
 

  
readonly static uint CrcSeed = 0xFFFFFFFF
   
  
readonly static uint[] CrcTable = new uint[] 
               
0x000000000x770730960xEE0E612C0x990951BA0x076DC419
               
0x706AF48F0xE963A5350x9E6495A30x0EDB88320x79DCB8A4
               
0xE0D5E91E0x97D2D9880x09B64C2B0x7EB17CBD0xE7B82D07
               
0x90BF1D910x1DB710640x6AB020F20xF3B971480x84BE41DE
               
0x1ADAD47D0x6DDDE4EB0xF4D4B5510x83D385C70x136C9856
               
0x646BA8C00xFD62F97A0x8A65C9EC0x14015C4F0x63066CD9
               
0xFA0F3D630x8D080DF50x3B6E20C80x4C69105E0xD56041E4
               
0xA26771720x3C03E4D10x4B04D4470xD20D85FD0xA50AB56B
               
0x35B5A8FA0x42B2986C0xDBBBC9D60xACBCF9400x32D86CE3
               
0x45DF5C750xDCD60DCF0xABD13D590x26D930AC0x51DE003A
               
0xC8D751800xBFD061160x21B4F4B50x56B3C4230xCFBA9599
               
0xB8BDA50F0x2802B89E0x5F0588080xC60CD9B20xB10BE924
               
0x2F6F7C870x58684C110xC1611DAB0xB6662D3D0x76DC4190
               
0x01DB71060x98D220BC0xEFD5102A0x71B185890x06B6B51F
               
0x9FBFE4A50xE8B8D4330x7807C9A20x0F00F9340x9609A88E
               
0xE10E98180x7F6A0DBB0x086D3D2D0x91646C970xE6635C01
               
0x6B6B51F40x1C6C61620x856530D80xF262004E0x6C0695ED
               
0x1B01A57B0x8208F4C10xF50FC4570x65B0D9C60x12B7E950
               
0x8BBEB8EA0xFCB9887C0x62DD1DDF0x15DA2D490x8CD37CF3
               
0xFBD44C650x4DB261580x3AB551CE0xA3BC00740xD4BB30E2
               
0x4ADFA5410x3DD895D70xA4D1C46D0xD3D6F4FB0x4369E96A
               
0x346ED9FC0xAD6788460xDA60B8D00x44042D730x33031DE5
               
0xAA0A4C5F0xDD0D7CC90x5005713C0x270241AA0xBE0B1010
               
0xC90C20860x5768B5250x206F85B30xB966D4090xCE61E49F
               
0x5EDEF90E0x29D9C9980xB0D098220xC7D7A8B40x59B33D17
               
0x2EB40D810xB7BD5C3B0xC0BA6CAD0xEDB883200x9ABFB3B6
               
0x03B6E20C0x74B1D29A0xEAD547390x9DD277AF0x04DB2615
               
0x73DC16830xE3630B120x94643B840x0D6D6A3E0x7A6A5AA8
               
0xE40ECF0B0x9309FF9D0x0A00AE270x7D079EB10xF00F9344
               
0x8708A3D20x1E01F2680x6906C2FE0xF762575D0x806567CB
               
0x196C36710x6E6B06E70xFED41B760x89D32BE00x10DA7A5A
               
0x67DD4ACC0xF9B9DF6F0x8EBEEFF90x17B7BE430x60B08ED5
               
0xD6D6A3E80xA1D1937E0x38D8C2C40x4FDFF2520xD1BB67F1
               
0xA6BC57670x3FB506DD0x48B2364B0xD80D2BDA0xAF0A1B4C
               
0x36034AF60x41047A600xDF60EFC30xA867DF550x316E8EEF
               
0x4669BE790xCB61B38C0xBC66831A0x256FD2A00x5268E236
               
0xCC0C77950xBB0B47030x220216B90x5505262F0xC5BA3BBE
               
0xB2BD0B280x2BB45A920x5CB36A040xC2D7FFA70xB5D0CF31
               
0x2CD99E8B0x5BDEAE1D0x9B64C2B00xEC63F2260x756AA39C
               
0x026D930A0x9C0906A90xEB0E363F0x720767850x05005713
               
0x95BF4A820xE2B87A140x7BB12BAE0x0CB61B380x92D28E9B
               
0xE5D5BE0D0x7CDCEFB70x0BDBDF210x86D3D2D40xF1D4E242
               
0x68DDB3F80x1FDA836E0x81BE16CD0xF6B9265B0x6FB077E1
               
0x18B747770x88085AE60xFF0F6A700x66063BCA0x11010B5C
               
0x8F659EFF0xF862AE690x616BFFD30x166CCF450xA00AE278
               
0xD70DD2EE0x4E0483540x3903B3C20xA76726610xD06016F7
               
0x4969474D0x3E6E77DB0xAED16A4A0xD9D65ADC0x40DF0B66
               
0x37D83BF00xA9BCAE530xDEBB9EC50x47B2CF7F0x30B5FFE9
               
0xBDBDF21C0xCABAC28A0x53B393300x24B4A3A60xBAD03605
               
0xCDD706930x54DE57290x23D967BF0xB3667A2E0xC4614AB8
               
0x5D681B020x2A6F2B940xB40BBE370xC30C8EA10x5A05DF1B
               
0x2D02EF8D 
              }

   
  
internal static uint ComputeCrc32(uint oldCrc, byte bval) 
  

   
return (uint)(Crc32.CrcTable[(oldCrc ^ bval) & 0xFF^ (oldCrc >> 8)); 
  }
 
   
  
uint crc = 0
   
  
public long Value 
  

   
get 
   

    
return (long)crc; 
   }
 
   
set 
   

    crc 
= (uint)value; 
   }
 
  }
 
   
  
public void Reset() 
  

   crc 
= 0
  }
 
   
  
public void Crc(int bval) 
  

   crc 
^= CrcSeed; 
   crc  
= CrcTable[(crc ^ bval) & 0xFF^ (crc >> 8); 
   crc 
^= CrcSeed; 
  }
 
   
  
public void Crc(byte[] buffer) 
  

   Crc(buffer, 
0, buffer.Length); 
  }
 
   
  
public void Crc(byte[] buf, int off, int len) 
  

   
if (buf == null
   

    
throw new ArgumentNullException("buf"); 
   }
 
    
   
if (off < 0 || len < 0 || off + len > buf.Length) 
   

    
throw new ArgumentOutOfRangeException(); 
   }
 
    
   crc 
^= CrcSeed; 
    
   
while (--len >= 0
   

    crc 
= CrcTable[(crc ^ buf[off++]) & 0xFF^ (crc >> 8); 
   }
 
    
   crc 
^= CrcSeed; 
  }
 
 }
 
}
 


發佈了40 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章