• 0

[C#] Calculating a BZIP2 CRC?


Question

I have a file where I need to recalculate the CRC32 and BZIP2 at certain offsets. I found a class online that does CRC32 but how can I change it so it will also calc the BZIP2 hash? The class can be found here and the original article where I found it here.

 

My code:

// Open the file to read
FileStream input = new FileStream(inputfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader reader = new BinaryReader(input);

// Read bytes from DB to calc CRC32
reader.ReadBytes(0x0000001C);
int bytes = Convert.ToInt32(input.Length - 0x1C);
byte[] buffer = reader.ReadBytes(bytes);
input.Close();

// Calculates the CRC32 Hash
Crc32 crc32 = new Crc32();
String hash = String.Empty;
foreach (byte b in crc32.ComputeHash(buffer)) hash += b.ToString("x2").ToLower();

// Writes CRC32 to file
BinaryWriter bw = new BinaryWriter(File.Open(inputfile, FileMode.Open));
bw.BaseStream.Seek(0x00000010, SeekOrigin.Begin);
bw.Write(HexStringToByteArray(hash));
bw.Close();
            
}
public static byte[] HexStringToByteArray(string hexString)
{
    if (hexString.Length % 2 != 0)
    {
	throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
    }

    byte[] HexAsBytes = new byte[hexString.Length / 2];
    for (int index = 0; index < HexAsBytes.Length; index++)
    {
	string byteValue = hexString.Substring(index * 2, 2);
	HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
    }

    return HexAsBytes;
}
Link to comment
https://www.neowin.net/forum/topic/1179597-c-calculating-a-bzip2-crc/
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.