Exemple #1
0
        /// <summary>
        /// Initalizes a new instance of the class using the provided algorithm type.
        /// </summary>
        /// <param name="type">Indicates what type of CRC algorithm this instance should use.</param>
        public CrcHelper(CrcAlgorithmType type)
        {
            this.Initialize(type);

            this._type  = type;
            this._table = this.CreateTable();
        }
Exemple #2
0
        private void Initialize(CrcAlgorithmType type)
        {
            switch (type)
            {
            case CrcAlgorithmType.Crc16Ccitt:
                this._order   = 16;
                this._polynom = 0x1021;
                //this.direct = true;
                this._crcInitial = 0xffff;
                this._crcXor     = 0xffff;
                this._reflectIn  = true;
                this._reflectOut = true;

                break;

            case CrcAlgorithmType.Crc32:
            default:
                this._order   = 32;
                this._polynom = 0x4c11db7;
                //this.direct = true;
                this._crcInitial = 0xffffffff;
                this._crcXor     = 0xffffffff;
                this._reflectIn  = true;
                this._reflectOut = true;

                break;
            }

            this._crcHighBit       = (ulong)1 << (this._order - 1);
            this._crcMask          = ((((ulong)1 << (this._order - 1)) - 1) << 1) | 1;
            this._crcInitialDirect = _crcInitial;
        }
Exemple #3
0
        public void AlgorithmCompareWithGeneral(CrcAlgorithmType type)
        {
            var testData = new byte[] { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39 };
            var crc1     = CrcFactory.GetCrc(type);
            var crc2     = CrcFactory.GetCrc(crc1.Argument);

            var result1 = crc1.GetCrcValue(testData);
            var result2 = crc2.GetCrcValue(testData);

            Assert.Equal(result1, result2);
        }
Exemple #4
0
 /// <summary>
 /// 获取指定类型的CRC算法
 /// </summary>
 /// <param name="type">crc类型</param>
 /// <exception cref="NotSupportedException">未实现的算法或内部错误</exception>
 public static ICrc GetCrc(CrcAlgorithmType type)
 {//对部分有具体计算方法的算法类型直接使用其实现,其他则采用通用算法
     if (type != CrcAlgorithmType.None)
     {
         const string SpecifiedNamaspace = "Parsifal.Util.CRC.Algorithm";
         var          name     = Enum.GetName(typeof(CrcAlgorithmType), type);
         var          instance = Assembly.GetExecutingAssembly().CreateInstance($"{SpecifiedNamaspace}.{name}");
         if (instance != null)
         {
             return((ICrc)instance);
         }
         else
         {
             var field = typeof(CrcStandardParam).GetField(name, BindingFlags.Public | BindingFlags.Static);
             if (field != null)
             {
                 var argument = (CrcArgument)field.GetValue(typeof(CrcArgument));
                 return(new GeneralCRC(argument));
             }
         }
     }
     throw new NotSupportedException();
 }