/// <summary> /// Generates a new Install Code with the specified length /// </summary> /// <param name="size">The size of the Install Code to generate</param> /// <returns>The new install code</returns> // Revision History // MM/DD/YY Who Version Issue# Description // -------- --- ------- ------ ------------------------------------------- // 08/30/11 RCG 2.52.10 Created public static byte[] GenerateInstallCode(InstallCodeSize size) { RNGCryptoServiceProvider RandomGenerator = new RNGCryptoServiceProvider(); byte[] NewCode = new byte[(int)size]; byte[] InstallCode = new byte[(int)size + 2]; CrcHelper CrcHelper = new CrcHelper(CrcAlgorithmType.Crc16Ccitt); ushort CRC = 0; RandomGenerator.GetBytes(NewCode); CRC = (ushort)CrcHelper.CalculateCrc(NewCode); Array.Copy(NewCode, 0, InstallCode, 0, NewCode.Length); InstallCode[InstallCode.Length - 2] = (byte)CRC; InstallCode[InstallCode.Length - 1] = (byte)(CRC >> 8); return(InstallCode); }
private ulong[] CreateTable() { ulong bit, crc; ulong[] crctab = new ulong[256]; for (ulong i = 0; i < (ulong)crctab.Length; i++) { crc = i; if (this._reflectIn) { crc = CrcHelper.Reflect(crc, 8); } crc <<= this._order - 8; for (int j = 0; j < 8; j++) { bit = crc & this._crcHighBit; crc <<= 1; if (bit > 0) { crc ^= this._polynom; } } if (this._reflectIn) { crc = CrcHelper.Reflect(crc, this._order); } crc &= this._crcMask; crctab[i] = crc; } return(crctab); }
/// <summary> /// Uses a fast lookup table algorithm without augmented zero bytes to produce a CRC for the provided input. /// </summary> /// <remarks> /// This algorithm is only usable for polynomial orders of 8, 16, 24 or 32. /// </remarks> /// <param name="input">The target of the CRC.</param> /// <returns>A <see cref="UInt64"/> containing the CRC value that was calculated for the provided input.</returns> public ulong CalculateCrc(byte[] input) { ulong crc = this._crcInitialDirect; if (_reflectIn) { crc = CrcHelper.Reflect(crc, this._order); } int len = input.Length; int index = 0; if (!this._reflectIn) { while (len-- > 0) { crc = (crc << 8) ^ this._table[((crc >> (this._order - 8)) & 0xff) ^ input[index++]]; } } else { while (len-- > 0) { crc = (crc >> 8) ^ this._table[(crc & 0xff) ^ input[index++]]; } } if (this._reflectOut ^ this._reflectIn) { crc = CrcHelper.Reflect(crc, this._order); } crc ^= this._crcXor; crc &= this._crcMask; return(crc); }