private static bool VerifyChecksum(byte[] raw, XISFChecksumTypeEnum cksumType, string providedCksum) { string computedCksum; SHA3Managed sha3; using (MyStopWatch.Measure($"XISF Checksum = {cksumType}")) { switch (cksumType) { case XISFChecksumTypeEnum.SHA1: SHA1 sha1 = new SHA1CryptoServiceProvider(); computedCksum = GetStringFromHash(sha1.ComputeHash(raw)); sha1.Dispose(); break; case XISFChecksumTypeEnum.SHA256: SHA256 sha256 = new SHA256CryptoServiceProvider(); computedCksum = GetStringFromHash(sha256.ComputeHash(raw)); sha256.Dispose(); break; case XISFChecksumTypeEnum.SHA512: SHA512 sha512 = new SHA512CryptoServiceProvider(); computedCksum = GetStringFromHash(sha512.ComputeHash(raw)); sha512.Dispose(); break; case XISFChecksumTypeEnum.SHA3_256: sha3 = new SHA3Managed(256); computedCksum = GetStringFromHash(sha3.ComputeHash(raw)); sha3.Dispose(); break; case XISFChecksumTypeEnum.SHA3_512: sha3 = new SHA3Managed(512); computedCksum = GetStringFromHash(sha3.ComputeHash(raw)); sha3.Dispose(); break; default: return(false); } } if (computedCksum.Equals(providedCksum)) { return(true); } else { Logger.Error($"XISF: Invalid data block checksum! Expected: {providedCksum} Got: {computedCksum}"); return(false); } }
/// <summary> /// Convert the ushort array to a byte arraay, compressing with the requested algorithm if required /// </summary> /// <param name="data"></param> /// <returns>Uncompressed or compressed byte array</returns> private byte[] PrepareArray(ushort[] data) { byte[] outArray; /* * Convert the ushort[] into a byte[] * From here onwards we deal in byte arrays only */ byte[] byteArray = new byte[data.Length * ShuffleItemSize]; Buffer.BlockCopy(data, 0, byteArray, 0, data.Length * ShuffleItemSize); /* * Compress the data block as configured. */ using (MyStopWatch.Measure($"XISF Compression = {CompressionType}")) { if (CompressionType == XISFCompressionTypeEnum.LZ4) { if (ByteShuffling) { CompressionName = "lz4+sh"; byteArray = Shuffle(byteArray, ShuffleItemSize); } else { CompressionName = "lz4"; } byte[] tmpArray = new byte[LZ4Codec.MaximumOutputSize(byteArray.Length)]; int compressedSize = LZ4Codec.Encode(byteArray, 0, byteArray.Length, tmpArray, 0, tmpArray.Length, LZ4Level.L00_FAST); outArray = new byte[compressedSize]; Array.Copy(tmpArray, outArray, outArray.Length); tmpArray = null; } else if (CompressionType == XISFCompressionTypeEnum.LZ4HC) { if (ByteShuffling) { CompressionName = "lz4hc+sh"; byteArray = Shuffle(byteArray, ShuffleItemSize); } else { CompressionName = "lz4hc"; } byte[] tmpArray = new byte[LZ4Codec.MaximumOutputSize(byteArray.Length)]; int compressedSize = LZ4Codec.Encode(byteArray, 0, byteArray.Length, tmpArray, 0, tmpArray.Length, LZ4Level.L06_HC); outArray = new byte[compressedSize]; Array.Copy(tmpArray, outArray, outArray.Length); tmpArray = null; } else if (CompressionType == XISFCompressionTypeEnum.ZLIB) { if (ByteShuffling) { CompressionName = "zlib+sh"; byteArray = Shuffle(byteArray, ShuffleItemSize); } else { CompressionName = "zlib"; } outArray = ZlibStream.CompressBuffer(byteArray); } else { outArray = new byte[byteArray.Length]; Array.Copy(byteArray, outArray, outArray.Length); CompressionName = null; } } // Revert to the original data array in case the compression is bigger than uncompressed if (outArray.Length > byteArray.Length) { if (ByteShuffling) { //As the original array is shuffled it needs to be unshuffled again - this scenario should be highly unlikely anyways outArray = Unshuffle(byteArray, ShuffleItemSize); } else { outArray = byteArray; } CompressionType = XISFCompressionTypeEnum.NONE; Logger.Debug("XISF output array is larger after compression. Image will be prepared uncompressed instead."); } if (CompressionType != XISFCompressionTypeEnum.NONE) { double percentChanged = (1 - ((double)outArray.Length / (double)byteArray.Length)) * 100; Logger.Debug($"XISF: {CompressionType} compressed {byteArray.Length} bytes to {outArray.Length} bytes ({percentChanged.ToString("#.##")}%)"); } /* * Checksum the data block as configured. * If the data block is compressed, we always checksum the compressed form, not the uncompressed form. */ using (MyStopWatch.Measure($"XISF Checksum = {ChecksumType}")) { SHA3Managed sha3; switch (ChecksumType) { case XISFChecksumTypeEnum.SHA1: SHA1 sha1 = new SHA1CryptoServiceProvider(); Checksum = GetStringFromHash(sha1.ComputeHash(outArray)); ChecksumName = "sha-1"; sha1.Dispose(); break; case XISFChecksumTypeEnum.SHA256: SHA256 sha256 = new SHA256CryptoServiceProvider(); Checksum = GetStringFromHash(sha256.ComputeHash(outArray)); ChecksumName = "sha-256"; sha256.Dispose(); break; case XISFChecksumTypeEnum.SHA512: SHA512 sha512 = new SHA512CryptoServiceProvider(); Checksum = GetStringFromHash(sha512.ComputeHash(outArray)); ChecksumName = "sha-512"; sha512.Dispose(); break; case XISFChecksumTypeEnum.SHA3_256: sha3 = new SHA3Managed(256); Checksum = GetStringFromHash(sha3.ComputeHash(outArray)); ChecksumName = "sha3-256"; sha3.Dispose(); break; case XISFChecksumTypeEnum.SHA3_512: sha3 = new SHA3Managed(512); Checksum = GetStringFromHash(sha3.ComputeHash(outArray)); ChecksumName = "sha3-512"; sha3.Dispose(); break; case XISFChecksumTypeEnum.NONE: default: Checksum = null; ChecksumName = null; break; } } return(outArray); }