Exemplo n.º 1
0
 /// <summary>
 /// 获取String
 /// </summary>
 /// <exception cref="ArgumentNullException">_string 为null</exception>
 /// <returns></returns>
 public static uint GetStringCRC(string _string)
 {
     if (_string == null)
     {
         throw (new ArgumentNullException("_string", "参数_string 为null"));
     }
     else
     {
         var crc32 = new CRC32();
         byte[] buffer = Encoding.Default.GetBytes(_string);
         return crc32.ByteCRC(buffer);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 校验文件路径
        /// </summary>
        /// <param name="_filePath">文件路径</param>
        /// <exception cref="OverflowException">溢出</exception>
        /// <exception cref="FiletoolargeException">文件长度大于int32</exception>
        /// <returns></returns>
        public static uint GetFileCRC(string _filePath)
        {
            int bufferLength = 2048;
            var crc32 = new CRC32();
            long readedLegth = 0;
            uint result = 0;
            using (var file = new FileStream(_filePath, FileMode.Open, FileAccess.Read))
            {
                if (file.Length > long.MaxValue)
                {
                    throw (new FiletoolargeException("文件长度大于int64"));
                }
                else
                {
                    uint state = 0xffffffff;

                    checked
                    {
                        while (readedLegth < file.Length)
                        {
                            if ((readedLegth + bufferLength) >= file.Length)
                            {
                                var buffer = new byte[file.Length - readedLegth];
                                file.Read(buffer, 0, buffer.Length);
                                result = crc32.ByteCRC(buffer, ref state);
                                readedLegth += (file.Length - readedLegth);
                                file.Seek(readedLegth, SeekOrigin.Begin);
                            }
                            else
                            {
                                var buffer = new byte[bufferLength];
                                file.Read(buffer, 0, bufferLength);
                                result = crc32.ByteCRC(buffer, ref state);
                                readedLegth += bufferLength;
                                file.Seek(readedLegth, SeekOrigin.Begin);
                            }
                        }
                    }
                }
            }

            return result;
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取流的CRC校验码
        /// </summary>
        /// <param name="_buffer"></param>
        /// <returns></returns>
        public static uint GetByteCRC(byte[] _buffer)
        {
            var crc32 = new CRC32();

            return(crc32.ByteCRC(_buffer));
        }
Exemplo n.º 4
0
 /// <summary>
 /// 获取流的CRC校验码 
 /// </summary>
 /// <param name="_buffer"></param>
 /// <returns></returns>
 public static uint GetByteCRC(byte[] _buffer)
 {
     var crc32 = new CRC32();
     return crc32.ByteCRC(_buffer);
 }