Esempio n. 1
0
        /// <summary>
        /// Gets the hash of the specified data buffer.
        /// </summary>
        /// <param name="data">Data buffer.</param>
        /// <param name="len">Length of the data buffer to hash.</param>
        /// <param name="hash">Byte array of the hash value.</param>
        public static string Data(byte[] data, uint len, out byte[] hash)
        {
            UInt16 localhashInt;

            localhashInt = 0x0000;

            for (int i = 0; i < len; i++)
            {
                localhashInt = (ushort)((localhashInt << 8) ^ table[data[i] ^ (localhashInt >> 8 & 0xFF)]);
            }

            localhashInt = (ushort)~localhashInt;

            BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
            hash = BigEndianBitConverter.GetBytes(localhashInt);

            StringBuilder crc16Output = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                crc16Output.Append(hash[i].ToString("x2"));
            }

            return(crc16Output.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the hash of a file in hexadecimal and as a byte array.
        /// </summary>
        /// <param name="filename">File path.</param>
        /// <param name="hash">Byte array of the hash value.</param>
        public static string File(string filename, out byte[] hash)
        {
            FileStream fileStream = new FileStream(filename, FileMode.Open);
            UInt16     localhashInt;

            localhashInt = 0x0000;

            for (int i = 0; i < fileStream.Length; i++)
            {
                localhashInt = (ushort)((localhashInt << 8) ^ table[fileStream.ReadByte() ^ (localhashInt >> 8 & 0xFF)]);
            }

            localhashInt = (ushort)~localhashInt;

            BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
            hash = BigEndianBitConverter.GetBytes(localhashInt);

            StringBuilder crc16Output = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                crc16Output.Append(hash[i].ToString("x2"));
            }

            return(crc16Output.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Returns a hexadecimal representation of the hash value.
        /// </summary>
        public string End()
        {
            hashInt = (ushort)~hashInt;

            StringBuilder crc16Output = new StringBuilder();

            BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
            for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++)
            {
                crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
            }

            return(crc16Output.ToString());
        }
Esempio n. 4
0
 /// <summary>
 /// Returns a byte array of the hash value.
 /// </summary>
 public byte[] Final()
 {
     hashInt = (ushort)~hashInt;
     BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
     return(BigEndianBitConverter.GetBytes(hashInt));
 }